Libp2p: How to get Poll<Option<SwarmEvent<...>>> instead of Poll<Option<()...>>

I'm trying to write a libp2p application and don't understand why I don't get the same types of poll results in my event loop as the libp2p chat example (https://github.com/libp2p/rust-libp2p/blob/master/examples/chat.rs, line 161).
The ExpandedSwarm Stream trait implementation is supposed to return items of type behaviour::OutEvent which is () for a behaviour constructed using #[derive(NetworkBehaviour)], and in my code it does exactly that, so I don't see SwarmEvents.
In the chat example (and other examples in the libp2p crate) the type of swarm.poll_next_unpin() is Poll<Option<SwarmEvent<(), EitherError<ProtocolsHandlerUpgrErr<Error>, Void>>>>

What am I supposed to do to achieve this? I've been trying to wrap my head around the type trickery for days and I just don't get it.

Cheers,
Hans-Martin

Well d'oh.

The crates.io libp2p is at version 0.38, while the github code I was looking at for the examples is at 0.39.
Apparently this little thing changed.

I don't know the lib, but do you have read this NetworkBehaviour in libp2p::swarm - Rust ?

I think by default the derive sets the NetworkBehaviour::OutEvent as (), means it would be SwarmEvent<(), ...>.
Are you using the DummyBehaviour? There is no OutEvent.

How does your transport, behaviour and swarm init looks like?

Couldn't you post your code anywhere to have an idea what you are doing? :wink:

It's the difference between 0.38 and 0.39. I copied the example chat.rs from github into a fresh project with this Cargo.toml:

[package]
name = "chat"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libp2p = "0.38"
futures = "0.3.1"
env_logger = "0.8.1"
async-std = { version = "1.6.2", features = ["attributes"] }

and get the same error. When I replace the libp2p line with libp2p = { git = "https://github.com/libp2p/rust-libp2p.git" } it just works. I also noticed that the examples in 0.38 had a different handling for the events from the swarm. In 0.38, the only events you could get through the swarm were your own NetworkBehaviour::OutEvent events, but in 0.39 you get SwarmEvent::..., and your OutEvents are wrapped in SwarmEvent::Behaviour(). Quite nice actually.

Cheers,
Hans-Martin

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.