-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stream::cycle implementation #34
Closed
vertexclique
wants to merge
1
commit into
async-rs:master
from
vertexclique:stream-cycle-implementation
+84
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Repeats given stream values and sum them | ||
#![feature(async_await)] | ||
|
||
use async_std::io; | ||
use async_std::prelude::*; | ||
use async_std::stream; | ||
use async_std::task; | ||
|
||
fn main() -> io::Result<()> { | ||
task::block_on(async { | ||
let mut s = stream::cycle(vec![6, 7, 8]); | ||
let mut total = 0; | ||
|
||
while let Some(v) = s.next().await { | ||
total += v; | ||
if total == 42 { | ||
println!("Found {} the meaning of life!", total); | ||
break; | ||
} | ||
} | ||
|
||
Ok(()) | ||
}) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::pin::Pin; | ||
use std::sync::Mutex; | ||
|
||
use crate::task::{Context, Poll}; | ||
|
||
/// Creates a stream that yields the given elements continually. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # #![feature(async_await)] | ||
/// # fn main() { async_std::task::block_on(async { | ||
/// # | ||
/// use async_std::prelude::*; | ||
/// use async_std::stream; | ||
/// | ||
/// let mut s = stream::cycle(vec![1, 2, 3]); | ||
/// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In std the API is: let a = [1, 2, 3];
let mut it = a.iter().cycle();
assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), Some(&1)); I'd expect this API to be (with #125 landed): let a = [1, 2, 3];
let mut it = a.into_stream().cycle();
assert_eq!(it.next().await, Some(&1));
assert_eq!(it.next().await, Some(&2));
assert_eq!(it.next().await, Some(&3));
assert_eq!(it.next().await, Some(&1));
assert_eq!(it.next().await, Some(&2));
assert_eq!(it.next().await, Some(&3));
assert_eq!(it.next().await, Some(&1)); Similar, but subtly different in that it's more generic, which should make it more widely applicable! |
||
/// 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<T>(items: Vec<T>) -> Cycle<T> | ||
yoshuawuyts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where | ||
T: Clone, | ||
{ | ||
Cycle { | ||
items, | ||
cursor: Mutex::new(0_usize), | ||
} | ||
} | ||
|
||
/// A stream that yields the given elements continually. | ||
/// | ||
/// This stream is constructed by the [`cycle`] function. | ||
/// | ||
/// [`cycle`]: fn.cycle.html | ||
#[derive(Debug)] | ||
pub struct Cycle<T> { | ||
items: Vec<T>, | ||
cursor: Mutex<usize>, | ||
} | ||
|
||
impl<T: Clone> futures::Stream for Cycle<T> { | ||
type Item = T; | ||
|
||
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let cursor = &mut *self.cursor.lock().unwrap(); | ||
let p = Poll::Ready(self.items.get(*cursor).map(|x| x.to_owned())); | ||
*cursor = (*cursor + 1_usize) % self.items.len(); | ||
p | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is cool, but I think it's probably fine to just have the doc test for now. In #131 and #129 there are quite a few more methods, and I think it'd be wise to try and keep our line count down where possible.