fluxion-runtime
Part of Fluxion - A reactive stream processing library for Rust
Runtime abstraction layer enabling Fluxion to work across multiple async runtimes and platforms (Tokio, smol, async-std, WASM, Embassy).
Overview
fluxion-runtime provides the core abstractions that allow Fluxion operators to work seamlessly across different async runtimes without code changes. It includes:
Timertrait - Runtime-agnostic time abstraction for sleep and instant operationsFluxionMutextrait - Mutex abstraction supporting both thread-safe (Arc) and single-threaded (Rc) contexts- Runtime implementations - Concrete timer implementations for 5 different runtimes
Supported Runtimes
| Runtime | Feature Flag | Platform Support | Thread Safety |
|---|---|---|---|
| Tokio | runtime-tokio (default) |
Native (std) | Multi-threaded |
| smol | runtime-smol |
Native (std) | Multi-threaded |
| async-std | runtime-async-std |
Native (std) | Multi-threaded (⚠️ deprecated) |
| WASM | runtime-wasm |
Browser (no_std) | Single-threaded |
| Embassy | runtime-embassy |
Embedded (no_std) | Single-threaded |
⚠️ Note: async-std is unmaintained (RUSTSEC-2025-0052). Use Tokio or smol for new projects.
Features
Timer Trait
The Timer trait provides runtime-agnostic time operations:
Key Benefits:
- Zero-cost abstraction (compiles to direct runtime calls)
- Type-safe instant handling
- No runtime overhead
Runtime Implementations
Each runtime has a custom Timer implementation optimized for its execution model:
TokioTimer (Multi-threaded)
use TokioTimer;
use Duration;
async
SmolTimer (Multi-threaded)
use SmolTimer;
use Duration;
WasmTimer (Browser)
use ;
use Duration;
// WASM target only
async
EmbassyTimerImpl (Embedded)
use EmbassyTimerImpl;
use Duration;
// Embassy executor context
async
Usage with Fluxion Time Operators
The Timer abstraction is primarily used by fluxion-stream-time operators (debounce, throttle, delay, sample, timeout). Users typically don't interact with timers directly when using convenience methods:
use *;
use Duration;
// Convenience API (recommended) - timer chosen automatically
stream.debounce;
// Explicit API - for custom timer implementations
stream.debounce_with_timer;
Feature Flags
[]
= "0.8.0"
# Choose your runtime:
# Default: Tokio (no configuration needed)
# Alternative runtimes:
= { = "0.8.0", = false, = ["runtime-smol"] }
= { = "0.8.0", = false, = ["runtime-wasm"] }
= { = "0.8.0", = false, = ["runtime-embassy"] }
Feature Combinations
std- Enable standard library support (default)alloc- Enable allocator support for no_stdruntime-tokio- Tokio runtime support (includesstd,parking_lot)runtime-smol- smol runtime support (includesstd,parking_lot,async-io)runtime-async-std- async-std runtime support (includesstd,parking_lot,async-io)runtime-wasm- WASM runtime support (includesparking_lot,gloo-timers,js-sys)runtime-embassy- Embassy runtime support (includesembassy-time)
Architecture
Multi-threaded Runtimes (Tokio, smol, async-std)
- Use
std::time::Instantfor timestamps - Thread-safe with
Arc<Mutex>synchronization - Spawning support via
FluxionTasktrait
Single-threaded Runtimes (WASM, Embassy)
- Custom instant types (
WasmInstant,EmbassyInstant) - No thread bounds required
- Embassy: Static task allocation, no dynamic spawning
Implementation Details
Timer Pattern
All time-based operators use a consistent pattern:
This pattern enables:
- Zero-cost abstraction (monomorphization)
- Optimal memory layout
- Runtime-specific optimizations
Instant Types
Each runtime has a custom Instant type implementing required traits:
Copy + Debug + Ord- For comparison and debuggingAdd<Duration>/Sub<Duration>- For time arithmeticSub<Self, Output = Duration>- For elapsed time calculation
Examples
See the complete examples in the workspace:
- wasm-dashboard - WASM timer in browser
- embassy-sensors - Embassy timer on ARM Cortex-M4F
- stream-aggregation - Tokio timer (default)
Migration from Pre-0.8.0
If upgrading from versions before the dual trait bound system:
Before (Tokio-only):
use ;
After (Runtime-agnostic):
use *;
use Duration;
// Timer automatically selected based on runtime feature
stream.debounce;
No code changes needed - the timer abstraction is transparent!
Advanced: Custom Timer Implementation
To add support for a new runtime, implement the Timer trait:
use Timer;
use Duration;
;
Testing
The crate includes comprehensive tests for all runtime implementations:
# Test Tokio runtime (default)
# Test smol runtime
# Test WASM runtime
# Test Embassy runtime (compilation check)
Performance
The Timer abstraction has zero runtime overhead:
- Monomorphization eliminates virtual dispatch
- Compiler optimizes timer calls to direct runtime functions
- No allocations on the hot path
- Optimal memory layout with
#[pin]projections
Documentation
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.