生 rust 的libp2p无法在机箱libp2p中找到函数development_transport

4uqofj5v  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(158)

我的代码总是得到错误“cannot find function development_transport in crate 'libp2p'。当我进入libp2p库时(单击编辑器中的import语句),它在libp2p的该文件(lib.rs)中显示函数development_transport。有人知道为什么找不到该函数吗?提前感谢。

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包含以下内容:

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

huus2vyu1#

查看source code,我看到以下内容:


# [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)>> {
...

因此,需要首先在Cargo.toml配置文件中启用某些特性。
正如你所看到的here,他们说:
此版本有61个功能标志,默认情况下启用0个。

相关问题