From dea1b67670d79ac8f6ef692c514a62c1c4be7229 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 24 Oct 2019 10:16:41 -0500 Subject: [PATCH 01/18] Skeleton cycle --- src/stream/stream/cycle.rs | 20 ++++++++++++++++++++ src/stream/stream/mod.rs | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/stream/stream/cycle.rs diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs new file mode 100644 index 00000000..881377f2 --- /dev/null +++ b/src/stream/stream/cycle.rs @@ -0,0 +1,20 @@ +use std::pin::Pin; + +use pin_project_lite::pin_project; + +use crate::stream::Stream; +use crate::task::{Context, Poll}; + +/// A stream that will repeatedly yield the same list of elements +pub struct Cycle { + source: Vec, + index: usize, +} + +impl Stream for Cycle { + type Item = T; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + Poll::Pending + } +} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index e182c033..4a845f11 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -24,9 +24,10 @@ mod all; mod any; mod chain; -mod cmp; -mod copied; mod enumerate; +mod cycle; +mod copied; +mod cmp; mod eq; mod filter; mod filter_map; From a096d5ec2dc7845fb05b09486342fe35dd46bfae Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 24 Oct 2019 10:21:42 -0500 Subject: [PATCH 02/18] stub out an example --- src/stream/stream/cycle.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index 881377f2..7ed0774e 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -18,3 +18,21 @@ impl Stream for Cycle { Poll::Pending } } + +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// +/// let values = vec![1,2,3]; +/// +/// # Ok(()) }) } +///``` +fn cycle(values: Vec) -> impl Stream { + Cycle { + source: values, + index: 0, + } +} From 486f9a964ca8d151b01ffa35a44316d8a265ddcd Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 24 Oct 2019 10:38:04 -0500 Subject: [PATCH 03/18] Cycle over a known set of values. --- src/stream/cycle.rs | 62 ++++++++++++++++++++++++++++++++++++++ src/stream/mod.rs | 2 ++ src/stream/stream/cycle.rs | 38 ----------------------- src/stream/stream/mod.rs | 3 +- 4 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 src/stream/cycle.rs delete mode 100644 src/stream/stream/cycle.rs diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs new file mode 100644 index 00000000..94384693 --- /dev/null +++ b/src/stream/cycle.rs @@ -0,0 +1,62 @@ +use std::pin::Pin; + +use pin_project_lite::pin_project; + +use crate::stream::Stream; +use crate::task::{Context, Poll}; + +pin_project! { + /// A stream that will repeatedly yield the same list of elements + pub struct Cycle { + source: Vec, + index: usize, + len: usize, + } +} + +impl Stream for Cycle { + type Item = T; + + fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + let value = self.source[self.index]; + + let next = self.index + 1; + + if next >= self.len { + self.as_mut().index = 0; + } else { + self.as_mut().index = next; + } + + Poll::Ready(Some(value)) + } +} + +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// # async_std::task::block_on(async { +/// # +/// use async_std::prelude::*; +/// use async_std::stream; +/// +/// let mut s = stream::cycle(vec![1,2,3]); +/// +/// assert_eq!(s.next().await, Some(1)); +/// assert_eq!(s.next().await, Some(2)); +/// assert_eq!(s.next().await, Some(3)); +/// assert_eq!(s.next().await, Some(1)); +/// assert_eq!(s.next().await, Some(2)); +/// # +/// # }) +/// ``` +pub fn cycle(source: Vec) -> impl Stream { + let len = source.len(); + Cycle { + source, + index: 0, + len, + } +} diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 07eecf28..449c484e 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -300,6 +300,7 @@ //! [`take`]: trait.Stream.html#method.take //! [`min`]: trait.Stream.html#method.min +pub use cycle::{cycle, Cycle}; pub use empty::{empty, Empty}; pub use from_fn::{from_fn, FromFn}; pub use from_iter::{from_iter, FromIter}; @@ -312,6 +313,7 @@ pub use stream::{ pub(crate) mod stream; +mod cycle; mod empty; mod from_fn; mod from_iter; diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs deleted file mode 100644 index 7ed0774e..00000000 --- a/src/stream/stream/cycle.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::pin::Pin; - -use pin_project_lite::pin_project; - -use crate::stream::Stream; -use crate::task::{Context, Poll}; - -/// A stream that will repeatedly yield the same list of elements -pub struct Cycle { - source: Vec, - index: usize, -} - -impl Stream for Cycle { - type Item = T; - - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - Poll::Pending - } -} - -/// # Examples -/// -/// Basic usage: -/// -/// ``` -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { -/// -/// let values = vec![1,2,3]; -/// -/// # Ok(()) }) } -///``` -fn cycle(values: Vec) -> impl Stream { - Cycle { - source: values, - index: 0, - } -} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 4a845f11..d46ae879 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -24,10 +24,11 @@ mod all; mod any; mod chain; -mod enumerate; +mod cmp; mod cycle; mod copied; mod cmp; +mod enumerate; mod eq; mod filter; mod filter_map; From 8126bb18821dd9be167ee892b9db8afa1e2d19b9 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 24 Oct 2019 16:40:27 -0500 Subject: [PATCH 04/18] use the module operator to calculate next index --- src/stream/cycle.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs index 94384693..135a4307 100644 --- a/src/stream/cycle.rs +++ b/src/stream/cycle.rs @@ -22,11 +22,7 @@ impl Stream for Cycle { let next = self.index + 1; - if next >= self.len { - self.as_mut().index = 0; - } else { - self.as_mut().index = next; - } + self.as_mut().index = next % self.len; Poll::Ready(Some(value)) } From e1ba87e7c167dcb88a2c8cae4e6e2c73f6ea48c3 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 24 Oct 2019 16:42:03 -0500 Subject: [PATCH 05/18] Add slightly better docs --- src/stream/cycle.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs index 135a4307..b9dd87f9 100644 --- a/src/stream/cycle.rs +++ b/src/stream/cycle.rs @@ -28,6 +28,8 @@ impl Stream for Cycle { } } +/// Creats a stream that yields the provided values infinitely and in order. +/// /// # Examples /// /// Basic usage: From 83ff11ff4c4b17ffada9317a61272b53420c9b81 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Sat, 26 Oct 2019 03:12:06 -0500 Subject: [PATCH 06/18] Switch cycle to stream --- src/stream/cycle.rs | 67 ++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs index b9dd87f9..1d16f855 100644 --- a/src/stream/cycle.rs +++ b/src/stream/cycle.rs @@ -7,24 +7,53 @@ use crate::task::{Context, Poll}; pin_project! { /// A stream that will repeatedly yield the same list of elements - pub struct Cycle { - source: Vec, + pub struct Cycle { + #[pin] + source: S, index: usize, - len: usize, + buffer: Vec, + state: CycleState, } } -impl Stream for Cycle { - type Item = T; +#[derive(Eq, PartialEq)] +enum CycleState { + FromStream, + FromBuffer, +} - fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - let value = self.source[self.index]; +impl Stream for Cycle + where + S: Stream, + T: Copy, +{ - let next = self.index + 1; + type Item = S::Item; - self.as_mut().index = next % self.len; + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let this = self.project(); - Poll::Ready(Some(value)) + let mut next; + if CycleState::FromStream == *this.state { + next = futures_core::ready!(this.source.poll_next(cx)); + + if let Some(val) = next { + this.buffer.push(val); + } else { + *this.state = CycleState::FromBuffer; + next = Some(this.buffer[*this.index]); + } + } else { + let mut index = *this.index; + if index == this.buffer.len() { + index = 0 + } + next = Some(this.buffer[index]); + + *this.index = index + 1; + } + + Poll::Ready(next) } } @@ -40,21 +69,21 @@ impl Stream for Cycle { /// use async_std::prelude::*; /// use async_std::stream; /// -/// let mut s = stream::cycle(vec![1,2,3]); +/// let mut s = stream::cycle(stream::once(7)); /// -/// assert_eq!(s.next().await, Some(1)); -/// assert_eq!(s.next().await, Some(2)); -/// assert_eq!(s.next().await, Some(3)); -/// assert_eq!(s.next().await, Some(1)); -/// assert_eq!(s.next().await, Some(2)); +/// assert_eq!(s.next().await, Some(7)); +/// assert_eq!(s.next().await, Some(7)); +/// assert_eq!(s.next().await, Some(7)); +/// assert_eq!(s.next().await, Some(7)); +/// assert_eq!(s.next().await, Some(7)); /// # /// # }) /// ``` -pub fn cycle(source: Vec) -> impl Stream { - let len = source.len(); +pub fn cycle, T: Copy>(source: S) -> impl Stream { Cycle { source, index: 0, - len, + buffer: Vec::new(), + state: CycleState::FromStream, } } From 171cc82aed62e5d3f2918f7a1637203fb239dc04 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 29 Oct 2019 08:37:16 -0500 Subject: [PATCH 07/18] Replace copy with clone bound --- src/stream/cycle.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs index 1d16f855..92f31ec1 100644 --- a/src/stream/cycle.rs +++ b/src/stream/cycle.rs @@ -25,7 +25,8 @@ enum CycleState { impl Stream for Cycle where S: Stream, - T: Copy, + T: Clone, + { type Item = S::Item; @@ -34,21 +35,22 @@ impl Stream for Cycle let this = self.project(); let mut next; - if CycleState::FromStream == *this.state { + if *this.state == CycleState::FromStream { next = futures_core::ready!(this.source.poll_next(cx)); if let Some(val) = next { - this.buffer.push(val); + this.buffer.push(val.clone()); + next = Some(val.clone()) } else { *this.state = CycleState::FromBuffer; - next = Some(this.buffer[*this.index]); + next = this.buffer.get(*this.index).map(|x| x.clone()); } } else { let mut index = *this.index; if index == this.buffer.len() { index = 0 } - next = Some(this.buffer[index]); + next = Some(this.buffer[index].clone()); *this.index = index + 1; } @@ -79,7 +81,7 @@ impl Stream for Cycle /// # /// # }) /// ``` -pub fn cycle, T: Copy>(source: S) -> impl Stream { +pub fn cycle, T: Clone>(source: S) -> impl Stream { Cycle { source, index: 0, From fd09e2f248ce8847a322362d6287e2d409e36158 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 29 Oct 2019 08:54:15 -0500 Subject: [PATCH 08/18] Run fmt --- src/stream/cycle.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs index 92f31ec1..5210a69b 100644 --- a/src/stream/cycle.rs +++ b/src/stream/cycle.rs @@ -22,13 +22,11 @@ enum CycleState { FromBuffer, } -impl Stream for Cycle - where - S: Stream, - T: Clone, - +impl Stream for Cycle +where + S: Stream, + T: Clone, { - type Item = S::Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { @@ -47,7 +45,7 @@ impl Stream for Cycle } } else { let mut index = *this.index; - if index == this.buffer.len() { + if index == this.buffer.len() { index = 0 } next = Some(this.buffer[index].clone()); From b979773505793ea9033ec54ae688854f85846998 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 29 Oct 2019 09:10:18 -0500 Subject: [PATCH 09/18] Follow clippys advice --- src/stream/cycle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stream/cycle.rs b/src/stream/cycle.rs index 5210a69b..df8b9563 100644 --- a/src/stream/cycle.rs +++ b/src/stream/cycle.rs @@ -41,7 +41,7 @@ where next = Some(val.clone()) } else { *this.state = CycleState::FromBuffer; - next = this.buffer.get(*this.index).map(|x| x.clone()); + next = this.buffer.get(*this.index).cloned(); } } else { let mut index = *this.index; From 5aadc5e4e96bb3c340450a4e40ef5fb638cfe741 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 29 Oct 2019 17:06:56 -0500 Subject: [PATCH 10/18] Make cycle a function on the stream type --- src/stream/mod.rs | 2 -- src/stream/{ => stream}/cycle.rs | 41 +++++++++----------------------- src/stream/stream/mod.rs | 34 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 32 deletions(-) rename src/stream/{ => stream}/cycle.rs (65%) diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 449c484e..07eecf28 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -300,7 +300,6 @@ //! [`take`]: trait.Stream.html#method.take //! [`min`]: trait.Stream.html#method.min -pub use cycle::{cycle, Cycle}; pub use empty::{empty, Empty}; pub use from_fn::{from_fn, FromFn}; pub use from_iter::{from_iter, FromIter}; @@ -313,7 +312,6 @@ pub use stream::{ pub(crate) mod stream; -mod cycle; mod empty; mod from_fn; mod from_iter; diff --git a/src/stream/cycle.rs b/src/stream/stream/cycle.rs similarity index 65% rename from src/stream/cycle.rs rename to src/stream/stream/cycle.rs index df8b9563..518a804c 100644 --- a/src/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -22,6 +22,17 @@ enum CycleState { FromBuffer, } +impl,> Cycle { + pub fn new(source: S) -> Cycle { + Cycle { + source, + index: 0, + buffer: Vec::new(), + state: CycleState::FromStream, + } + } +} + impl Stream for Cycle where S: Stream, @@ -57,33 +68,3 @@ where } } -/// Creats a stream that yields the provided values infinitely and in order. -/// -/// # Examples -/// -/// Basic usage: -/// -/// ``` -/// # async_std::task::block_on(async { -/// # -/// use async_std::prelude::*; -/// use async_std::stream; -/// -/// let mut s = stream::cycle(stream::once(7)); -/// -/// assert_eq!(s.next().await, Some(7)); -/// assert_eq!(s.next().await, Some(7)); -/// assert_eq!(s.next().await, Some(7)); -/// assert_eq!(s.next().await, Some(7)); -/// assert_eq!(s.next().await, Some(7)); -/// # -/// # }) -/// ``` -pub fn cycle, T: Clone>(source: S) -> impl Stream { - Cycle { - source, - index: 0, - buffer: Vec::new(), - state: CycleState::FromStream, - } -} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index d46ae879..274992b4 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -24,6 +24,7 @@ mod all; mod any; mod chain; +mod cycle; mod cmp; mod cycle; mod copied; @@ -91,6 +92,7 @@ use partial_cmp::PartialCmpFuture; use position::PositionFuture; use try_fold::TryFoldFuture; use try_for_each::TryForEachFuture; +use cycle::Cycle; pub use chain::Chain; pub use copied::Copied; @@ -411,6 +413,38 @@ extension_trait! { Copied::new(self) } + #[doc = r#" + Creats a stream that yields the provided values infinitely and in order. + + # Examples + + Basic usage: + + ``` + # async_std::task::block_on(async { + # + use async_std::prelude::*; + use async_std::stream; + + let mut s = stream::once(7).cycle(); + + assert_eq!(s.next().await, Some(7)); + assert_eq!(s.next().await, Some(7)); + assert_eq!(s.next().await, Some(7)); + assert_eq!(s.next().await, Some(7)); + assert_eq!(s.next().await, Some(7)); + # + # }) + ``` + "#] + fn cycle(self) -> Cycle + where + Self: Sized, + Self::Item: Clone, + { + Cycle::new(self) + } + #[doc = r#" Creates a stream that gives the current element's count as well as the next value. From ed5b095c7342c0690544089a05a5ba3766f5f6a1 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 29 Oct 2019 17:09:01 -0500 Subject: [PATCH 11/18] Run fmt --- src/stream/stream/cycle.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index 518a804c..9c145904 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -22,7 +22,7 @@ enum CycleState { FromBuffer, } -impl,> Cycle { +impl> Cycle { pub fn new(source: S) -> Cycle { Cycle { source, @@ -67,4 +67,3 @@ where Poll::Ready(next) } } - From 19381fa590c5af08cfdd4be6ca6d621e9c586dcc Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 29 Oct 2019 17:15:49 -0500 Subject: [PATCH 12/18] One clippy warning --- src/stream/stream/cycle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index 9c145904..91f33f97 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -49,7 +49,7 @@ where if let Some(val) = next { this.buffer.push(val.clone()); - next = Some(val.clone()) + next = Some(val) } else { *this.state = CycleState::FromBuffer; next = this.buffer.get(*this.index).cloned(); From 197253aa73abc16cd8a37a92767f87490894a9fa Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Fri, 1 Nov 2019 17:45:54 -0500 Subject: [PATCH 13/18] Run fmt --- src/stream/stream/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 274992b4..83b77308 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -24,11 +24,9 @@ mod all; mod any; mod chain; -mod cycle; mod cmp; -mod cycle; mod copied; -mod cmp; +mod cycle; mod enumerate; mod eq; mod filter; @@ -68,6 +66,7 @@ mod zip; use all::AllFuture; use any::AnyFuture; use cmp::CmpFuture; +use cycle::Cycle; use enumerate::Enumerate; use eq::EqFuture; use filter_map::FilterMap; @@ -92,7 +91,6 @@ use partial_cmp::PartialCmpFuture; use position::PositionFuture; use try_fold::TryFoldFuture; use try_for_each::TryForEachFuture; -use cycle::Cycle; pub use chain::Chain; pub use copied::Copied; From 0186124aef9716820d16ceefc9bf89425b3452e3 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Fri, 1 Nov 2019 20:37:20 -0500 Subject: [PATCH 14/18] Simpler impl --- src/stream/stream/cycle.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index 91f33f97..4d2dbf58 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -22,8 +22,12 @@ enum CycleState { FromBuffer, } -impl> Cycle { - pub fn new(source: S) -> Cycle { +impl Cycle +where + S: Stream, + S::Item: Clone, +{ + pub fn new(source: S) -> Cycle { Cycle { source, index: 0, From eaa56580e3a513910c18c30633d672e64a913ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Ser=C3=A9?= Date: Fri, 1 Nov 2019 21:24:46 -0500 Subject: [PATCH 15/18] Update src/stream/stream/mod.rs Co-Authored-By: Yoshua Wuyts --- src/stream/stream/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 83b77308..747c54e1 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -412,7 +412,7 @@ extension_trait! { } #[doc = r#" - Creats a stream that yields the provided values infinitely and in order. + Creates a stream that yields the provided values infinitely and in order. # Examples From 9ee804f9edc0e8bfea3e9122e114ef854161dcb2 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Fri, 1 Nov 2019 22:57:32 -0500 Subject: [PATCH 16/18] Only one generic type needed --- src/stream/stream/cycle.rs | 18 +++++++++++------- src/stream/stream/mod.rs | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index 4d2dbf58..c7781498 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -7,11 +7,15 @@ use crate::task::{Context, Poll}; pin_project! { /// A stream that will repeatedly yield the same list of elements - pub struct Cycle { + pub struct Cycle +where + S: Stream, + S::Item: Clone, + { #[pin] source: S, index: usize, - buffer: Vec, + buffer: Vec, state: CycleState, } } @@ -22,12 +26,12 @@ enum CycleState { FromBuffer, } -impl Cycle +impl Cycle where S: Stream, S::Item: Clone, { - pub fn new(source: S) -> Cycle { + pub fn new(source: S) -> Cycle { Cycle { source, index: 0, @@ -37,10 +41,10 @@ where } } -impl Stream for Cycle +impl Stream for Cycle where - S: Stream, - T: Clone, + S: Stream, + S::Item: Clone, { type Item = S::Item; diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 747c54e1..7b21bf07 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -435,7 +435,7 @@ extension_trait! { # }) ``` "#] - fn cycle(self) -> Cycle + fn cycle(self) -> Cycle where Self: Sized, Self::Item: Clone, From fbd5bd867de9ef6ece29463a03fa930ff2141bd6 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Fri, 1 Nov 2019 23:25:54 -0500 Subject: [PATCH 17/18] Revert "Only one generic type needed" This reverts commit e9b9284863a614b852c22d58205cb983fc26682a. --- src/stream/stream/cycle.rs | 18 +++++++----------- src/stream/stream/mod.rs | 2 +- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index c7781498..4d2dbf58 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -7,15 +7,11 @@ use crate::task::{Context, Poll}; pin_project! { /// A stream that will repeatedly yield the same list of elements - pub struct Cycle -where - S: Stream, - S::Item: Clone, - { + pub struct Cycle { #[pin] source: S, index: usize, - buffer: Vec, + buffer: Vec, state: CycleState, } } @@ -26,12 +22,12 @@ enum CycleState { FromBuffer, } -impl Cycle +impl Cycle where S: Stream, S::Item: Clone, { - pub fn new(source: S) -> Cycle { + pub fn new(source: S) -> Cycle { Cycle { source, index: 0, @@ -41,10 +37,10 @@ where } } -impl Stream for Cycle +impl Stream for Cycle where - S: Stream, - S::Item: Clone, + S: Stream, + T: Clone, { type Item = S::Item; diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 7b21bf07..747c54e1 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -435,7 +435,7 @@ extension_trait! { # }) ``` "#] - fn cycle(self) -> Cycle + fn cycle(self) -> Cycle where Self: Sized, Self::Item: Clone, From 57a6516e639317c8f89696dd8e8133a1c84a983b Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Fri, 1 Nov 2019 23:28:07 -0500 Subject: [PATCH 18/18] Make bounds on Stream impl simpler --- src/stream/stream/cycle.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stream/stream/cycle.rs b/src/stream/stream/cycle.rs index 4d2dbf58..8a31cc17 100644 --- a/src/stream/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -37,10 +37,10 @@ where } } -impl Stream for Cycle +impl Stream for Cycle where - S: Stream, - T: Clone, + S: Stream, + S::Item: Clone, { type Item = S::Item;