forked from mirror/async-std
split stream into multiple files (#150)
* split stream into multiple files Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com> * cargo fmt Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>staging
parent
be71ac9d76
commit
ba43a05d01
@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue