From e1c08d3e61ab4a7b19e9927921f3acb082c2b88f Mon Sep 17 00:00:00 2001 From: Daniel Prilik Date: Wed, 27 Apr 2022 11:06:30 -0700 Subject: [PATCH] implement FusedFuture on next --- src/lib.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3d380af..1629a19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,7 +177,7 @@ use self::pin_slab::PinSlab; use self::wake_set::{SharedWakeSet, WakeSet}; use self::waker::SharedWaker; #[cfg(feature = "futures-rs")] -use futures_core::{FusedStream, Stream}; +use futures_core::{FusedFuture, FusedStream, Stream}; /// Our very own homemade `ready!` impl. macro_rules! ready { @@ -435,21 +435,22 @@ where /// println!("done!"); /// } /// ``` - pub async fn next(&mut self) -> Option<::Item> { - return Next(self).await; + pub fn next(&mut self) -> Next<'_, Self> { + Next(self) + } +} - struct Next<'a, T>(&'a mut T); +/// Future returned by [Unordered::next]. +pub struct Next<'a, T>(&'a mut T); - impl Future for Next<'_, T> - where - T: Unpin + PollNext, - { - type Output = Option; +impl Future for Next<'_, T> +where + T: Unpin + PollNext, +{ + type Output = Option; - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Pin::new(&mut *self.0).poll_next(cx) - } - } + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + Pin::new(&mut *self.0).poll_next(cx) } } @@ -1083,4 +1084,13 @@ cfg_futures_rs! { streams } } + + impl FusedFuture for Next<'_, T> + where + T: Unpin + PollNext + FusedStream, + { + fn is_terminated(&self) -> bool { + self.0.is_terminated() + } + } }