#timer #sleep #cancel

cancellable-timer

A timer that can be interrupted

1 unstable release

0.1.0 Apr 9, 2019

#20 in #cancel

Download history 17/week @ 2024-11-13 15/week @ 2024-11-20 16/week @ 2024-11-27 43/week @ 2024-12-04 96/week @ 2024-12-11 34/week @ 2024-12-18 4/week @ 2024-12-25 5/week @ 2025-01-01 31/week @ 2025-01-08 22/week @ 2025-01-15 37/week @ 2025-01-22 6/week @ 2025-01-29 77/week @ 2025-02-05 21/week @ 2025-02-12 46/week @ 2025-02-19 27/week @ 2025-02-26

172 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