问题描述
我正在关注 mdns Rust 文档 并粘贴了示例代码,但它抛出以下错误:
线程main"因没有反应器在运行,必须从 Tokio 运行时的上下文中调用"而恐慌
这是我拥有的代码:
use futures_util::{pin_mut, stream::StreamExt};使用 mdns::{Error, Record, RecordKind};使用 std::{net::IpAddr, time::Duration};const SERVICE_NAME: &'static str = "_googlecast._tcp.local";#[东京::主要]async fn main() ->结果{//迭代来自每个 Cast 设备的响应,每 15 秒请求一次新设备让流 = mdns::discover::all(SERVICE_NAME, Duration::from_secs(15))?.listen();pin_mut!(流);而让 Some(Ok(response)) = stream.next().await {让 addr = response.records().filter_map(self::to_ip_addr).next();如果让 Some(addr) = addr {println!(在{}找到投射设备",地址);} 别的 {println!(投射设备不通告地址");}}好的(())}fn to_ip_addr(record: &Record) ->选项{匹配记录.种类{RecordKind::A(addr) =>一些(addr.into()),RecordKind::AAAA(addr) =>一些(addr.into()),_ =>没有任何,}}
依赖:
[依赖项]mdns = "1.1.0";期货-util =0.3.8";tokio = { version = "0.3.3", features = ["full"] }
我错过了什么?我尝试在网上查找,但还没有找到如何为此用例创建反应器.
您使用的是较新版本的 Tokio,例如 0.3 或 1.x,并且许多软件包(包括 mdns 1.1.0)依赖于较旧版本的Tokio,比如0.2.
% cargo tree -d东京 v0.2.22└── mdns v1.1.0└── example_project v0.1.0东京 v0.3.3└── example_project v0.1.0
目前,您需要匹配 Tokio 运行时的版本.最简单的方法是自己使用 Tokio 0.2.tokio-compat-02 crate 在某些情况下也可能有用.
另见:
具有相同根本原因的各种错误消息:
没有反应器在运行,必须从 Tokio 1.x 运行时的上下文中调用
没有反应器在运行,必须从 Tokio 运行时的上下文中调用
当前未在 Tokio 运行时上运行
I'm following the mdns Rust documentation and pasted the example code but it throws the following error:
thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime'
Here's the code that I have:
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:
[dependencies]
mdns = "1.1.0"
futures-util = "0.3.8"
tokio = { version = "0.3.3", features = ["full"] }
What am I missing? I tried looking online but haven't found how to create a reactor for this use case.
You are using a newer version of Tokio, such as 0.3 or 1.x, and many packages, including mdns 1.1.0, rely on an older version of Tokio, such as 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
For now, you will need to match versions of the Tokio runtime. The easiest way is to use Tokio 0.2 yourself. The tokio-compat-02 crate may also be useful in some cases.
See also:
Various error messages with the same root cause:
这篇关于为什么我收到错误“没有反应器正在运行,必须从 Tokio 运行时的上下文中调用"?即使我有#[tokio::main]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!