Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros: add assignment form to pin! (#2274) #5

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion tokio/src/macros/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),*) => { $(
Expand All @@ -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);
)*
};
}
15 changes: 15 additions & 0 deletions tokio/tests/macros_pin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use tokio::pin;

async fn one() {}
async fn two() {}

#[tokio::test]
async fn multi_pin() {
pin! {
let f1 = one();
let f2 = two();
}

(&mut f1).await;
(&mut f2).await;
}