小编Exc*_*ser的帖子

Rust“未来无法在线程之间安全地发送”

我正在调用异步实现的方法:

let mut safebrowsing: MutexGuard<Safebrowsing> = self.safebrowsing.lock().unwrap();
safebrowsing.is_safe(input: &message.content).await;
Run Code Online (Sandbox Code Playgroud)

is_safe -方法:

pub async fn is_safe(&mut self, input: &str) {
    let links = self.finder.links(input);

    for link in links {
        match reqwest::get("url").await {
            Ok(response) => {
                println!(
                    "{}",
                    response.text().await.expect("response has no content")
                );
            }
            Err(_) => {
                println!("Unable to get safebrowsing-response")
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是,通过异步调用is_safe -方法,编译器告诉我线程无法安全发送。该错误是关于:

future cannot be sent between threads safely
within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `std::sync::MutexGuard<'_, Safebrowsing>`
required for the cast …
Run Code Online (Sandbox Code Playgroud)

multithreading mutex rust

18
推荐指数
2
解决办法
2万
查看次数

标签 统计

multithreading ×1

mutex ×1

rust ×1