我正在调用异步实现的方法:
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)