The following program fails to compile, complaining that the future for bar is not Send because it contains a MutexGuard:
use std::sync::Mutex;
async fn foo() {
todo!();
}
async fn bar(mutex: &Mutex<()>) {
let mut guard = mutex.lock().unwrap();
loop {
drop(guard);
foo().await;
guard = mutex.lock().unwrap();
}
}
fn check_send() {
fn require_send<T: Send>(_: T) {}
require_send(bar(&Mutex::new(())));
}
(Playground)
The error:
error: future cannot be sent between threads safely
--> src/lib.rs:18:18
|
18 | require_send(bar(&Mutex::new(())));
| ^^^^^^^^^^^^^^^^^^^^ future returned by `bar` is not `Send`
|
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ()>`
note: future is not `Send` as this value is used across an await
--> src/lib.rs:11:15
|
8 | let mut guard = mutex.lock().unwrap();
| --------- has type `std::sync::MutexGuard<'_, ()>` which is not `Send`
...
11 | foo().await;
| ^^^^^ await occurs here, with `mut guard` maybe used later
However this code should be accepted—there is not actually any MutexGuard alive across the await expression, so it is sound. Rejecting it makes it hard to write and use async condition variable types.
The following program fails to compile, complaining that the future for
baris notSendbecause it contains aMutexGuard:(Playground)
The error:
However this code should be accepted—there is not actually any
MutexGuardalive across the await expression, so it is sound. Rejecting it makes it hard to write and use async condition variable types.