-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
487811e
commit 4b1c188
Showing
3 changed files
with
84 additions
and
0 deletions.
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 same 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]); | ||
/// | ||
/// 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> | ||
where | ||
T: Clone, | ||
{ | ||
Cycle { | ||
items, | ||
cursor: Mutex::new(0_usize), | ||
} | ||
} | ||
|
||
/// A stream that yields the same 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