mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-24 09:16:46 +00:00
Add samples for some of the functions
This commit is contained in:
parent
aabfefd015
commit
c4b9a7f680
4 changed files with 79 additions and 31 deletions
|
@ -37,10 +37,10 @@ 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::DoubleEndedStreamExt;
|
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
||||||
use async_std::stream;
|
|
||||||
|
|
||||||
let mut s = stream::from_iter(vec![1u8, 2, 3, 4, 5]);
|
let mut s = Sample::from(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));
|
||||||
|
@ -58,6 +58,27 @@ extension_trait! {
|
||||||
NthBackFuture::new(self, n)
|
NthBackFuture::new(self, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc = r#"
|
||||||
|
Returns the the frist element from the right that matches the predicate.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
Basic usage:
|
||||||
|
|
||||||
|
```
|
||||||
|
# fn main() { async_std::task::block_on(async {
|
||||||
|
#
|
||||||
|
use async_std::stream::Sample;
|
||||||
|
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
||||||
|
|
||||||
|
let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]);
|
||||||
|
|
||||||
|
let second = s.rfind(|v| v % 2 == 0).await;
|
||||||
|
assert_eq!(second, Some(4));
|
||||||
|
#
|
||||||
|
# }) }
|
||||||
|
```
|
||||||
|
"#]
|
||||||
fn rfind<P>(
|
fn rfind<P>(
|
||||||
&mut self,
|
&mut self,
|
||||||
p: P,
|
p: P,
|
||||||
|
@ -69,6 +90,26 @@ extension_trait! {
|
||||||
RFindFuture::new(self, p)
|
RFindFuture::new(self, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[doc = r#"
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
Basic usage:
|
||||||
|
|
||||||
|
```
|
||||||
|
# fn main() { async_std::task::block_on(async {
|
||||||
|
#
|
||||||
|
use async_std::stream::Sample;
|
||||||
|
use async_std::stream::double_ended::DoubleEndedStreamExt;
|
||||||
|
|
||||||
|
let s = Sample::from(vec![1, 2, 3, 4, 5]);
|
||||||
|
|
||||||
|
let second = s.rfold(0, |acc, v| v + acc).await;
|
||||||
|
|
||||||
|
assert_eq!(second, 15);
|
||||||
|
#
|
||||||
|
# }) }
|
||||||
|
```
|
||||||
|
"#]
|
||||||
fn rfold<B, F>(
|
fn rfold<B, F>(
|
||||||
self,
|
self,
|
||||||
accum: B,
|
accum: B,
|
||||||
|
|
|
@ -22,31 +22,3 @@ pub trait DoubleEndedStream: Stream {
|
||||||
/// [trait-level]: trait.DoubleEndedStream.html
|
/// [trait-level]: trait.DoubleEndedStream.html
|
||||||
fn poll_next_back(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
|
fn poll_next_back(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -307,8 +307,9 @@ 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(crate) mod stream;
|
pub mod stream;
|
||||||
|
|
||||||
mod empty;
|
mod empty;
|
||||||
mod from_fn;
|
mod from_fn;
|
||||||
|
@ -316,6 +317,7 @@ 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;
|
||||||
|
|
33
src/stream/sample.rs
Normal file
33
src/stream/sample.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
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