Skip to content

split stream into multiple files #150

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

Merged
2 commits merged into from
Sep 8, 2019
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
1 change: 0 additions & 1 deletion src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub use repeat::{repeat, Repeat};
pub use stream::{Stream, Take};

mod empty;
mod min_by;
mod once;
mod repeat;
mod stream;
52 changes: 52 additions & 0 deletions src/stream/stream/all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::future::Future;
use crate::task::{Context, Poll};

use std::marker::PhantomData;
use std::pin::Pin;

#[derive(Debug)]
pub struct AllFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pub(crate) stream: &'a mut S,
pub(crate) f: F,
pub(crate) result: bool,
pub(crate) __item: PhantomData<T>,
}

impl<'a, S, F, T> AllFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pin_utils::unsafe_pinned!(stream: &'a mut S);
pin_utils::unsafe_unpinned!(result: bool);
pin_utils::unsafe_unpinned!(f: F);
}

impl<S, F> Future for AllFuture<'_, S, F, S::Item>
where
S: futures_core::stream::Stream + Unpin + Sized,
F: FnMut(S::Item) -> bool,
{
type Output = bool;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures_core::stream::Stream;
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(v) => {
let result = (self.as_mut().f())(v);
*self.as_mut().result() = result;
if result {
// don't forget to wake this task again to pull the next item from stream
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(false)
}
}
None => Poll::Ready(self.result),
}
}
}
52 changes: 52 additions & 0 deletions src/stream/stream/any.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::future::Future;
use crate::task::{Context, Poll};

use std::marker::PhantomData;
use std::pin::Pin;

#[derive(Debug)]
pub struct AnyFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pub(crate) stream: &'a mut S,
pub(crate) f: F,
pub(crate) result: bool,
pub(crate) __item: PhantomData<T>,
}

impl<'a, S, F, T> AnyFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pin_utils::unsafe_pinned!(stream: &'a mut S);
pin_utils::unsafe_unpinned!(result: bool);
pin_utils::unsafe_unpinned!(f: F);
}

impl<S, F> Future for AnyFuture<'_, S, F, S::Item>
where
S: futures_core::stream::Stream + Unpin + Sized,
F: FnMut(S::Item) -> bool,
{
type Output = bool;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures_core::stream::Stream;
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(v) => {
let result = (self.as_mut().f())(v);
*self.as_mut().result() = result;
if result {
Poll::Ready(true)
} else {
// don't forget to wake this task again to pull the next item from stream
cx.waker().wake_by_ref();
Poll::Pending
}
}
None => Poll::Ready(self.result),
}
}
}
12 changes: 6 additions & 6 deletions src/stream/min_by.rs → src/stream/stream/min_by.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
use std::cmp::Ordering;
use std::pin::Pin;

use super::stream::Stream;
use crate::future::Future;
use crate::stream::Stream;
use crate::task::{Context, Poll};

/// A future that yields the minimum item in a stream by a given comparison function.
#[derive(Clone, Debug)]
pub struct MinBy<S: Stream, F> {
pub struct MinByFuture<S: Stream, F> {
stream: S,
compare: F,
min: Option<S::Item>,
}

impl<S: Stream + Unpin, F> Unpin for MinBy<S, F> {}
impl<S: Stream + Unpin, F> Unpin for MinByFuture<S, F> {}

impl<S: Stream + Unpin, F> MinBy<S, F> {
impl<S: Stream + Unpin, F> MinByFuture<S, F> {
pub(super) fn new(stream: S, compare: F) -> Self {
MinBy {
MinByFuture {
stream,
compare,
min: None,
}
}
}

impl<S, F> Future for MinBy<S, F>
impl<S, F> Future for MinByFuture<S, F>
where
S: futures_core::stream::Stream + Unpin,
S::Item: Copy,
Expand Down
163 changes: 16 additions & 147 deletions src/stream/stream.rs → src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@
//! # }) }
//! ```

use std::cmp::Ordering;
use std::pin::Pin;
mod all;
mod any;
mod min_by;
mod next;
mod take;

use cfg_if::cfg_if;
pub use take::Take;

use all::AllFuture;
use any::AnyFuture;
use min_by::MinByFuture;
use next::NextFuture;

use super::min_by::MinBy;
use crate::future::Future;
use crate::task::{Context, Poll};
use std::cmp::Ordering;
use std::marker::PhantomData;

use cfg_if::cfg_if;

cfg_if! {
if #[cfg(feature = "docs")] {
#[doc(hidden)]
Expand Down Expand Up @@ -145,12 +153,12 @@ pub trait Stream {
/// #
/// # }) }
/// ```
fn min_by<F>(self, compare: F) -> MinBy<Self, F>
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F>
where
Self: Sized + Unpin,
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
MinBy::new(self, compare)
MinByFuture::new(self, compare)
}

/// Tests if every element of the stream matches a predicate.
Expand Down Expand Up @@ -278,142 +286,3 @@ impl<T: futures_core::stream::Stream + Unpin + ?Sized> Stream for T {
NextFuture { stream: self }
}
}

#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct NextFuture<'a, T: Unpin + ?Sized> {
stream: &'a mut T,
}

impl<T: futures_core::stream::Stream + Unpin + ?Sized> Future for NextFuture<'_, T> {
type Output = Option<T::Item>;

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

/// A stream that yields the first `n` items of another stream.
#[derive(Clone, Debug)]
pub struct Take<S> {
stream: S,
remaining: usize,
}

impl<S: Unpin> Unpin for Take<S> {}

impl<S: futures_core::stream::Stream> Take<S> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(remaining: usize);
}

impl<S: futures_core::stream::Stream> futures_core::stream::Stream for Take<S> {
type Item = S::Item;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
if self.remaining == 0 {
Poll::Ready(None)
} else {
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(_) => *self.as_mut().remaining() -= 1,
None => *self.as_mut().remaining() = 0,
}
Poll::Ready(next)
}
}
}

#[derive(Debug)]
pub struct AllFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
stream: &'a mut S,
f: F,
result: bool,
__item: PhantomData<T>,
}

impl<'a, S, F, T> AllFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pin_utils::unsafe_pinned!(stream: &'a mut S);
pin_utils::unsafe_unpinned!(result: bool);
pin_utils::unsafe_unpinned!(f: F);
}

impl<S, F> Future for AllFuture<'_, S, F, S::Item>
where
S: futures_core::stream::Stream + Unpin + Sized,
F: FnMut(S::Item) -> bool,
{
type Output = bool;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures_core::stream::Stream;
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(v) => {
let result = (self.as_mut().f())(v);
*self.as_mut().result() = result;
if result {
// don't forget to wake this task again to pull the next item from stream
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(false)
}
}
None => Poll::Ready(self.result),
}
}
}

#[derive(Debug)]
pub struct AnyFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
stream: &'a mut S,
f: F,
result: bool,
__item: PhantomData<T>,
}

impl<'a, S, F, T> AnyFuture<'a, S, F, T>
where
F: FnMut(T) -> bool,
{
pin_utils::unsafe_pinned!(stream: &'a mut S);
pin_utils::unsafe_unpinned!(result: bool);
pin_utils::unsafe_unpinned!(f: F);
}

impl<S, F> Future for AnyFuture<'_, S, F, S::Item>
where
S: futures_core::stream::Stream + Unpin + Sized,
F: FnMut(S::Item) -> bool,
{
type Output = bool;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use futures_core::stream::Stream;
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(v) => {
let result = (self.as_mut().f())(v);
*self.as_mut().result() = result;
if result {
Poll::Ready(true)
} else {
// don't forget to wake this task again to pull the next item from stream
cx.waker().wake_by_ref();
Poll::Pending
}
}
None => Poll::Ready(self.result),
}
}
}
17 changes: 17 additions & 0 deletions src/stream/stream/next.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::future::Future;
use crate::task::{Context, Poll};
use std::pin::Pin;

#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct NextFuture<'a, T: Unpin + ?Sized> {
pub(crate) stream: &'a mut T,
}

impl<T: futures_core::stream::Stream + Unpin + ?Sized> Future for NextFuture<'_, T> {
type Output = Option<T::Item>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut *self.stream).poll_next(cx)
}
}
34 changes: 34 additions & 0 deletions src/stream/stream/take.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::task::{Context, Poll};

use std::pin::Pin;

/// A stream that yields the first `n` items of another stream.
#[derive(Clone, Debug)]
pub struct Take<S> {
pub(crate) stream: S,
pub(crate) remaining: usize,
}

impl<S: Unpin> Unpin for Take<S> {}

impl<S: futures_core::stream::Stream> Take<S> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(remaining: usize);
}

impl<S: futures_core::stream::Stream> futures_core::stream::Stream for Take<S> {
type Item = S::Item;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
if self.remaining == 0 {
Poll::Ready(None)
} else {
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next {
Some(_) => *self.as_mut().remaining() -= 1,
None => *self.as_mut().remaining() = 0,
}
Poll::Ready(next)
}
}
}