forked from mirror/async-std
No need for a custom impl for FromIter for DoubleEndedStream
This commit is contained in:
parent
b0038e11be
commit
182fe6896f
3 changed files with 9 additions and 59 deletions
|
@ -1,57 +0,0 @@
|
||||||
use crate::stream::Stream;
|
|
||||||
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
use crate::stream::DoubleEndedStream;
|
|
||||||
|
|
||||||
/// A double-ended stream that was created from iterator.
|
|
||||||
///
|
|
||||||
/// This stream is created by the [`from_iter`] function.
|
|
||||||
/// See it documentation for more.
|
|
||||||
///
|
|
||||||
/// [`from_iter`]: fn.from_iter.html
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct FromIter<T> {
|
|
||||||
inner: Vec<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Converts an iterator into a double-ended stream.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # async_std::task::block_on(async {
|
|
||||||
/// #
|
|
||||||
/// use async_std::stream::double_ended_stream::{self, DoubleEndedStream};
|
|
||||||
///
|
|
||||||
/// let mut s = double_ended_stream::from_iter(vec![0, 1, 2, 3]);
|
|
||||||
///
|
|
||||||
/// assert_eq!(s.next_back().await, Some(3));
|
|
||||||
/// assert_eq!(s.next_back().await, Some(2));
|
|
||||||
/// assert_eq!(s.next_back().await, Some(1));
|
|
||||||
/// assert_eq!(s.next_back().await, Some(0));
|
|
||||||
/// assert_eq!(s.next_back().await, None);
|
|
||||||
/// #
|
|
||||||
/// # })
|
|
||||||
/// ```
|
|
||||||
pub fn from_iter<I: IntoIterator>(iter: I) -> FromIter<I::Item> {
|
|
||||||
FromIter { inner: iter.into_iter().collect() }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Unpin for FromIter<T> {}
|
|
||||||
|
|
||||||
impl<T> Stream for FromIter<T> {
|
|
||||||
type Item = T;
|
|
||||||
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
||||||
if self.inner.len() > 0 {
|
|
||||||
return Poll::Ready(Some(self.inner.remove(0)));
|
|
||||||
}
|
|
||||||
return Poll::Ready(None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> DoubleEndedStream for FromIter<T> {
|
|
||||||
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
||||||
Poll::Ready(self.inner.pop())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,14 +3,12 @@ use crate::stream::Stream;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
mod from_iter;
|
|
||||||
mod next_back;
|
mod next_back;
|
||||||
mod nth_back;
|
mod nth_back;
|
||||||
mod rfind;
|
mod rfind;
|
||||||
mod rfold;
|
mod rfold;
|
||||||
mod try_rfold;
|
mod try_rfold;
|
||||||
|
|
||||||
pub use from_iter::{from_iter, FromIter};
|
|
||||||
use next_back::NextBackFuture;
|
use next_back::NextBackFuture;
|
||||||
use nth_back::NthBackFuture;
|
use nth_back::NthBackFuture;
|
||||||
use rfind::RFindFuture;
|
use rfind::RFindFuture;
|
||||||
|
|
|
@ -3,6 +3,8 @@ use std::pin::Pin;
|
||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
|
|
||||||
use crate::stream::Stream;
|
use crate::stream::Stream;
|
||||||
|
#[cfg(feature = "unstable")]
|
||||||
|
use crate::stream::double_ended_stream::DoubleEndedStream;
|
||||||
use crate::task::{Context, Poll};
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
pin_project! {
|
pin_project! {
|
||||||
|
@ -51,3 +53,10 @@ impl<I: Iterator> Stream for FromIter<I> {
|
||||||
Poll::Ready(self.iter.next())
|
Poll::Ready(self.iter.next())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "unstable")]
|
||||||
|
impl<T: DoubleEndedIterator> DoubleEndedStream for FromIter<T> {
|
||||||
|
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<T::Item>> {
|
||||||
|
Poll::Ready(self.iter.next_back())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue