Skip to main content

wallclock_timer/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! Wall-clock-based timer utilities.
3//!
4//! This crate provides a timer that triggers actions at fixed wall-clock deadlines,
5//! running on a dedicated thread. You interact with the timer through a cloneable
6//! handle (`TimerRef`) and schedule actions by absolute `SystemTime` deadlines.
7//!
8//! # Quick start (UUID ids)
9//! ```no_run
10//! use wallclock_timer::thread_timer::UuidClosureTimer;
11//! use wallclock_timer::ClosureTimer;
12//! use std::time::{Duration, SystemTime};
13//! use uuid::Uuid;
14//!
15//! let timer = UuidClosureTimer::default();
16//! let mut tref = timer.timer_ref();
17//!
18//! let deadline = SystemTime::now() + Duration::from_secs(1);
19//! tref.schedule_action_at(Uuid::new_v4(), deadline, |id| {
20//!     println!("Triggered {id}");
21//! }).expect("schedule");
22//!
23//! std::thread::sleep(Duration::from_secs(2));
24//! timer.shutdown().expect("shutdown");
25//! ```
26//!
27//! # Errors and logging
28//! Public APIs return  an implementation-specific [[Result]]
29//! (e.g. [[thread_timer::ThreadTimerError]] for the [[thread_timer::TimerWithThread]])
30//! for failures (such as channel send errors).
31//!
32//! The crate also uses the `log` facade for internal error reporting. Provide a logger
33//! implementation in your application to see logs.
34//!
35//! # UUID feature
36//! Enable the `uuid` feature to use UUID ids and the UUID-based shorthand aliases:
37//! - [[thread_timer::UuidClosureTimer]]
38//! - [[thread_timer::UuidClosureTimerRef]]
39//!
40//! # Chrono feature
41//! Enable the `chrono` feature to schedule timers using `chrono::DateTime` via
42//! [[timers::chrono::ChronoTimer::schedule_at_datetime]].
43
44pub mod thread_timer;
45mod timers;
46
47pub use timers::*;