rust 为什么即使我有#[时雄::main],也会出现错误“没有React器在运行,必须从时雄运行时的上下文调用”?

13z8s7eq  于 2023-10-20  发布在  React
关注(0)|答案(4)|浏览(172)

我跟随the mdns Rust documentation并粘贴了示例代码,但它抛出了以下错误:

thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime'

下面是我的代码:

use futures_util::{pin_mut, stream::StreamExt};
use mdns::{Error, Record, RecordKind};
use std::{net::IpAddr, time::Duration};

const SERVICE_NAME: &'static str = "_googlecast._tcp.local";

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Iterate through responses from each Cast device, asking for new devices every 15s
    let stream = mdns::discover::all(SERVICE_NAME, Duration::from_secs(15))?.listen();
    pin_mut!(stream);

    while let Some(Ok(response)) = stream.next().await {
        let addr = response.records().filter_map(self::to_ip_addr).next();

        if let Some(addr) = addr {
            println!("found cast device at {}", addr);
        } else {
            println!("cast device does not advertise address");
        }
    }

    Ok(())
}

fn to_ip_addr(record: &Record) -> Option<IpAddr> {
    match record.kind {
        RecordKind::A(addr) => Some(addr.into()),
        RecordKind::AAAA(addr) => Some(addr.into()),
        _ => None,
    }
}

依赖项:

[dependencies]
mdns = "1.1.0"
futures-util = "0.3.8"
tokio = { version = "0.3.3", features = ["full"] }

我错过了什么?我试着在网上寻找,但还没有找到如何为这个用例创建一个React器。

n7taea2i

n7taea2i1#

您使用的是较新版本的时雄,如0.3或1.x,而许多软件包(包括mdns 1.1.0)依赖于较旧版本的时雄,如0.2。

% cargo tree -d
tokio v0.2.22
└── mdns v1.1.0
    └── example_project v0.1.0

tokio v0.3.3
└── example_project v0.1.0

现在,您需要匹配时雄运行时的版本。最简单的方法是自己使用时雄0. 2。tokio-compat-02 crate在某些情况下也可能有用。
另请参阅:

具有相同根本原因的各种错误消息:
没有React器正在运行,必须从时雄1.x运行时的上下文调用
没有React器正在运行,必须从时雄运行时的上下文调用
当前未在时雄运行时上运行

h79rfbju

h79rfbju2#

我的修正是将这个添加到Cargo.toml

[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }

https://github.com/ATiltedTree/ytextract/issues/25

iyfamqjs

iyfamqjs3#

在撰写本文时,相当数量的crates已经在使用时雄v1,但其他人可能仍处于试验阶段。检查您的板条箱中是否有 * 预发布版本 *,这些版本可能已经升级了时雄运行时兼容性。
一个相关的例子是actix-web,它从版本4开始就使用时雄的运行时1.0。尽管自2022-01-07以来有此主要增量的预发布,但4.0.0版本仅于2022-02-25发布。

actix-web = { version = "4.0.0-beta.10" }
4xy9mtcn

4xy9mtcn4#

我运行了产生错误的相同代码,只对依赖项进行了更改。

[dependencies]
mdns = "3.0.0"
futures-util = "0.3.28"
tokio = { version = "1.32.0", features 
  = ["full"] }
    
    use futures_util::{pin_mut, 
      stream::StreamExt};
    use mdns::{Error, Record, 
      RecordKind};
    use std::{net::IpAddr, 
      time::Duration};
    
    const SERVICE_NAME: &'static str = 
      "_googlecast._tcp.local";
    
    #[tokio::main]
    async fn main() -> Result<(), 
      Error> {
        // Iterate through responses from 
        // each Cast device, asking for new 
        // devices every 15s
        let stream = 
          mdns::discover::all(SERVICE_NAME, 
          Duration::from_secs(15))?.listen();
        pin_mut!(stream);
    
        while let Some(Ok(response)) = 
          stream.next().await 
        {
            let addr = 
            response.records().filter_map(  
              self::to_ip_addr).next();
    
            if let Some(addr) = addr {
                println!("found cast device 
                  at {}", addr);
            } else {
                println!("cast device does 
                  not advertise address");
            }
        }
    
        Ok(())
    }
    
    fn to_ip_addr(record: &Record) -> 
        Option<IpAddr> {
        match record.kind {
            RecordKind::A(addr) => 
              Some(addr.into()),
            RecordKind::AAAA(addr) => 
              Some(addr.into()),
            _ => None,
        }
    }

// A small sample of the output I 
// received without error is below:

found cast device at 10.0.0.165
found cast device at 10.0.0.171
found cast device at 10.0.0.158

相关问题