summaryrefslogtreecommitdiffstats
path: root/tokio/src/macros
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2020-02-25 20:06:54 -0800
committerGitHub <noreply@github.com>2020-02-25 20:06:54 -0800
commit1dadc701c04879f1df4ddaa0df362297a144d7b3 (patch)
treec279e8428b6a0090a63f503d2ba126a277ebeaad /tokio/src/macros
parent10f1507cf44b62f4b126ecdd28351e4a4a1ce3f7 (diff)
macros: add assignment form to pin! (#2274)
Allows combining assignment to a binding and pinning it.
Diffstat (limited to 'tokio/src/macros')
-rw-r--r--tokio/src/macros/pin.rs34
1 files changed, 33 insertions, 1 deletions
diff --git a/tokio/src/macros/pin.rs b/tokio/src/macros/pin.rs
index e8511d3f..5b677610 100644
--- a/tokio/src/macros/pin.rs
+++ b/tokio/src/macros/pin.rs
@@ -97,6 +97,30 @@
/// }
/// }
/// ```
+///
+/// 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),*) => { $(
@@ -108,5 +132,13 @@ macro_rules! pin {
let mut $x = unsafe {
$crate::macros::support::Pin::new_unchecked(&mut $x)
};
- )* }
+ )* };
+ ($(
+ let $x:ident = $init:expr;
+ )*) => {
+ $(
+ let $x = $init;
+ crate::pin!($x);
+ )*
+ };
}