Skip to main content

boxed

Attribute Macro boxed 

Source
#[boxed]
Expand description

Heap-allocate an async function’s state machine.

An async function’s future embeds every nested future and all locals held across awaits, so deep call chains compound into futures that bloat every caller. #[boxed] moves the state machine to the heap, shrinking the returned future to a pointer.

Use on cold entry points with large state machines (initialization, teardown, recovery). Avoid on hot paths: each call allocates.

§Example

use commonware_macros::boxed;

#[boxed]
async fn init() -> u64 {
    let buffer = [0u8; 1024];
    async {}.await;
    buffer.len() as u64
}

let fut = init();
assert!(std::mem::size_of_val(&fut) <= 16);