#timer #sleep #cancel

cancellable-timer

A timer that can be interrupted

1 unstable release

0.1.0 Apr 9, 2019

#1541 in Asynchronous

Download history 10/week @ 2024-01-09 4/week @ 2024-01-16 4/week @ 2024-01-23 3/week @ 2024-01-30 3/week @ 2024-02-06 30/week @ 2024-02-13 44/week @ 2024-02-20 31/week @ 2024-02-27 22/week @ 2024-03-05 28/week @ 2024-03-12 29/week @ 2024-03-19 31/week @ 2024-03-26 46/week @ 2024-04-02 17/week @ 2024-04-09 18/week @ 2024-04-16 21/week @ 2024-04-23

109 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