[][src]Crate safina_timer

This is a safe Rust library providing async sleep_for and sleep_until functions. These functions return futures that complete at the specified time.

It works well with safina.

Features

Limitations

  • Requires Rust nightly, for OnceCell
  • Timers complete around 2ms late, but never early
  • Allocates memory

Examples

safina_timer::start_timer_thread();
safina_timer::sleep_for(Duration::from_secs(10)).await.unwrap();
safina_timer::start_timer_thread();
let deadline = Instant::now() + Duration::from_millis(500);
safina_timer::sleep_until(deadline).await.unwrap();

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 OSes, Windows
    • Fast, internally complicated, and full of unsafe
  • embedded-async-timer
    • no_std
    • Supports: bare_metal

Release Process

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

Changelog

TO DO

  • DONE - Implement sleep_until
  • DONE - Implement sleep_for
  • DONE - Add tests
  • Add docs
  • Publish on crates.io

Structs

DeadlineFuture

A future wrapper that returns DeadlineExceeded at a specified deadline.

SleepFuture

A future that completes after the specified time.

TimerThreadNotStarted

Call start_timer_thread to prevent this error.

Enums

DeadlineError

Functions

sleep_for

Returns a future that completes duration time from now.

sleep_until

Returns a future that completes after deadline.

start_timer_thread

Starts the worker thread, if it's not already started. You must call this before calling sleep_until or sleep_for.

with_deadline

Make a future that awaits inner, or returns DeadlineError after deadline.

with_timeout

Make a future that awaits inner, or returns DeadlineError after duration time from now.