#timer #sleep #cancel

cancellable-timer

A timer that can be interrupted

1 unstable release

0.1.0 Apr 9, 2019

#1518 in Asynchronous

Download history 21/week @ 2023-12-11 31/week @ 2023-12-18 6/week @ 2023-12-25 28/week @ 2024-01-01 15/week @ 2024-01-08 5/week @ 2024-01-15 7/week @ 2024-01-29 1/week @ 2024-02-05 19/week @ 2024-02-12 53/week @ 2024-02-19 30/week @ 2024-02-26 23/week @ 2024-03-04 28/week @ 2024-03-11 27/week @ 2024-03-18 30/week @ 2024-03-25

114 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