Skip to main content

BoxFuture

Type Alias BoxFuture 

Source
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
Expand description

Owned, pinned, type-erased future used by Effect::run and IntoBind.

§Why not “stack pin! only”?

An async body is a Future that often borrows the environment &mut R for its whole poll lifetime. Effect is a uniform Box<dyn EffectOp<…>> so combinators and dyn dispatch stay ergonomic. To return “something to poll” from run across that boundary, the compiler must erase the concrete future type → dyn Future + 'a, which in safe Rust is stored as Pin<Box<…>> (this alias).

std::pin::pin! can pin on the stack, but that Pin<&mut impl Future> is tied to the current stack frame and cannot be owned by Effect or returned from new_async without self-referential / unsafe patterns (this crate uses #![forbid(unsafe_code)]).

A thin wrapper future around a concrete Fut still ends in Pin<Box<…>> for the wrapper and adds extra state (e.g. init vs polling) versus a single async move { … } state machine, so the current shape keeps one allocation for the async body.

For bind-free bodies, effect! uses Effect::new instead, which keeps the work in the internal sync node and boxes only a trivial core::future::ready future on Effect::run. crate::runtime::run_blocking and crate::runtime::run_async short-circuit that sync node directly.

The boxed future is dyn Future (not Send) so crate::streaming::stream::Stream and other Rc-based code can use Effect; prefer crate::runtime::run_blocking or a single-threaded async runtime when driving Effect::run.

Aliased Type§

pub struct BoxFuture<'a, T> { /* private fields */ }