#pin

ergo-pin

Ergonomic stack pinning

1 unstable release

0.1.0 Sep 22, 2019

#12 in #pin

Download history 41/week @ 2021-04-11 23/week @ 2021-04-18 34/week @ 2021-04-25 17/week @ 2021-05-02 1/week @ 2021-05-09 2/week @ 2021-05-16 661/week @ 2021-05-23 196/week @ 2021-05-30 43/week @ 2021-06-06 35/week @ 2021-06-13 33/week @ 2021-06-20 34/week @ 2021-06-27 106/week @ 2021-07-04 35/week @ 2021-07-11 87/week @ 2021-07-18 187/week @ 2021-07-25

68 downloads per month
Used in 2 crates

MIT/Apache

11KB
126 lines

ergo-pin

Immobilis ergo pin

Ergonomic stack pinning for Rust.

ergo-pin exports a single proc-macro-attribute #[ergo_pin] that can be applied to a function/block/tt-accepting-macro-invocation to provide the "magical" pin! within the scope. You can consider this pin! macro equivalent to a function with the signature:

extern "bla̴ck̀ mag̸ic͘" fn pin!<T>(t: T) -> Pin<&'local mut T>;

it will take in any value and return a Pin<&mut _> of the value, with the correct local stack lifetime.

Internals

Internally the pin! macro works by injecting extra statements before the statement that contains it to create this Pinned reference, it's probably easiest to see what it's doing by example. Given some code like

#[ergo_pin] {
    quux(pin!(Foo::new().bar()).baz());
}

this will be re-written to

{
    let mut __ergo_pin_0 = Foo::new().bar();
    let __ergo_pin_0 = unsafe {
        ::core::pin::Pin::new_unchecked(&mut __ergo_pin_0)
    };
    quux(__ergo_pin_0.baz());
}

The expression passed to pin! is first bound to a local variable, this is then shadowed by a Pinned reference to itself (you may recognise this from the pin-utils::pin_mut! stack pinning macro), finally the pin! call is replaced with the local variable used.

Since this requires re-writing code outside the macro this can't be implemented by as a normal pin! macro, which is why the main entrypoint is the #[ergo_pin] attribute.

Rust Version Policy

This crate only supports the current stable version of Rust, patch releases may use new features at any time.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~325–750KB
~19K SLoC