Skip to main content

veilid_tools/
pin.rs

1use super::*;
2
3/// A heap-pinned box.
4pub type PinBox<T> = Pin<Box<T>>;
5/// A heap-pinned, `Send`, boxed trait-object future borrowing for `'a`.
6pub type PinBoxFuture<'a, T> = PinBox<dyn Future<Output = T> + Send + 'a>;
7/// A [`PinBoxFuture`] with a `'static` lifetime.
8pub type PinBoxFutureStatic<T> = PinBoxFuture<'static, T>;
9
10/// Pin a future to the heap, returning a concrete boxed future.
11///
12/// Moves the future to the heap from the caller's stack. May allocate the future on the
13/// stack first and then move it.
14#[macro_export]
15macro_rules! pin_future {
16    ($call: expr) => {
17        Box::pin($call)
18    };
19}
20
21/// Pin a future to the heap inside a closure, returning a concrete boxed future.
22///
23/// Keeps the future off the calling function's stack completely. The closure is still on
24/// the stack, but smaller than the future would be.
25#[macro_export]
26macro_rules! pin_future_closure {
27    ($call: expr) => {
28        (|| Box::pin($call))()
29    };
30}
31
32/// Pin a future to the heap, returning a [`PinBoxFuture`] trait object.
33///
34/// Moves the future to the heap from the caller's stack. May allocate the future on the
35/// stack first and then move it.
36#[macro_export]
37macro_rules! pin_dyn_future {
38    ($call: expr) => {
39        Box::pin($call) as PinBoxFuture<_>
40    };
41}
42
43/// Pin a future to the heap inside a closure, returning a [`PinBoxFuture`] trait object.
44///
45/// Keeps the future off the calling function's stack completely. The closure is still on
46/// the stack, but smaller than the future would be.
47#[macro_export]
48macro_rules! pin_dyn_future_closure {
49    ($call: expr) => {
50        (|| Box::pin($call) as PinBoxFuture<_>)()
51    };
52}