[go: up one dir, main page]

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

Streams

  • stream::interval Creates a new stream that yields at a set interval.
  • Stream::audit
  • Stream::buffer Returns a stream which buffers items and flushes them at each interval.
  • Stream::debounce Returns a stream that debounces for the given duration.
  • Stream::delay Delay execution for a specified time.
  • Stream::sample
  • Stream::throttle Filter out all items after the first for a specified time.
  • Stream::timeout Cancel the stream if the execution takes longer than the specified time.

Modules

std::future extensions.

The futures-time prelude.

std::stream extensions.

std::task extensions.

async-aware std::time replacement.