Merge pull request #446 from yjhmelody/stream-cloned

Add Stream cloned
poc-serde-support
Yoshua Wuyts 5 years ago committed by GitHub
commit 8a0e29473a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,32 @@
use crate::stream::Stream;
use crate::task::{Context, Poll};
use pin_project_lite::pin_project;
use std::pin::Pin;
pin_project! {
#[derive(Debug)]
pub struct Cloned<S> {
#[pin]
stream: S,
}
}
impl<S> Cloned<S> {
pub(super) fn new(stream: S) -> Self {
Self { stream }
}
}
impl<'a, S, T: 'a> Stream for Cloned<S>
where
S: Stream<Item = &'a T>,
T: Clone,
{
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
let next = futures_core::ready!(this.stream.poll_next(cx));
Poll::Ready(next.cloned())
}
}

@ -24,6 +24,7 @@
mod all; mod all;
mod any; mod any;
mod chain; mod chain;
mod cloned;
mod cmp; mod cmp;
mod copied; mod copied;
mod enumerate; mod enumerate;
@ -91,6 +92,7 @@ use try_fold::TryFoldFuture;
use try_for_each::TryForEachFuture; use try_for_each::TryForEachFuture;
pub use chain::Chain; pub use chain::Chain;
pub use cloned::Cloned;
pub use copied::Copied; pub use copied::Copied;
pub use filter::Filter; pub use filter::Filter;
pub use fuse::Fuse; pub use fuse::Fuse;
@ -379,6 +381,40 @@ extension_trait! {
Chain::new(self, other) Chain::new(self, other)
} }
#[doc = r#"
Creates an stream which copies all of its elements.
# Examples
Basic usage:
```
# fn main() { async_std::task::block_on(async {
#
use async_std::prelude::*;
use async_std::stream;
let v = stream::from_iter(vec![&1, &2, &3]);
let mut v_cloned = v.cloned();
assert_eq!(v_cloned.next().await, Some(1));
assert_eq!(v_cloned.next().await, Some(2));
assert_eq!(v_cloned.next().await, Some(3));
assert_eq!(v_cloned.next().await, None);
#
# }) }
```
"#]
fn cloned<'a,T>(self) -> Cloned<Self>
where
Self: Sized + Stream<Item = &'a T>,
T : 'a + Clone,
{
Cloned::new(self)
}
#[doc = r#" #[doc = r#"
Creates an stream which copies all of its elements. Creates an stream which copies all of its elements.
@ -394,8 +430,6 @@ extension_trait! {
use async_std::stream; use async_std::stream;
let s = stream::from_iter(vec![&1, &2, &3]); let s = stream::from_iter(vec![&1, &2, &3]);
let second = stream::from_iter(vec![2, 3]);
let mut s_copied = s.copied(); let mut s_copied = s.copied();
assert_eq!(s_copied.next().await, Some(1)); assert_eq!(s_copied.next().await, Some(1));

Loading…
Cancel
Save