#timer #async #async-executor #safe

safina-timer

Safe async timers - ARCHIVED: Code moved to safina crate

14 releases

0.1.13 Oct 27, 2024
0.1.12 Oct 27, 2024
0.1.11 Mar 20, 2022
0.1.10 Feb 25, 2022
0.1.6 Dec 24, 2020

#740 in Asynchronous

Download history 33/week @ 2024-07-29 33/week @ 2024-08-05 24/week @ 2024-08-12 21/week @ 2024-08-19 61/week @ 2024-08-26 17/week @ 2024-09-02 24/week @ 2024-09-09 16/week @ 2024-09-16 51/week @ 2024-09-23 35/week @ 2024-09-30 13/week @ 2024-10-07 15/week @ 2024-10-14 47/week @ 2024-10-21 247/week @ 2024-10-28 17/week @ 2024-11-04 3/week @ 2024-11-11

316 downloads per month
Used in 9 crates (7 directly)

Apache-2.0

41KB
667 lines

ARCHIVED ARCHIVED ARCHIVED

This crate is archived and will not be updated.

The code is now at safina::timer in the safina crate.


safina-timer

Provides async sleep_for and sleep_until functions.

This crate is part of safina, a safe async runtime.

Features

Limitations

  • Building on stable requires the feature once_cell. This uses once_cell crate which contains some unsafe code. This is necessary until std::lazy::OnceCell is stable.
  • Timers complete around 2ms late, but never early
  • Allocates memory

Examples

safina_timer::start_timer_thread();
let duration = Duration::from_secs(10);
safina_timer::sleep_for(duration).await;
safina_timer::start_timer_thread();
let deadline =
    Instant::now() + Duration::from_secs(1);
safina_timer::sleep_until(deadline).await;
safina_timer::start_timer_thread();
let deadline =
    Instant::now() + Duration::from_secs(1);
let req = safina_timer::with_deadline(
    read_request(), deadline).await??;
let data = safina_timer::with_deadline(
    read_data(req), deadline).await??;
safina_timer::with_deadline(
    write_data(data), deadline ).await??;
safina_timer::with_deadline(
    send_response(data), deadline).await??;
safina_timer::start_timer_thread();
let req = safina_timer::with_timeout(
    read_request(), Duration::from_secs(1)
).await??;
let data = safina_timer::with_timeout(
    read_data(req), Duration::from_secs(2)
).await??;
safina_timer::with_timeout(
    write_data(data), Duration::from_secs(2)
).await??;
safina_timer::with_timeout(
    send_response(data),
    Duration::from_secs(1)
).await??;

Documentation

https://docs.rs/safina-timer

Alternatives

  • futures-timer
    • popular
    • Supports: Wasm, Linux, Windows, macOS
    • Contains generous amounts of unsafe code
    • Uses std::thread::park_timeout as its source of time
  • async-io
    • popular
    • single and repeating timers
    • Supports: Linux, Windows, macOS, iOS, Android, and many others.
    • Uses polling crate which makes unsafe calls to OS.
  • async-timer
    • Supports: Linux & Android
    • Makes unsafe calls to OS
  • tokio
    • very popular
    • single and repeating timers
    • Supports: Linux, macOS, other unix-like operating systems, Windows
    • Fast, internally complicated, and full of unsafe
  • embedded-async-timer
    • no_std
    • Supports bare_metal

Changelog

  • v0.1.11 - Remove some type constraints.
  • v0.1.10 - Use safina-executor v0.2.0.
  • v0.1.9 - Name the timer thread.
  • v0.1.8 - Increase test coverage
  • v0.1.7 - Support stable with rust 1.51 and once_cell.
  • v0.1.6 - Update dependencies
  • v0.1.5 - Update docs
  • v0.1.4 - Upgrade to new safina-executor version which removes need for Box::pin.
  • v0.1.3 - Add badges to readme
  • v0.1.2
  • v0.1.1
  • v0.1.0 - First published version

TO DO

  • Add a way to schedule jobs (FnOnce structs).

Release Process

  1. Edit Cargo.toml and bump version number.
  2. Run ./release.sh

Dependencies

~47KB