mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-23 16:56:46 +00:00
Replace sample with a hidden from_iter implementation for double-ended-stream
This commit is contained in:
parent
abd360893c
commit
892c6008c2
4 changed files with 50 additions and 50 deletions
38
src/stream/double_ended/from_iter.rs
Normal file
38
src/stream/double_ended/from_iter.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
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>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,12 +3,14 @@ mod nth_back;
|
||||||
mod rfind;
|
mod rfind;
|
||||||
mod rfold;
|
mod rfold;
|
||||||
mod try_rfold;
|
mod try_rfold;
|
||||||
|
mod from_iter;
|
||||||
|
|
||||||
use next_back::NextBackFuture;
|
use next_back::NextBackFuture;
|
||||||
use nth_back::NthBackFuture;
|
use nth_back::NthBackFuture;
|
||||||
use rfind::RFindFuture;
|
use rfind::RFindFuture;
|
||||||
use rfold::RFoldFuture;
|
use rfold::RFoldFuture;
|
||||||
use try_rfold::TryRFoldFuture;
|
use try_rfold::TryRFoldFuture;
|
||||||
|
pub use from_iter::{from_iter, FromIter};
|
||||||
|
|
||||||
extension_trait! {
|
extension_trait! {
|
||||||
use crate::stream::Stream;
|
use crate::stream::Stream;
|
||||||
|
@ -113,10 +115,9 @@ extension_trait! {
|
||||||
```
|
```
|
||||||
# fn main() { async_std::task::block_on(async {
|
# fn main() { async_std::task::block_on(async {
|
||||||
#
|
#
|
||||||
use async_std::stream::Sample;
|
use async_std::stream::double_ended::{self, DoubleEndedStreamExt};
|
||||||
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
|
||||||
|
|
||||||
let mut s = Sample::from(vec![7u8]);
|
let mut s = double_ended::from_iter(vec![7u8]);
|
||||||
|
|
||||||
assert_eq!(s.next_back().await, Some(7));
|
assert_eq!(s.next_back().await, Some(7));
|
||||||
assert_eq!(s.next_back().await, None);
|
assert_eq!(s.next_back().await, None);
|
||||||
|
@ -141,10 +142,9 @@ extension_trait! {
|
||||||
```
|
```
|
||||||
# fn main() { async_std::task::block_on(async {
|
# fn main() { async_std::task::block_on(async {
|
||||||
#
|
#
|
||||||
use async_std::stream::Sample;
|
use async_std::stream::double_ended::{self, DoubleEndedStreamExt};
|
||||||
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
|
||||||
|
|
||||||
let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]);
|
let mut s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]);
|
||||||
|
|
||||||
let second = s.nth_back(1).await;
|
let second = s.nth_back(1).await;
|
||||||
assert_eq!(second, Some(4));
|
assert_eq!(second, Some(4));
|
||||||
|
@ -172,10 +172,9 @@ extension_trait! {
|
||||||
```
|
```
|
||||||
# fn main() { async_std::task::block_on(async {
|
# fn main() { async_std::task::block_on(async {
|
||||||
#
|
#
|
||||||
use async_std::stream::Sample;
|
use async_std::stream::double_ended::{self, DoubleEndedStreamExt};
|
||||||
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
|
||||||
|
|
||||||
let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]);
|
let mut s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]);
|
||||||
|
|
||||||
let second = s.rfind(|v| v % 2 == 0).await;
|
let second = s.rfind(|v| v % 2 == 0).await;
|
||||||
assert_eq!(second, Some(4));
|
assert_eq!(second, Some(4));
|
||||||
|
@ -202,10 +201,9 @@ extension_trait! {
|
||||||
```
|
```
|
||||||
# fn main() { async_std::task::block_on(async {
|
# fn main() { async_std::task::block_on(async {
|
||||||
#
|
#
|
||||||
use async_std::stream::Sample;
|
use async_std::stream::double_ended::{self, DoubleEndedStreamExt};
|
||||||
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
|
||||||
|
|
||||||
let s = Sample::from(vec![1, 2, 3, 4, 5]);
|
let s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]);
|
||||||
|
|
||||||
let second = s.rfold(0, |acc, v| v + acc).await;
|
let second = s.rfold(0, |acc, v| v + acc).await;
|
||||||
|
|
||||||
|
@ -237,10 +235,9 @@ extension_trait! {
|
||||||
```
|
```
|
||||||
# fn main() { async_std::task::block_on(async {
|
# fn main() { async_std::task::block_on(async {
|
||||||
#
|
#
|
||||||
use async_std::stream::Sample;
|
use async_std::stream::double_ended::{self, DoubleEndedStreamExt};
|
||||||
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
|
||||||
|
|
||||||
let s = Sample::from(vec![1, 2, 3, 4, 5]);
|
let s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]);
|
||||||
let sum = s.try_rfold(0, |acc, v| {
|
let sum = s.try_rfold(0, |acc, v| {
|
||||||
if (acc+v) % 2 == 1 {
|
if (acc+v) % 2 == 1 {
|
||||||
Ok(v+3)
|
Ok(v+3)
|
||||||
|
|
|
@ -307,7 +307,6 @@ pub use once::{once, Once};
|
||||||
pub use repeat::{repeat, Repeat};
|
pub use repeat::{repeat, Repeat};
|
||||||
pub use repeat_with::{repeat_with, RepeatWith};
|
pub use repeat_with::{repeat_with, RepeatWith};
|
||||||
pub use stream::*;
|
pub use stream::*;
|
||||||
pub use crate::stream::sample::Sample;
|
|
||||||
|
|
||||||
pub mod stream;
|
pub mod stream;
|
||||||
|
|
||||||
|
@ -317,7 +316,6 @@ mod from_iter;
|
||||||
mod once;
|
mod once;
|
||||||
mod repeat;
|
mod repeat;
|
||||||
mod repeat_with;
|
mod repeat_with;
|
||||||
mod sample;
|
|
||||||
|
|
||||||
cfg_unstable! {
|
cfg_unstable! {
|
||||||
pub mod double_ended;
|
pub mod double_ended;
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
use crate::stream::Stream;
|
|
||||||
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
use crate::stream::DoubleEndedStream;
|
|
||||||
|
|
||||||
pub struct Sample<T> {
|
|
||||||
inner: Vec<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> From<Vec<T>> for Sample<T> {
|
|
||||||
fn from(data: Vec<T>) -> Self {
|
|
||||||
Sample { inner: data }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Unpin for Sample<T> {}
|
|
||||||
|
|
||||||
impl<T> Stream for Sample<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 Sample<T> {
|
|
||||||
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
||||||
Poll::Ready(self.inner.pop())
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue