boxpin is a tiny Rust crate that exposes Box::pin(...) as a readable suffix:
use boxpin::BoxPinExt;
let future = async { 42 }.pinned();
let value = futures::executor::block_on(future);
assert_eq!(value, 42);It does not perform type erasure and it does not return BoxFuture.
It is exactly equivalent to:
let future = Box::pin(async { 42 });This repo started as a benchmark project comparing .boxed() and Box::pin().
.boxed() is often nicer to read in suffix-heavy async code, but it also adds
type erasure and dynamic dispatch. boxpin keeps the suffix ergonomics while
preserving the concrete type inside Pin<Box<T>>.
pub trait BoxPinExt: Sized {
fn pinned(self) -> Pin<Box<Self>>;
}The trait is implemented for every Sized type, so .pinned() also works for
plain values, not just futures.
This repository still contains the local Criterion benchmark harness used to compare:
- no boxing
Box::pin(...).boxed()
Run it with:
cargo test
cargo benchLicensed under either of:
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)