Skip to content

Commit

Permalink
implement FusedFuture on next
Browse files Browse the repository at this point in the history
  • Loading branch information
daprilik authored and udoprog committed Apr 28, 2022
1 parent 409078b commit e1c08d3
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -435,21 +435,22 @@ where
/// println!("done!");
/// }
/// ```
pub async fn next(&mut self) -> Option<<Self as PollNext>::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<T> Future for Next<'_, T>
where
T: Unpin + PollNext,
{
type Output = Option<T::Item>;
impl<T> Future for Next<'_, T>
where
T: Unpin + PollNext,
{
type Output = Option<T::Item>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut *self.0).poll_next(cx)
}
}
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut *self.0).poll_next(cx)
}
}

Expand Down Expand Up @@ -1083,4 +1084,13 @@ cfg_futures_rs! {
streams
}
}

impl<T> FusedFuture for Next<'_, T>
where
T: Unpin + PollNext + FusedStream,
{
fn is_terminated(&self) -> bool {
self.0.is_terminated()
}
}
}

0 comments on commit e1c08d3

Please # to comment.