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.

Β§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 invokingpin_drop!andpin_set!on the givenvar, which have in turn been designed to avoid silly borrow-checking errors. - pin_
drop - Drops, in-place, the
value: Tto which the given$var: Pin<&mut T>points to. - pin_set
- Same as
Pin::set(&mut $var, $value), but usable even afterpin_drop!has been invoked on the given$var.