trait-concept

Send depends on state across suspension

A future can be Send when non-Send temporaries do not survive into its stored suspension state. Values that are held across await can prevent the future from being Send. The fixture uses a scoped Rc and then awaits an immediately ready future. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 6 Common mistake: Assuming any use of Rc anywhere in async code always makes the future non-Send.

Formal Syntax & Structure

rust syntaxrust
fn require_send<F: Future + Send>(f: F) -> F

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Waker}};    fn ready_now<F: Future>(f: F) -> F::Output {        let mut f = std::pin::pin!(f);        let mut cx = Context::from_waker(Waker::noop());        match f.as_mut().poll(&mut cx) {            Poll::Ready(value) => value,            Poll::Pending => panic!("fixture expected immediate readiness"),        }    }    fn require_send<F: Future + Send>(f: F) -> F { f }    let f = async {        let value = { let local = std::rc::Rc::new(6u32); *local };        std::future::ready(value).await    };    println!("{}", ready_now(require_send(f)));}
Rust Reference