Fixed `flatten`

master
Oleg Nosov 5 years ago
parent 85c32ef9d2
commit 32068942a6
No known key found for this signature in database
GPG Key ID: DE90B83800644E24

@ -69,4 +69,4 @@ where
} }
} }
} }
} }

@ -1,5 +1,5 @@
use std::fmt; use core::fmt;
use std::pin::Pin; use core::pin::Pin;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
@ -52,14 +52,21 @@ where
let mut this = self.project(); let mut this = self.project();
loop { loop {
if let Some(inner) = this.inner_stream.as_mut().as_pin_mut() { if let Some(inner) = this.inner_stream.as_mut().as_pin_mut() {
if let item @ Some(_) = futures_core::ready!(inner.poll_next(cx)) { let next_item = futures_core::ready!(inner.poll_next(cx));
return Poll::Ready(item);
if next_item.is_some() {
return Poll::Ready(next_item);
} else {
this.inner_stream.set(None);
} }
} }
match futures_core::ready!(this.stream.as_mut().poll_next(cx)) { let inner = futures_core::ready!(this.stream.as_mut().poll_next(cx));
None => return Poll::Ready(None),
Some(inner) => this.inner_stream.set(Some(inner.into_stream())), if inner.is_some() {
this.inner_stream.set(inner.map(IntoStream::into_stream));
} else {
return Poll::Ready(None);
} }
} }
} }

@ -1,3 +1,5 @@
use std::convert::identity;
use std::marker::Unpin;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
@ -99,58 +101,52 @@ fn merge_works_with_unfused_streams() {
assert_eq!(xs, vec![92, 92]); assert_eq!(xs, vec![92, 92]);
} }
#[test] struct S<T>(T);
fn flat_map_doesnt_poll_completed_inner_stream() {
async_std::task::block_on(async {
use async_std::prelude::*;
use async_std::task::*;
use std::convert::identity;
use std::marker::Unpin;
use std::pin::Pin;
struct S<T>(T); impl<T: Stream + Unpin> Stream for S<T> {
type Item = T::Item;
impl<T: Stream + Unpin> Stream for S<T> { fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
type Item = T::Item; unsafe { Pin::new_unchecked(&mut self.0) }.poll_next(ctx)
}
}
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> { struct StrictOnce {
unsafe { Pin::new_unchecked(&mut self.0) }.poll_next(ctx) polled: bool,
} }
}
struct StrictOnce { impl Stream for StrictOnce {
polled: bool, type Item = ();
};
impl Stream for StrictOnce { fn poll_next(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Option<Self::Item>> {
type Item = (); assert!(!self.polled, "Polled after completion!");
self.polled = true;
Poll::Ready(None)
}
}
fn poll_next(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Option<Self::Item>> { struct Interchanger {
assert!(!self.polled, "Polled after completion!"); polled: bool,
self.polled = true; }
Poll::Ready(None)
}
}
struct Interchanger { impl Stream for Interchanger {
polled: bool, type Item = S<Box<dyn Stream<Item = ()> + Unpin>>;
};
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
impl Stream for Interchanger { if self.polled {
type Item = S<Box<dyn Stream<Item = ()> + Unpin>>; self.polled = false;
ctx.waker().wake_by_ref();
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> { Poll::Pending
if self.polled { } else {
self.polled = false; self.polled = true;
ctx.waker().wake_by_ref(); Poll::Ready(Some(S(Box::new(StrictOnce { polled: false }))))
Poll::Pending
} else {
self.polled = true;
Poll::Ready(Some(S(Box::new(StrictOnce { polled: false }))))
}
}
} }
}
}
#[test]
fn flat_map_doesnt_poll_completed_inner_stream() {
task::block_on(async {
assert_eq!( assert_eq!(
Interchanger { polled: false } Interchanger { polled: false }
.take(2) .take(2)
@ -161,3 +157,17 @@ fn flat_map_doesnt_poll_completed_inner_stream() {
); );
}); });
} }
#[test]
fn flatten_doesnt_poll_completed_inner_stream() {
task::block_on(async {
assert_eq!(
Interchanger { polled: false }
.take(2)
.flatten()
.count()
.await,
0
);
});
}

Loading…
Cancel
Save