tokio/macros/pin.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/// Pins a value on the stack.
///
/// Calls to `async fn` return anonymous [`Future`] values that are `!Unpin`.
/// These values must be pinned before they can be polled. Calling `.await` will
/// handle this, but consumes the future. If it is required to call `.await` on
/// a `&mut _` reference, the caller is responsible for pinning the future.
///
/// Pinning may be done by allocating with [`Box::pin`] or by using the stack
/// with the `pin!` macro.
///
/// The following will **fail to compile**:
///
/// ```compile_fail
/// async fn my_async_fn() {
/// // async logic here
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let mut future = my_async_fn();
/// (&mut future).await;
/// }
/// ```
///
/// To make this work requires pinning:
///
/// ```
/// use tokio::pin;
///
/// async fn my_async_fn() {
/// // async logic here
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let future = my_async_fn();
/// pin!(future);
///
/// (&mut future).await;
/// }
/// ```
///
/// Pinning is useful when using `select!` and stream operators that require `T:
/// Stream + Unpin`.
///
/// [`Future`]: trait@std::future::Future
/// [`Box::pin`]: std::boxed::Box::pin
///
/// # Usage
///
/// The `pin!` macro takes **identifiers** as arguments. It does **not** work
/// with expressions.
///
/// The following does not compile as an expression is passed to `pin!`.
///
/// ```compile_fail
/// async fn my_async_fn() {
/// // async logic here
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let mut future = pin!(my_async_fn());
/// (&mut future).await;
/// }
/// ```
///
/// # Examples
///
/// Using with select:
///
/// ```
/// use tokio::{pin, select};
/// use tokio_stream::{self as stream, StreamExt};
///
/// async fn my_async_fn() {
/// // async logic here
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let mut stream = stream::iter(vec![1, 2, 3, 4]);
///
/// let future = my_async_fn();
/// pin!(future);
///
/// loop {
/// select! {
/// _ = &mut future => {
/// // Stop looping `future` will be polled after completion
/// break;
/// }
/// Some(val) = stream.next() => {
/// println!("got value = {}", val);
/// }
/// }
/// }
/// }
/// ```
///
/// Because assigning to a variable followed by pinning is common, there is also
/// a variant of the macro that supports doing both in one go.
///
/// ```
/// use tokio::{pin, select};
///
/// async fn my_async_fn() {
/// // async logic here
/// }
///
/// #[tokio::main]
/// async fn main() {
/// pin! {
/// let future1 = my_async_fn();
/// let future2 = my_async_fn();
/// }
///
/// select! {
/// _ = &mut future1 => {}
/// _ = &mut future2 => {}
/// }
/// }
/// ```
#[macro_export]
macro_rules! pin {
($($x:ident),*) => { $(
// Move the value to ensure that it is owned
let mut $x = $x;
// Shadow the original binding so that it can't be directly accessed
// ever again.
#[allow(unused_mut)]
let mut $x = unsafe {
$crate::macros::support::Pin::new_unchecked(&mut $x)
};
)* };
($(
let $x:ident = $init:expr;
)*) => {
$(
let $x = $init;
$crate::pin!($x);
)*
};
}