Crates.io | timer-lib |
lib.rs | timer-lib |
version | 0.1.0 |
source | src |
created_at | 2024-11-25 13:25:14.951435 |
updated_at | 2024-11-25 13:25:14.951435 |
description | A feature-rich Rust library for creating and managing timers. |
homepage | https://github.com/Tfc538/timer_lib |
repository | https://github.com/Tfc538/timer_lib |
max_upload_size | |
id | 1460322 |
size | 36,195 |
A feature-rich timer library for Rust with support for one-time, recurring, and scheduled timers. TimerLib is designed to be robust, flexible, and easy to use with advanced features like pause/resume functionality, dynamic interval adjustments, and timer statistics. Under the hood, it uses a combination of tokio
and async-std
to provide a seamless async/await experience.
Feature | TimerLib | tokio | async-std |
---|---|---|---|
One-Time Timers | ✓ | ✓ | ✓ |
Recurring Timers | ✓ | ✓ | ✓ |
Pause and Resume | ✓ | ✗ | ✗ |
Dynamic Interval Adjustment | ✓ | ✗ | ✗ |
Timer Statistics | ✓ | ✗ | ✗ |
Thread Safety | ✓ | ✓ | ✓ |
Async/Futures Integration | ✓ | ✓ | ✓ |
Error Handling | ✓ | ✓ | ✓ |
Performance | High | High | High |
Add TimerLib to your Cargo.toml
:
[dependencies]
timer-lib = "1.0.0"
Here’s a quick example of using TimerLib to schedule a one-time task:
use timer-lib::{Timer, TimerCallback};
use async_trait::async_trait;
struct MyCallback;
#[async_trait]
impl TimerCallback for MyCallback {
async fn execute(&self) {
println!("Timer executed!");
}
}
#[tokio::main]
async fn main() {
let mut timer = Timer::new();
timer.start_once(std::time::Duration::from_secs(2), MyCallback).await.unwrap();
}
use timer-lib::{Timer, TimerCallback};
use async_trait::async_trait;
struct RecurringCallback;
#[async_trait]
impl TimerCallback for RecurringCallback {
async fn execute(&self) {
println!("Recurring timer executed!");
}
}
#[tokio::main]
async fn main() {
let mut timer = Timer::new();
timer.start_recurring(std::time::Duration::from_secs(5), RecurringCallback, None).await.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(15)).await; // Let it run for 15 seconds
timer.stop(); // Stop the timer
}
For more examples, check the documentation.
Detailed documentation is available on docs.rs.
Contributions are welcome! Please read the CONTRIBUTING.md file for guidelines.
This project is licensed under the MIT License. See the LICENSE file for details.