tokio-interval-set 0.1.0

Run a closure repeatedly on a fixed time interval using tokio
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented3 out of 4 items with examples
  • Size
  • Source code size: 15.57 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 353.08 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 8s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ordinaryday-my

tokio-interval-set

A lightweight utility for running a closure repeatedly on a fixed time interval, backed by [tokio]'s time and task system.

Example

use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::interval;
use tokio_interval_set::set_interval;

let rt = tokio::runtime::Builder::new_current_thread()
    .enable_time()
    .build()
    .unwrap();

rt.block_on(async {
    let count = Arc::new(Mutex::new(0u64));
    let count_cloned = Arc::clone(&count);

    let handle = set_interval(interval(Duration::from_millis(100)), move || {
        let count = Arc::clone(&count_cloned);
        async move {
            let mut c = count.lock().await;
            *c += 1;
        }
    });

    tokio::time::sleep(Duration::from_millis(350)).await;
    handle.clear(); // stop the interval

    let final_count = *count.lock().await;
    assert!(final_count >= 3, "expected at least 3 ticks, got {}", final_count);
});