portable-async-sleep

A portable async sleep function for Rust.
This crate provides a runtime-agnostic implementation of async sleep functionality.
Unlike runtime-specific sleep functions (e.g., tokio::time::sleep or async_std::task::sleep),
portable_async_sleep works with any async runtime or executor.
Features
- Runtime-agnostic: Works with tokio, async-std, smol, or any other async runtime
- Lightweight: Uses standard library primitives with minimal overhead
- Thread-safe: Can be used from multiple async tasks simultaneously
- Accurate timing: Respects the requested sleep duration
Implementation
The default implementation uses a dedicated background thread that manages sleep timers using standard library channels and timeouts. This approach ensures maximum compatibility across all async runtimes while maintaining good performance.
While the stdlib-based implementation is the most portable, the crate is designed to support alternative backends in the future that may offer better performance or integration with specific runtimes, while still maintaining the same portable API.
Examples
Basic usage:
use async_sleep;
use Duration;
# block_on;
Using with concurrent tasks:
use async_sleep;
use Duration;
use Instant;
use Instant;
# block_on;