kutil_std/future/
captured.rs

1use std::{io, pin::*};
2
3/// A thread-safe and pinned `dyn` [Future].
4///
5/// Returning a [CapturedFuture] can allow us to call `async` code in a non-async function,
6/// e.g. the polling function for futures, readers, writers, etc.
7///
8/// See also [crate::capture_async].
9pub type CapturedFuture<OutputT> = Pin<Box<dyn Future<Output = OutputT> + Send>>;
10
11/// Captures async code into a [CapturedFuture].
12///
13/// This works by wrapping the code in `Box::pin(async move { ... } )`.
14#[macro_export]
15macro_rules! capture_async {
16    ( $( $code:tt )* ) => {
17        ::std::boxed::Box::pin(async move { $( $code )* })
18    };
19}
20
21/// A [CapturedFuture] for I/O tasks.
22pub type CapturedIoTask = CapturedFuture<io::Result<()>>;