7 releases

0.1.6 Dec 24, 2020
0.1.5 Dec 24, 2020

#83 in Asynchronous

45 downloads per month
Used in less than 7 crates

Apache-2.0

42KB
651 lines

safina-timer

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

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 is part of safina, a safe async runtime.

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;
safina_timer::start_timer_thread();
let deadline =
    Instant::now() + Duration::from_millis(500);
safina_timer::sleep_until(deadline).await;
use safina_timer::with_deadline;
safina_timer::start_timer_thread();
let deadline =
    Instant::now() + Duration::from_millis(500);
let req =
    with_deadline(read_request(), deadline).await??;
let data =
    with_deadline(read_data(req), deadline).await??;
with_deadline(write_data(data), deadline).await??;
with_deadline(send_response(data), deadline).await??;
use safina_timer::with_timeout;
safina_timer::start_timer_thread();
let req = with_timeout(
    read_request(), Duration::from_millis(500)
).await??;
let data = with_timeout(
    read_data(req), Duration::from_millis(1000)
).await??;
with_timeout(
    write_data(data), Duration::from_millis(1000)
).await??;
with_timeout(
    send_response(data), Duration::from_millis(5000)
).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

TO DO

  • DONE - Implement sleep_until
  • DONE - Implement sleep_for
  • DONE - Add tests
  • DONE - Add docs
  • DONE - Publish on crates.io
  • Add a way to schedule jobs (FnOnce structs).
  • Add a way to build on stable, using unsafe once_cell.
    #![cfg_attr(feature = "unstable", feature(fused))]
    
    #[cfg(feature = "unstable")]
    impl ::std::iter::FusedIterator for Iter {}
    
  • Make tests run on stable. Make tests use RawWaker instead of unstable wake_trait feature. Will need to move tests to integration tests, since those are separate crates.

Release Process

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

License: Apache-2.0

No runtime deps