You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#![allow(unused)]structNotSend(std::marker::PhantomData<*mut()>);implNotSend{fnnew() -> Self{Self(std::marker::PhantomData)}}unsafeimplSyncforNotSend{}#[derive(Clone)]structWatchRecv;implWatchRecv{fnborrow(&self) -> Ref<'_>{Ref(self,NotSend::new())}}structRef<'a>(&'aWatchRecv,NotSend);fnassert_send<F: std::future::Future + Send>(f:F) -> F{ f }implRef<'_>{fnlen(&self) -> usize{0}}asyncfnanother_future(){loop{}}asyncfnreal_main(){let rx = WatchRecv;letmut binding = rx.clone();assert_send(asyncmove{// This doesn't work.let value = binding.borrow();let len = value.len();drop(value);println!("len: {len}");/* // This works. let len = { let value = binding.borrow(); value.len() }; */another_future().await;}).await;println!("{:?}", rx.borrow().len());}
I expected to see this happen: pass compile
Instead, this happened: error
Compiling playground v0.0.1 (/playground)
error: future cannot be sent between threads safely
--> src/lib.rs:37:5
|
37 | / assert_send(async move {
38 | |
39 | | // This doesn't work.
40 | | let value = binding.borrow();
... |
52 | | another_future().await;
53 | | }).await;
| |______^ future created by async block is not `Send`
|
= help: within `{async block@src/lib.rs:37:17: 53:6}`, the trait `Send` is not implemented for `*mut ()`, which is required by `{async block@src/lib.rs:37:17: 53:6}: Send`
note: future is not `Send` as this value is used across an await
--> src/lib.rs:52:26
|
40 | let value = binding.borrow();
| ----- has type `Ref<'_>` which is not `Send`
...
52 | another_future().await;
| ^^^^^ await occurs here, with `value` maybe used later
note: required by a bound in `assert_send`
--> src/lib.rs:24:41
|
24 | fn assert_send<F: std::future::Future + Send>(f: F) -> F { f }
| ^^^^ required by this bound in `assert_send`
error: could not compile `playground` (lib) due to 1 previous error
but if I use the following code:
let len = {
let value = binding.borrow();
value.len()
};
I tried this code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5203de66797e0725271a464be3604d0c
I expected to see this happen: pass compile
Instead, this happened: error
but if I use the following code:
it will pass compile.
Meta
rustc --version --verbose
:And rust online playground.
The text was updated successfully, but these errors were encountered: