Expand description
§os-trait
os-trait provides a unified trait layer for adapting multiple RTOS implementations to embedded Rust HALs.
It makes embedded Rust code more portable, testable, and OS‑agnostic by standardizing common OS primitives such as mutexes, delays, timeouts, notifier, and thread yielding.
This crate integrates with several foundational components of the embedded Rust ecosystem:
timeout-trait— timeout abstractionsembedded-hal— uses theDelayNstraitmutex— usesBlockingMutexandRawMutex
§📦 Usage
cargo add os-traituse os_trait::{prelude::*, FakeOs, StdOs, Duration, Timeout, Instant};
fn use_os<OS: OsInterface>() {
let mutex = OS::mutex(2);
let mut guard = mutex.try_lock().unwrap();
assert_eq!(*guard, 2);
OS::yield_thread();
OS::delay().delay_ms(1);
let mut t = Timeout::<OS>::millis(1);
if t.timeout() {
// handle timeout
}
let mut now = Instant::<OS>::now();
now.elapsed();
if now.timeout(&Duration::<OS>::millis(1)) {}
let (notifier, waiter) = OS::notify();
assert!(notifier.notify());
assert!(waiter.wait(&Duration::<OS>::millis(1)));
}
fn select_os() {
use_os::<FakeOs>();
use_os::<StdOs>();
}Use alias for convenience:
use os_trait::{prelude::*, StdOs as OS, os_type_alias};
os_type_alias!(OS);
fn use_os_type() {
let mutex = Mutex::new(2);
OS::yield_thread();
OS::delay().delay_ms(1);
let t = Timeout::millis(1);
let dur = Duration::millis(1);
let mut now = Instant::now();
if now.timeout(&dur) {}
let (notifier, waiter) = OS::notify();
assert!(notifier.notify());
assert!(waiter.wait(&Duration::millis(1)));
}§⚙️ Cargo Features
| Feature | Default | Description |
|---|---|---|
alloc | ✔️ | Enables allocation support |
std | ❌ | Enables std for unit testing |
std-custom-mutex | ❌ | Use BlockingMutex instead of std::sync::Mutex in std environments |
§🧩 Implementing Your Own OS
Implement the OsInterface trait and provide:
- A
RawMuteximplementation - A
NotifierInterfaceimplementation - A
DelayNsimplementation - A timeout implementation
- A thread‑yielding function
Once implemented, your OS becomes compatible with any HAL or driver that depends on os-trait.
For a Basic examples are available in os_impls.rs. Full implementation example, see os_trait_impls.rs for FreeRTOS.
§🔖 Keywords
embedded rust · rtos · hal · mutex · delay · timeout · portability · no_std · embedded-hal · traits
Re-exports§
pub use mutex::BlockingMutex;pub use mutex::FakeRawMutex;pub use os_impls::FakeOs;pub use os_impls::StdOs;pub use fugit;pub use mutex_traits;pub use portable_atomic;pub use timeout_trait;pub use notifier::*;pub use notifier_impls::*;
Modules§
- delay
- duration
- fake_
impls - fugit
fugitprovides a comprehensive library ofDurationandInstantfor the handling of time in embedded systems. The library is specifically designed to maximize const-ification which allows for most comparisons and changes of time-base to be made at compile time, rather than run time.- mutex
- notifier
- notifier_
impls - os_
impls - prelude
- std_
impls - timeout
Macros§
- os_
type_ alias - Use this macro to alias the OS-specific types for greater convenience, or manually alias only the ones you need.
Structs§
- Tick
Delay DelayNsimplementation- Tick
Duration - Tick
Timeout
Traits§
- Const
Init - Const Init Trait
- DelayNs
- Delay with up to nanosecond precision.
- OsInterface
- The interface for different operating systems.
- RawMutex
- Raw mutex trait.
- Tick
Instant - It doesn’t require operation interfaces on
TickInstantitself. Embedded systems can thus implement only the relative time version, which means you can not use it as a global timestamp.
Type Aliases§
- Delay
- Duration
- Instant
- Kilohertz
U32 - Alias for kilohertz rate (
u32backing storage) - Mutex
- Notifier
- Notify
Waiter - Timeout