pub enum Error<'a> {
Show 20 variants
OutOfMemory,
QueueSendTimeout,
QueueReceiveTimeout,
MutexTimeout,
MutexLockFailed,
Timeout,
QueueFull,
StringConversionError,
TaskNotFound,
InvalidQueueSize,
NullPtr,
NotFound,
OutOfIndex,
InvalidType,
Empty,
WriteError(&'a str),
ReadError(&'a str),
ReturnWithCode(i32),
Unhandled(&'a str),
UnhandledOwned(String),
}Expand description
Error types for OSAL-RS operations.
Represents all possible error conditions that can occur when using the OSAL-RS library.
§Lifetime Parameter
The error type is generic over lifetime 'a to allow flexible error messages.
Most of the time, you can use the default Result<T> type alias which uses
Error<'static>. For custom lifetimes in error messages, use
core::result::Result<T, Error<'a>> explicitly.
§Examples
§Basic usage with static errors
use osal_rs::os::{Queue, QueueFn};
use osal_rs::utils::Error;
match Queue::new(10, 32) {
Ok(queue) => { /* use queue */ },
Err(Error::OutOfMemory) => println!("Failed to allocate queue"),
Err(e) => println!("Other error: {:?}", e),
}§Using borrowed error messages
use osal_rs::utils::Error;
fn validate_input(input: &str) -> core::result::Result<(), Error> {
if input.is_empty() {
// Use static lifetime for compile-time strings
Err(Error::Unhandled("Input cannot be empty"))
} else {
Ok(())
}
}
// For dynamic error messages from borrowed data
fn process_data<'a>(data: &'a str) -> core::result::Result<(), Error<'a>> {
if !data.starts_with("valid:") {
// Error message borrows from 'data' lifetime
Err(Error::ReadError(data))
} else {
Ok(())
}
}Variants§
OutOfMemory
Insufficient memory to complete operation
QueueSendTimeout
Queue send operation timed out
QueueReceiveTimeout
Queue receive operation timed out
MutexTimeout
Mutex operation timed out
MutexLockFailed
Failed to acquire mutex lock
Timeout
Generic timeout error
QueueFull
Queue is full and cannot accept more items
StringConversionError
String conversion failed
TaskNotFound
Thread/task not found
InvalidQueueSize
Invalid queue size specified
NullPtr
Null pointer encountered
NotFound
Requested item not found
OutOfIndex
Index out of bounds
InvalidType
Invalid type for operation
Empty
No data available
WriteError(&'a str)
Write error occurred
ReadError(&'a str)
Read error occurred
ReturnWithCode(i32)
Return error with code
Unhandled(&'a str)
Unhandled error with description
UnhandledOwned(String)
Unhandled error with description owned
Trait Implementations§
impl<'a> Eq for Error<'a>
Source§impl<'a> Error for Error<'a>
Implements the standard Error trait for Error<'a>. This allows
Error<'a> to be used with Rust’s error handling ecosystem, including
Result and ? operator.
impl<'a> Error for Error<'a>
Implements the standard Error trait for Error<'a>. This allows
Error<'a> to be used with Rust’s error handling ecosystem, including
Result and ? operator.
§Examples
§Using ? with the crate’s Result alias
use osal_rs::utils::{Error, Result};
fn parse_level(input: &str) -> Result<u8> {
input.parse::<u8>().map_err(|_| Error::StringConversionError)
}
fn set_level(input: &str) -> Result<()> {
// `?` propagates `Error` because it implements `core::error::Error`
let level = parse_level(input)?;
assert!(level <= 255);
Ok(())
}
assert!(set_level("42").is_ok());
assert_eq!(set_level("abc"), Err(Error::StringConversionError));§Boxing into a dyn Error
extern crate alloc;
use alloc::boxed::Box;
use osal_rs::utils::Error;
fn fallible() -> core::result::Result<(), Box<dyn core::error::Error>> {
// `Error<'static>` converts into `Box<dyn Error>` automatically
Err(Error::Timeout)?;
Ok(())
}
let err = fallible().unwrap_err();
assert_eq!(err.to_string(), "Operation timeout");§Inspecting the error source chain
use core::error::Error as _;
use osal_rs::utils::Error;
let err = Error::Unhandled("sensor offline");
// `Error` has no underlying cause, so `source()` is `None`
assert!(err.source().is_none());
assert_eq!(err.to_string(), "Unhandled error: sensor offline");1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<Error> for Error<'static>
Available on crate feature posix only.Converts a std::io::Error into an Error<'static>. This is useful for
integrating standard I/O errors with the crate’s error handling system.
impl From<Error> for Error<'static>
posix only.Converts a std::io::Error into an Error<'static>. This is useful for
integrating standard I/O errors with the crate’s error handling system.
The resulting variant is always Error::UnhandledOwned, with the
message prefixed by io error: .
§Examples
§Propagating I/O errors with ?
use osal_rs::utils::Result;
fn read_config(path: &str) -> Result<String> {
// `std::io::Error` is converted into `Error<'static>` by `?`
let content = std::fs::read_to_string(path)?;
Ok(content)
}
let err = read_config("/this/path/does/not/exist").unwrap_err();
assert!(err.to_string().starts_with("Unhandled error owned: io error: "));§Explicit conversion and pattern matching
use std::io;
use osal_rs::utils::Error;
let io_err = io::Error::new(io::ErrorKind::PermissionDenied, "access denied");
let err: Error = io_err.into();
match err {
Error::UnhandledOwned(msg) => assert_eq!(msg, "io error: access denied"),
other => panic!("unexpected variant: {other:?}"),
}