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