Crate droppable_pin

Crate droppable_pin 

Source
Expand description

Β§::droppable-pin

The eponoymous droppable_pin! macro around a given let var = pin!() declaration allows invoking pin_drop! and pin_set! on the given var, which have in turn been designed to avoid silly borrow-checking errors.

Repository Latest version Documentation MSRV unsafe used so that you don’t License CI no_std compatible

just some flair around *dropping* pins, and dropping Mufasa from the Lion King


Β§Example

use ::core::pin::pin;
use ::droppable_pin::{droppable_pin, pin_drop, pin_set}; // πŸ‘ˆ
use ::futures_util::future::{Fuse, FusedFuture, FutureExt};

async fn foo() {}
async fn bar(_borrowed: &mut i32) {}

let mut borrowed = 42;
droppable_pin! { // πŸ‘ˆ
    let mut a = pin!(Fuse::terminated()); // Reminder: `Fuse::terminated()` is akin to `None`,
    let mut b = pin!(Fuse::terminated()); // and `future().fuse()`, to `Some(future())`.
}
loop {
    if a.is_terminated() {
        // πŸ‘‡
        pin_set!(a, foo().fuse());
        // same as:
        a.set(foo().fuse());
    }
    if b.is_terminated() {
        // 1. Needed because of the `&mut borrowed` capture (see # Motivation).
        // πŸ‘‡
        pin_drop!(b);
        pin_set!(b, bar(&mut borrowed).fuse());
        // πŸ‘†
        // 2. Cannot use `Pin::set()` here because of `pin_drop!()`.
    }
    ::futures_util::select! {
        () = a.as_mut() => {
            /* handle this case... */
        },
        () = b.as_mut() => {
            /* handle this case... */
        },
    }
}

See the docs of droppable_pin! for more information and the motivation behind this.

MacrosΒ§

droppable_pin
Invoke this macro around a given let [mut] var = pin!() declaration to allow invoking pin_drop! and pin_set! on the given var, which have in turn been designed to avoid silly borrow-checking errors.
pin_drop
Drops, in-place, the value: T to which the given $var: Pin<&mut T> points to.
pin_set
Same as Pin::set(&mut $var, $value), but usable even after pin_drop! has been invoked on the given $var.