trait-concept
Explicit Send future in a trait API
A trait method can state that the returned future implements Send. This is a useful public API decision when callers must move tasks between worker threads. Adding Send later can break implementations, while removing it can break callers. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 9 Common mistake: Assuming Self: Send automatically implies all of its async methods return Send futures.
Formal Syntax & Structure
rust syntaxrust
fn count(&self) -> impl Future<Output = u32> + Send + '_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"), } } trait Lookup { fn count(&self) -> impl Future<Output=u32> + Send + '_; } struct Memory(u32); impl Lookup for Memory { fn count(&self) -> impl Future<Output=u32> + Send + '_ { async move { self.0 } } } fn check_send<F: Future + Send>(f: F) -> F { f } let source = Memory(9); println!("{}", ready_now(check_send(source.count())));}