Add Stream cloned

poc-serde-support
yjhmelody 5 years ago
parent fa91d7f856
commit 4942dc7f9f

@ -0,0 +1,33 @@
use crate::stream::Stream;
use crate::task::{Context, Poll};
use pin_project_lite::pin_project;
use std::pin::Pin;
pin_project! {
#[doc(hidden)]
#[allow(missing_debug_implementations)]
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;
@ -373,6 +375,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 std::collections::VecDeque;
let v: VecDeque<_> = vec![&1, &2, &3].into_iter().collect();
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.
@ -395,7 +431,6 @@ extension_trait! {
assert_eq!(v_copied.next().await, Some(2)); assert_eq!(v_copied.next().await, Some(2));
assert_eq!(v_copied.next().await, Some(3)); assert_eq!(v_copied.next().await, Some(3));
assert_eq!(v_copied.next().await, None); assert_eq!(v_copied.next().await, None);
# #
# }) } # }) }

Loading…
Cancel
Save