rustrust-tokiolibp2p

Rust libp2p cannot find function development_transport in crate libp2p


My code keeps getting the error 'cannot find function development_transport in crate 'libp2p'. When I go the libp2p library (click on the import statement in the editor) it shows the function development_transport in that file (lib.rs) of libp2p. Does anyone know why the function cannot be found? Thanks in advance.

use libp2p::futures::StreamExt;
use libp2p::swarm::dummy::Behaviour;
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::{identity, PeerId};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let local_key = identity::Keypair::generate_ed25519();

    let local_peer_id = PeerId::from(local_key.public());

    println!("Local peer id is: {}", local_peer_id);

    let behaviour = Behaviour;

    let transport = libp2p::development_transport(local_key)?;

    let mut swarm = Swarm::new(transport, behaviour, local_peer_id);

    swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

    loop {
        match swarm.select_next_some().await {
            SwarmEvent::NewListenAddr { address, .. } => {
                println!("Listening on local address {:?}", address)
            }
            _ => {}
        }
    }
}

My Cargo.toml contains the following:

[dependencies]
libp2p = "0.49.0"
tokio = { version = "1.21.2", features = ["full"] }

Solution

  • Looking at the source code, I see this:

    #[cfg(all(
        not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")),
        any(
            all(feature = "tcp-async-io", feature = "dns-async-std"),
            all(feature = "tcp", feature = "dns", feature = "async-std")
        ),
        feature = "websocket",
        feature = "noise",
        feature = "mplex",
        feature = "yamux"
    ))]
    #[cfg_attr(
        docsrs,
        doc(cfg(all(
            not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")),
            any(
                all(feature = "tcp-async-io", feature = "dns-async-std"),
                all(feature = "tcp", feature = "dns", feature = "async-std")
            ),
            feature = "websocket",
            feature = "noise",
            feature = "mplex",
            feature = "yamux"
        )))
    )]
    #[cfg_attr(
        all(
            any(feature = "tcp-async-io", feature = "dns-async-std"),
            not(feature = "async-std")
        ),
        deprecated(
            since = "0.49.0",
            note = "The `tcp-async-io` and `dns-async-std` features are deprecated. Use the new `tcp` and `dns` features together with the `async-std` feature."
        )
    )]
    pub async fn development_transport(
        keypair: identity::Keypair,
    ) -> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>> {
    ...
    

    So there are certain features that need to be enabled in your Cargo.toml config file first.

    As you can see here, they say:

    This version has 61 feature flags, 0 of them enabled by default.