diff --git a/src/future/join/tuple.rs b/src/future/join/tuple.rs index 9c91660..1b1fee6 100644 --- a/src/future/join/tuple.rs +++ b/src/future/join/tuple.rs @@ -1,14 +1,85 @@ use super::Join as JoinTrait; -use crate::utils::MaybeDone; +use crate::utils::{self, PollState, PollStates}; use core::fmt::{self, Debug}; use core::future::{Future, IntoFuture}; +use core::mem::MaybeUninit; use core::pin::Pin; use core::task::{Context, Poll}; use pin_project::pin_project; -macro_rules! impl_merge_tuple { +macro_rules! maybe_poll { + ($idx:tt, $len:ident, $this:ident, $fut:ident, $cx:ident) => { + if $this.states[$idx].is_pending() { + if let Poll::Ready(out) = $this.$fut.poll($cx) { + $this.outputs.$idx = MaybeUninit::new(out); + $this.states[$idx] = PollState::Done; + *$this.len -= 1; + } + } + }; +} + +macro_rules! poll_all_pending { + (@inner 0, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(0, $len, $this, $fut, $cx); + poll_all_pending!(@inner 1, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 1, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(1, $len, $this, $fut, $cx); + poll_all_pending!(@inner 2, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 2, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(2, $len, $this, $fut, $cx); + poll_all_pending!(@inner 3, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 3, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(3, $len, $this, $fut, $cx); + poll_all_pending!(@inner 4, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 4, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(4, $len, $this, $fut, $cx); + poll_all_pending!(@inner 5, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 5, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(5, $len, $this, $fut, $cx); + poll_all_pending!(@inner 6, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 6, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(6, $len, $this, $fut, $cx); + poll_all_pending!(@inner 7, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 7, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(7, $len, $this, $fut, $cx); + poll_all_pending!(@inner 8, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 8, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(8, $len, $this, $fut, $cx); + poll_all_pending!(@inner 9, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 9, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(9, $len, $this, $fut, $cx); + poll_all_pending!(@inner 10, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 10, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(10, $len, $this, $fut, $cx); + poll_all_pending!(@inner 11, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 11, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(11, $len, $this, $fut, $cx); + poll_all_pending!(@inner 12, $len, $this, $cx, ($($rest,)*)); + }; + (@inner 12, $len:ident, $this:ident, $cx:ident, ($fut:ident, $($rest:ident,)*)) => { + maybe_poll!(12, $len, $this, $fut, $cx); + }; + (@inner $ignore:literal, $len:ident, $this:ident, $cx:ident, ()) => { }; + ($len:ident, $this:ident, $cx:ident, $($F:ident,)*) => { + poll_all_pending!(@inner 0, $len, $this, $cx, ($($F,)*)); + }; +} + +macro_rules! impl_join_tuple { ($StructName:ident $($F:ident)*) => { /// Waits for two similarly-typed futures to complete. /// @@ -21,8 +92,10 @@ macro_rules! impl_merge_tuple { #[must_use = "futures do nothing unless you `.await` or poll them"] #[allow(non_snake_case)] pub struct $StructName<$($F: Future),*> { - done: bool, - $(#[pin] $F: MaybeDone<$F>,)* + len: u32, + $(#[pin] $F: $F,)* + outputs: ($(MaybeUninit<$F::Output>,)*), + states: PollStates, } impl<$($F),*> Debug for $StructName<$($F),*> @@ -32,7 +105,8 @@ macro_rules! impl_merge_tuple { )* { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Join") - $(.field(&self.$F))* + .field(&($(&self.$F,)*)) + .field(&self.states) .finish() } } @@ -41,20 +115,18 @@ macro_rules! impl_merge_tuple { #[allow(unused_parens)] #[allow(unused_variables)] impl<$($F: Future),*> Future for $StructName<$($F),*> { - type Output = ($($F::Output),*); + type Output = ($($F::Output,)*); fn poll( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll { - let mut all_done = true; let mut this = self.project(); - assert!(!*this.done, "Futures must not be polled after completing"); - $(all_done &= this.$F.as_mut().poll(cx).is_ready();)* + poll_all_pending!(LEN, this, cx, $($F,)*); - if all_done { - *this.done = true; - Poll::Ready(($(this.$F.take().unwrap()),*)) + if *this.len <= 0 { + let out = unsafe {(this.outputs as *const _ as *const ($($F::Output,)*)).read()}; + Poll::Ready(out) } else { Poll::Pending } @@ -66,33 +138,36 @@ macro_rules! impl_merge_tuple { where $( $F: IntoFuture, )* { - type Output = ($($F::Output),*); + type Output = ($($F::Output,)*); type Future = $StructName<$($F::IntoFuture),*>; fn join(self) -> Self::Future { let ($($F,)*): ($($F,)*) = self; + const LEN: u32 = utils::tuple_len!($($F,)*); $StructName { - done: false, - $($F: MaybeDone::new($F.into_future())),* + len: LEN, + $($F: $F.into_future(),)* + outputs: ($(MaybeUninit::<$F::Output>::uninit(),)*), + states: PollStates::new(LEN as usize), } } } }; } -impl_merge_tuple! { Join0 } -impl_merge_tuple! { Join1 A } -impl_merge_tuple! { Join2 A B } -impl_merge_tuple! { Join3 A B C } -impl_merge_tuple! { Join4 A B C D } -impl_merge_tuple! { Join5 A B C D E } -impl_merge_tuple! { Join6 A B C D E F } -impl_merge_tuple! { Join7 A B C D E F G } -impl_merge_tuple! { Join8 A B C D E F G H } -impl_merge_tuple! { Join9 A B C D E F G H I } -impl_merge_tuple! { Join10 A B C D E F G H I J } -impl_merge_tuple! { Join11 A B C D E F G H I J K } -impl_merge_tuple! { Join12 A B C D E F G H I J K L } +impl_join_tuple! { Join0 } +impl_join_tuple! { Join1 A } +impl_join_tuple! { Join2 A B } +impl_join_tuple! { Join3 A B C } +impl_join_tuple! { Join4 A B C D } +impl_join_tuple! { Join5 A B C D E } +impl_join_tuple! { Join6 A B C D E F } +impl_join_tuple! { Join7 A B C D E F G } +impl_join_tuple! { Join8 A B C D E F G H } +impl_join_tuple! { Join9 A B C D E F G H I } +impl_join_tuple! { Join10 A B C D E F G H I J } +impl_join_tuple! { Join11 A B C D E F G H I J K } +impl_join_tuple! { Join12 A B C D E F G H I J K L } #[cfg(test)] mod test { @@ -110,7 +185,7 @@ mod test { fn join_1() { futures_lite::future::block_on(async { let a = future::ready("hello"); - assert_eq!((a,).join().await, ("hello")); + assert_eq!((a,).join().await, ("hello",)); }); } diff --git a/src/utils/poll_state.rs b/src/utils/poll_state.rs index e9497f4..0aeadab 100644 --- a/src/utils/poll_state.rs +++ b/src/utils/poll_state.rs @@ -69,6 +69,19 @@ pub(crate) enum PollStates { Boxed(Box<[PollState]>), } +impl core::fmt::Debug for PollStates { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Inline(len, states) => f + // .debug_tuple("Inline") + .debug_list() + .entries(&states[..(*len as usize)]) + .finish(), + Self::Boxed(states) => f.debug_list().entries(&**states).finish(), + } + } +} + impl PollStates { pub(crate) fn new(len: usize) -> Self { assert!(MAX_INLINE_ENTRIES <= u8::MAX as usize); diff --git a/src/utils/tuple.rs b/src/utils/tuple.rs index 5e8dc4e..d50b1d5 100644 --- a/src/utils/tuple.rs +++ b/src/utils/tuple.rs @@ -1,6 +1,10 @@ /// Compute the number of permutations for a number /// during compilation. pub(crate) const fn permutations(mut num: u32) -> u32 { + if num == 0 { + return 0; + } + let mut total = 1; loop { total *= num;