pub fn throttle<T>(duration: Duration, stream: T) -> Throttle<T>
where
    T: Stream,
{
    let delay = if duration == Duration::from_millis(0) {
        None
    } else {
        Some(Delay::new_timeout(Instant::now() + duration, duration))
    };

    Throttle {
        delay,
        duration,
        has_delayed: true,
        stream,
    }
}
/// Slows down a stream by enforcing a delay between items.
/// They will be produced not more often than the specified interval.
///
/// # Example
///
/// Create a throttled stream.
/// ```rust,no_run
/// use std::time::Duration;
/// use tokio::stream::StreamExt;
/// use tokio::time::throttle;
///
/// # async fn dox() {
/// let mut item_stream = throttle(Duration::from_secs(2), futures::stream::repeat("one"));
///
/// loop {
///     // The string will be produced at most every 2 seconds
///     println!("{:?}", item_stream.next().await);
/// }
/// # }
/// ```
Slows down a stream by enforcing a delay between items.
They will be produced not more often than the specified interval.
Slows down a stream by enforcing a delay between items . They will be produced not more often than the specified interval .
pub fn throttle<T>(duration: Duration, stream: T) -> Throttle<T>
where
    T: Stream,
{
    let delay = if duration == Duration::from_millis(0) {
        None
    } else {
        Some(Delay::new_timeout(Instant::now() + duration, duration))
    };
    Throttle {
        delay,
        duration,
        has_delayed: true,
        stream,
    }
}
pub fn throttle < T > ( duration : Duration , stream : T ) -> Throttle < T > where T : Stream , { let delay = if duration == Duration :: from_millis ( 0 ) { None } else { Some ( Delay :: new_timeout ( Instant :: now ( ) + duration , duration ) ) } ; Throttle { delay , duration , has_delayed : true , stream , } }
Slows down a stream by enforcing a delay between items.
Slows down a stream by enforcing a delay between items .
duration
Duration
stream
T
duration
Duration
stream
T
example
Create a throttled stream.
```rust,no_run
use std::time::Duration;
use tokio::stream::StreamExt;
use tokio::time::throttle.
Create a throttled stream . ` ` ` rust no_run use std :: time :: Duration ; use tokio :: stream :: StreamExt ; use tokio :: time :: throttle .
async fn dox() {
let mut item_stream = throttle(Duration::from_secs(2), futures::stream::repeat("one")).

loop {
The string will be produced at most every 2 seconds
println!("{:?}", item_stream.next().await);
}
let mut item_stream = throttle ( Duration :: from_secs ( 2 ) futures :: stream :: repeat ( " one " )) . loop { The string will be produced at most every 2 seconds println! ( " { : ? } " item_stream . next () . await ) ; }
}
None