rust async_error ai_generated true

the trait `Unpin` is not implemented for `impl Future<Output = T>`

ID: rust/rust-pin-unpin-error

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Async futures are not Unpin by default. Use Box::pin or pin! macro to handle self-referential futures.

generic

Workarounds

  1. 92% success Use Box::pin() to pin the future on the heap
    let fut = Box::pin(async { ... });
    // or for trait objects: Box<dyn Future<Output=T> + Send>

    Sources: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.pin

  2. 88% success Use tokio::pin! or std::pin::pin! macro for stack pinning
    use tokio::pin;
    let fut = async { ... };
    pin!(fut);
    fut.await;

    Sources: https://docs.rs/tokio/latest/tokio/macro.pin.html

Dead Ends

Common approaches that don't work:

  1. Implement Unpin for the future type manually 90% fail

    Self-referential futures are unsound to move; manually implementing Unpin breaks safety guarantees

Error Chain

Leads to:
Preceded by:
Frequently confused with: