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 is part of safina, a safe async runtime.
Features
forbid(unsafe_code)- Depends only on
std - Good test coverage (90%)
- Source of time is
std::thread::park_timeoutviastd::sync::mpsc::Receiver::recv_timeout. - Works with
safina-executoror any async executor
Limitations
- Requires Rust
nightly, forOnceCell - Timers complete around 2ms late, but never early
- Allocates memory
Examples
start_timer_thread;
sleep_for.await;
start_timer_thread;
let deadline =
now + from_millis;
sleep_until.await;
use with_deadline;
start_timer_thread;
let deadline =
now + from_millis;
let req =
with_deadline.await??;
let data =
with_deadline.await??;
with_deadline.await??;
with_deadline.await??;
use with_timeout;
start_timer_thread;
let req = with_timeout.await??;
let data = with_timeout.await??;
with_timeout.await??;
with_timeout.await??;
Documentation
Alternatives
- futures-timer
- popular
- Supports: Wasm, Linux, Windows, macOS
- Contains generous amounts of
unsafecode - Uses
std::thread::park_timeoutas 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.6 - Fix tests broken by
safina-async-testchanges - 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
- Update
with_deadlineandwith_timeout:- Make them panic on
TimerThreadNotStartederror and return newDeadlineExceededstruct instead ofDeadlineErrorenum. This allows callers to write a match clause likeErr(DeadlineExceeded). - Make them use
std::boxed::Box::pinso callers don't have to.
- Make them panic on
- Make
sleep_untilandsleep_forreturn()and panic ifstart_timer_thread()has not been called.
- Update
- v0.1.1
- Use most recent waker passed to
SleepFuture::poll, as required by thestd::future::Future::pollcontract. - Add
with_deadlineandwith_timeoutfunctions.
- Use most recent waker passed to
- v0.1.0 - First published version
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 (
FnOncestructs). - 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
RawWakerinstead of unstablewake_traitfeature. Will need to move tests to integration tests, since those are separate crates.
Release Process
- Edit
Cargo.tomland bump version number. - Run
./release.sh
License: Apache-2.0