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

Smaller await #1159

Merged
merged 5 commits into from
Aug 2, 2018
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
45 changes: 24 additions & 21 deletions futures-util/src/async_await/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,21 @@ macro_rules! join {
let mut $fut = $crate::future::maybe_done($fut);
$crate::pin_mut!($fut);
)*
loop {
await!($crate::future::poll_fn(move |cx| {
let mut all_done = true;
$(
if $crate::poll!($fut.reborrow()).is_pending() {
if $crate::core_reexport::future::Future::poll($fut.reborrow(), cx).is_pending() {
all_done = false;
}
)*
if all_done {
break;
$crate::core_reexport::task::Poll::Ready(($(
$fut.reborrow().take_output().unwrap(),
)*))
} else {
$crate::pending!();
$crate::core_reexport::task::Poll::Pending
}
}

($(
$fut.reborrow().take_output().unwrap(),
)*)
}))
} }
}

Expand Down Expand Up @@ -94,30 +92,35 @@ macro_rules! try_join {
let mut $fut = $crate::future::maybe_done($fut);
$crate::pin_mut!($fut);
)*
let res: $crate::core_reexport::result::Result<(), _> = loop {

let res: $crate::core_reexport::result::Result<_, _> = await!($crate::future::poll_fn(move |cx| {
let mut all_done = true;
$(
if $crate::poll!($fut.reborrow()).is_pending() {
if $crate::core_reexport::future::Future::poll($fut.reborrow(), cx).is_pending() {
all_done = false;
} else if $fut.reborrow().output_mut().unwrap().is_err() {
// `.err().unwrap()` rather than `.unwrap_err()` so that we don't introduce
// a `T: Debug` bound.
break $crate::core_reexport::result::Result::Err(
$fut.reborrow().take_output().unwrap().err().unwrap()
return $crate::core_reexport::task::Poll::Ready(
$crate::core_reexport::result::Result::Err(
$fut.reborrow().take_output().unwrap().err().unwrap()
)
);
}
)*
if all_done {
break $crate::core_reexport::result::Result::Ok(());
$crate::core_reexport::task::Poll::Ready(
$crate::core_reexport::result::Result::Ok(($(
// `.ok().unwrap()` rather than `.unwrap()` so that we don't introduce
// an `E: Debug` bound.
$fut.reborrow().take_output().unwrap().ok().unwrap(),
)*))
)
} else {
$crate::pending!();
$crate::core_reexport::task::Poll::Pending
}
};
}));

res.map(|()| ($(
// `.ok().unwrap()` rather than `.unwrap()` so that we don't introduce
// an `E: Debug` bound.
$fut.reborrow().take_output().unwrap().ok().unwrap(),
)*))
res
Copy link
Contributor

@MajorBreakfast MajorBreakfast Aug 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's basically:

let res = await!(/* ... */);
res

You could give back the value from the expression above directly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's intentionally done this way in order to provide the : Result<_, _> annotation .

} }
}
1 change: 1 addition & 0 deletions futures-util/src/async_await/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ mod join;
mod select;

#[doc(hidden)]
#[inline(always)]
pub fn assert_unpin<T: Future + Unpin>(_: &T) {}
16 changes: 9 additions & 7 deletions futures-util/src/async_await/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ macro_rules! select {
)*
}

let __priv_res = loop {
let __priv_res = await!($crate::future::poll_fn(|cx| {
$(
let poll_res = $crate::poll!($crate::core_reexport::mem::PinMut::new(
&mut $name));
if let $crate::core_reexport::task::Poll::Ready(x) = poll_res {
break __PrivResult::$name(x);
match $crate::core_reexport::future::Future::poll(
$crate::core_reexport::mem::PinMut::new(&mut $name), cx)
{
$crate::core_reexport::task::Poll::Ready(x) =>
return $crate::core_reexport::task::Poll::Ready(__PrivResult::$name(x)),
$crate::core_reexport::task::Poll::Pending => {},
Copy link
Contributor

@MajorBreakfast MajorBreakfast Aug 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this branch is empty, you could use if let $crate::...::Poll::Ready(x) = poll {} instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally changed it because I think it's easier to read as a match since all of the paths included are so long.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have introduced a poll variable. But match is also fine

}
)*
$crate::pending!();
};
$crate::core_reexport::task::Poll::Pending
}));
match __priv_res {
$(
__PrivResult::$name($name) => {
Expand Down
55 changes: 54 additions & 1 deletion futures/tests/async_await_macros.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(async_await, await_macro, pin, arbitrary_self_types, futures_api)]

use futures::{Poll, pending, poll, pin_mut, join, select};
use futures::{Poll, future, pending, poll, pin_mut, join, try_join, select};
use futures::channel::oneshot;
use futures::executor::block_on;

Expand Down Expand Up @@ -75,3 +75,56 @@ fn select_can_move_uncompleted_futures() {
});
assert!(ran);
}

#[test]
fn select_size() {
let fut = async {
let mut ready = future::ready(0i32);
select! {
ready => {},
}
};
assert_eq!(::std::mem::size_of_val(&fut), 40);

let fut = async {
let mut ready1 = future::ready(0i32);
let mut ready2 = future::ready(0i32);
select! {
ready1 => {},
ready2 => {},
}
};
assert_eq!(::std::mem::size_of_val(&fut), 56);
}

#[test]
fn join_size() {
let fut = async {
let ready = future::ready(0i32);
join!(ready)
};
assert_eq!(::std::mem::size_of_val(&fut), 40);

let fut = async {
let ready1 = future::ready(0i32);
let ready2 = future::ready(0i32);
join!(ready1, ready2)
};
assert_eq!(::std::mem::size_of_val(&fut), 64);
}

#[test]
fn try_join_size() {
let fut = async {
let ready = future::ready(Ok::<i32, i32>(0));
try_join!(ready)
};
assert_eq!(::std::mem::size_of_val(&fut), 40);

let fut = async {
let ready1 = future::ready(Ok::<i32, i32>(0));
let ready2 = future::ready(Ok::<i32, i32>(0));
try_join!(ready1, ready2)
};
assert_eq!(::std::mem::size_of_val(&fut), 64);
}