|
|
@ -319,23 +319,23 @@ extension_trait! {
|
|
|
|
Limit the amount of items yielded per timeslice in a stream.
|
|
|
|
Limit the amount of items yielded per timeslice in a stream.
|
|
|
|
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
# Examples
|
|
|
|
```ignore
|
|
|
|
```
|
|
|
|
# fn main() { async_std::task::block_on(async {
|
|
|
|
# fn main() { async_std::task::block_on(async {
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
use async_std::prelude::*;
|
|
|
|
use async_std::stream;
|
|
|
|
use async_std::stream;
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
// emit value every 1 second
|
|
|
|
// emit value every 1 second
|
|
|
|
let s = stream::interval(Duration::from_nanos(1000000)).enumerate();
|
|
|
|
let s = stream::interval(Duration::from_millis(5)).enumerate().take(3);
|
|
|
|
|
|
|
|
|
|
|
|
// throttle for 2 seconds
|
|
|
|
// throttle for 2 seconds
|
|
|
|
let s = s.throttle(Duration::from_secs(2));
|
|
|
|
let mut s = s.throttle(Duration::from_millis(10));
|
|
|
|
|
|
|
|
|
|
|
|
s.for_each(|(n, _)| {
|
|
|
|
assert_eq!(s.next().await, Some((0, ())));
|
|
|
|
dbg!(n);
|
|
|
|
assert_eq!(s.next().await, Some((1, ())));
|
|
|
|
})
|
|
|
|
assert_eq!(s.next().await, Some((2, ())));
|
|
|
|
.await;
|
|
|
|
assert_eq!(s.next().await, None);
|
|
|
|
// => 0 .. 1 .. 2 .. 3
|
|
|
|
|
|
|
|
// with a pause of 2 seconds between each print
|
|
|
|
// with a pause of 2 seconds between each print
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# }) }
|
|
|
|
# }) }
|
|
|
|