Skip to content

Commit ab7b5a4

Browse files
mexskicanmxinden
andauthored
swarm/src/either: Implement NetworkBehaviour on Either (libp2p#2370)
Implement `NetworkBehaviour` on `either::Either<L, R>` where both L and R both implement `NetworkBehaviour`. Add NetworkBehaviour derive tests for Either and Toggle Co-authored-by: Max Inden <[email protected]>
1 parent dd9e0a1 commit ab7b5a4

File tree

5 files changed

+392
-0
lines changed

5 files changed

+392
-0
lines changed

swarm-derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ quote = "1.0"
1919

2020
[dev-dependencies]
2121
libp2p = { path = "../" }
22+
either = "1.6.0"
2223
futures = "0.3.1"

swarm-derive/tests/test.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,98 @@ fn event_process_false() {
324324
};
325325
}
326326
}
327+
328+
#[test]
329+
fn with_toggle() {
330+
use libp2p::swarm::behaviour::toggle::Toggle;
331+
332+
#[allow(dead_code)]
333+
#[derive(NetworkBehaviour)]
334+
#[behaviour(event_process = true)]
335+
struct Foo {
336+
identify: libp2p::identify::Identify,
337+
ping: Toggle<libp2p::ping::Ping>,
338+
}
339+
340+
impl libp2p::swarm::NetworkBehaviourEventProcess<libp2p::identify::IdentifyEvent> for Foo {
341+
fn inject_event(&mut self, _: libp2p::identify::IdentifyEvent) {}
342+
}
343+
344+
impl libp2p::swarm::NetworkBehaviourEventProcess<libp2p::ping::PingEvent> for Foo {
345+
fn inject_event(&mut self, _: libp2p::ping::PingEvent) {}
346+
}
347+
348+
#[allow(dead_code)]
349+
fn foo() {
350+
require_net_behaviour::<Foo>();
351+
}
352+
}
353+
354+
#[test]
355+
fn with_either() {
356+
use either::Either;
357+
358+
#[allow(dead_code)]
359+
#[derive(NetworkBehaviour)]
360+
#[behaviour(event_process = true)]
361+
struct Foo {
362+
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
363+
ping_or_identify: Either<libp2p::ping::Ping, libp2p::identify::Identify>,
364+
}
365+
366+
impl libp2p::swarm::NetworkBehaviourEventProcess<libp2p::kad::KademliaEvent> for Foo {
367+
fn inject_event(&mut self, _: libp2p::kad::KademliaEvent) {}
368+
}
369+
370+
impl
371+
libp2p::swarm::NetworkBehaviourEventProcess<
372+
Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>,
373+
> for Foo
374+
{
375+
fn inject_event(
376+
&mut self,
377+
_: Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>,
378+
) {
379+
}
380+
}
381+
382+
#[allow(dead_code)]
383+
fn foo() {
384+
require_net_behaviour::<Foo>();
385+
}
386+
}
387+
388+
#[test]
389+
fn no_event_with_either() {
390+
use either::Either;
391+
392+
enum BehaviourOutEvent {
393+
Kad(libp2p::kad::KademliaEvent),
394+
PingOrIdentify(Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>),
395+
}
396+
397+
#[allow(dead_code)]
398+
#[derive(NetworkBehaviour)]
399+
#[behaviour(out_event = "BehaviourOutEvent", event_process = false)]
400+
struct Foo {
401+
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
402+
ping_or_identify: Either<libp2p::ping::Ping, libp2p::identify::Identify>,
403+
}
404+
405+
impl From<libp2p::kad::KademliaEvent> for BehaviourOutEvent {
406+
fn from(event: libp2p::kad::KademliaEvent) -> Self {
407+
BehaviourOutEvent::Kad(event)
408+
}
409+
}
410+
411+
impl From<Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>> for BehaviourOutEvent {
412+
fn from(event: Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>) -> Self {
413+
BehaviourOutEvent::PingOrIdentify(event)
414+
}
415+
}
416+
417+
#[allow(dead_code)]
418+
fn foo() {
419+
require_net_behaviour::<Foo>();
420+
}
421+
}

swarm/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
- Move `swarm::Toggle` to `swarm::behaviour::Toggle` (see [PR 2375]).
1212

13+
- Implement `swarm::NetworkBehaviour` on `either::Either` (see [PR 2370]).
14+
1315
[PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339
1416
[PR 2350]: https://github.com/libp2p/rust-libp2p/pull/2350
1517
[PR 2362]: https://github.com/libp2p/rust-libp2p/pull/2362
18+
[PR 2370]: https://github.com/libp2p/rust-libp2p/pull/2370
1619
[PR 2375]: https://github.com/libp2p/rust-libp2p/pull/2375
1720

1821
# 0.32.0 [2021-11-16]

swarm/src/behaviour.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21+
pub mod either;
2122
pub mod toggle;
2223

2324
use crate::dial_opts::DialOpts;
@@ -718,6 +719,50 @@ where
718719
}
719720
}
720721

722+
impl<TInEventOld, TOutEvent, THandlerOld> NetworkBehaviourAction<TOutEvent, THandlerOld>
723+
where
724+
THandlerOld: IntoProtocolsHandler,
725+
<THandlerOld as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEventOld>,
726+
{
727+
/// Map the handler and handler event.
728+
pub fn map_handler_and_in<THandlerNew, TInEventNew>(
729+
self,
730+
f_handler: impl FnOnce(THandlerOld) -> THandlerNew,
731+
f_in_event: impl FnOnce(TInEventOld) -> TInEventNew,
732+
) -> NetworkBehaviourAction<TOutEvent, THandlerNew>
733+
where
734+
THandlerNew: IntoProtocolsHandler,
735+
<THandlerNew as IntoProtocolsHandler>::Handler: ProtocolsHandler<InEvent = TInEventNew>,
736+
{
737+
match self {
738+
NetworkBehaviourAction::GenerateEvent(e) => NetworkBehaviourAction::GenerateEvent(e),
739+
NetworkBehaviourAction::Dial { opts, handler } => NetworkBehaviourAction::Dial {
740+
opts,
741+
handler: f_handler(handler),
742+
},
743+
NetworkBehaviourAction::NotifyHandler {
744+
peer_id,
745+
handler,
746+
event,
747+
} => NetworkBehaviourAction::NotifyHandler {
748+
peer_id,
749+
handler,
750+
event: f_in_event(event),
751+
},
752+
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
753+
NetworkBehaviourAction::ReportObservedAddr { address, score }
754+
}
755+
NetworkBehaviourAction::CloseConnection {
756+
peer_id,
757+
connection,
758+
} => NetworkBehaviourAction::CloseConnection {
759+
peer_id,
760+
connection,
761+
},
762+
}
763+
}
764+
}
765+
721766
/// The options w.r.t. which connection handler to notify of an event.
722767
#[derive(Debug, Clone)]
723768
pub enum NotifyHandler {

0 commit comments

Comments
 (0)