Crate futures_time
source · [−]Expand description
Async time operators.
About
This crate provides ergonomic, async time-based operations. It serves as an
experimental playground to experiment with how we could potentially add
time-based operations to async-std, and subsequently the stdlib.
The goal is to make working with time and other events feel natural. A major source of inspiration for this has been RxJS, which uses events (including time) to trigger operations. This crate takes that principle, inverts the model to make it evaluate lazily, and wraps it in an ergnomic Rust interface.
Examples
Throttle a stream
use futures_lite::prelude::*;
use futures_time::prelude::*;
use futures_time::time::Duration;
use futures_time::stream;
fn main() {
async_io::block_on(async {
let mut counter = 0;
stream::interval(Duration::from_millis(100)) // Yield an item every 100ms
.take(4) // Stop after 4 items
.throttle(Duration::from_millis(200)) // Only let an item through every 200ms
.for_each(|_| counter += 1) // Increment a counter for each item
.await;
assert_eq!(counter, 2);
})
}Futures
task::sleepSleeps for the specified amount of time.task::sleep_untilSleeps until the specified deadline.Future::delayDelay execution for a specified time.Future::timeoutCancel the future if the execution takes longer than the specified time.
Streams
stream::intervalCreates a new stream that yields at a set interval.Stream::auditStream::bufferReturns a stream which buffers items and flushes them at each interval.Stream::debounceReturns a stream that debounces for the given duration.Stream::delayDelay execution for a specified time.Stream::sampleStream::throttleFilter out all items after the first for a specified time.Stream::timeoutCancel the stream if the execution takes longer than the specified time.