Expand description
Utility types and functions for OSAL-RS.
This module contains common types, error definitions, and helper functions used throughout the library.
§Overview
The utilities module provides essential building blocks for working with OSAL-RS in embedded environments:
- Error handling: Comprehensive
Errorenum for all OSAL operations - String utilities: Fixed-size
Bytestype for embedded string handling - Conversion macros: Safe C string conversion and parameter extraction
- FFI types: Type aliases for C interoperability
- Thread safety:
SyncUnsafeCellfor static mutable state in Rust 2024+
§Main Types
§Error Handling
Error<'a>- All possible error conditions with optional borrowed error messagesResult<T, E>- Type alias forcore::result::Resultwith defaultError<'static>OsalRsBool- Boolean type compatible with RTOS return values
§String Handling
Bytes<SIZE>- Fixed-size byte buffer with string conversion utilitiesAsSyncStr- Trait for thread-safe string references
§Constants
MAX_DELAY- Maximum timeout for blocking indefinitelyCpuRegisterSize- CPU register size detection (32-bit or 64-bit)
§FFI Types
§Thread Safety
SyncUnsafeCell<T>- Wrapper for static mutable variables (replacesstatic mut)
§Macros
§String Conversion
- [
from_c_str!] - Convert C string pointer to Rust String (lossy conversion) - [
to_cstring!] - Convert Rust string to CString with error handling - [
to_c_str!] - Convert Rust string to C string pointer (panics on error) - [
from_str_to_array!] - Convert string to fixed-size byte array
§Parameter Handling
- [
thread_extract_param!] - Extract typed parameter from thread entry point - [
access_static_option!] - Access static Option variable (panics if None)
§Helper Functions
§Hex Conversion
bytes_to_hex- Convert bytes to hex string (allocates)bytes_to_hex_into_slice- Convert bytes to hex into buffer (no allocation)hex_to_bytes- Parse hex string to bytes (allocates)hex_to_bytes_into_slice- Parse hex string into buffer (no allocation)
§Platform Detection
register_bit_size- Const function to detect CPU register size (32-bit or 64-bit)
§Best Practices
- Use
Bytes<SIZE>for embedded strings: Avoids heap allocation, fixed size - Prefer no-alloc variants: Use
_into_slicefunctions when possible - Handle errors explicitly: Always check
Resultreturns - Use
to_cstring!overto_c_str!: Better error handling with?operator - Synchronize
SyncUnsafeCellaccess: Use mutexes or critical sections in multi-threaded environments
Structs§
- Bytes
- Fixed-size byte array wrapper with string conversion utilities.
- Sync
Unsafe Cell - Thread-safe wrapper for
UnsafeCellusable instaticcontexts.
Enums§
- CpuRegister
Size - CPU register size enumeration.
- Error
- Error types for OSAL-RS operations.
- Osal
RsBool - Boolean type compatible with RTOS return values.
Constants§
- MAX_
DELAY - Maximum delay constant for blocking operations.
Traits§
- AsSync
Str - Trait for types that can provide a string reference in a thread-safe manner.
Functions§
- bytes_
to_ hex - Converts a byte slice to a hexadecimal string representation.
- bytes_
to_ hex_ into_ slice - Converts a byte slice to hexadecimal representation into a pre-allocated buffer.
- hex_
to_ bytes - Converts a hexadecimal string to a vector of bytes.
- hex_
to_ bytes_ into_ slice - Converts a hexadecimal string to bytes into a pre-allocated buffer.
- register_
bit_ size - Determines the CPU register size at compile time.