#timer #sleep #cancel

cancellable-timer

A timer that can be interrupted

1 unstable release

0.1.0 Apr 9, 2019

#15 in #cancel

Download history 18/week @ 2024-07-29 13/week @ 2024-08-05 20/week @ 2024-08-12 8/week @ 2024-08-19 37/week @ 2024-08-26 17/week @ 2024-09-02 9/week @ 2024-09-09 9/week @ 2024-09-16 37/week @ 2024-09-23 19/week @ 2024-09-30 1/week @ 2024-10-07 21/week @ 2024-10-14 24/week @ 2024-10-21 6/week @ 2024-10-28 37/week @ 2024-11-04 23/week @ 2024-11-11

92 downloads per month
Used in 3 crates

MIT/Apache

8KB
88 lines

cancellable-timer

Crate that implements a timer with a sleep method that can be cancelled.

Example

use std::time::Duration;
use cancellable_timer::*;

fn main() {
    let (mut timer, canceller) = Timer::new2().unwrap();

    // Spawn a thread that will cancel the timer after 2s.
    std::thread::spawn(move || {
        std::thread::sleep(Duration::from_secs(2));
        println!("Stop the timer.");
        canceller.cancel();
    });

    println!("Wait 10s");
    let r = timer.sleep(Duration::from_secs(10));
    println!("Done: {:?}", r);
}

License: MIT/Apache-2.0


lib.rs:

Crate that implements a timer with a sleep method that can be cancelled.

Example

use std::time::Duration;
use cancellable_timer::*;

fn main() {
    let (mut timer, canceller) = Timer::new2().unwrap();

    // Spawn a thread that will cancel the timer after 2s.
    std::thread::spawn(move || {
        std::thread::sleep(Duration::from_secs(2));
        println!("Stop the timer.");
        canceller.cancel();
    });

    println!("Wait 10s");
    let r = timer.sleep(Duration::from_secs(10));
    println!("Done: {:?}", r);
}

Dependencies

~0.6–1MB
~14K SLoC