pub trait QueueStreamedFn<T>where
T: Deserialize + Sized,{
// Required methods
fn fetch(&self, buffer: &mut T, time: TickType) -> Result<()>;
fn fetch_from_isr(&self, buffer: &mut T) -> Result<()>;
fn post(&self, item: &T, time: TickType) -> Result<()>;
fn post_from_isr(&self, item: &T) -> Result<()>;
fn delete(&mut self);
}Expand description
Type-safe queue for structured message passing.
This trait provides a queue that works with specific types, offering compile-time type safety for queue operations.
§Type Safety
Unlike raw Queue, QueueStreamed ensures that only messages
of type T can be sent and received, preventing type confusion
at compile time.
§Serialization
Messages are automatically serialized when sent and deserialized
when received. The type T must implement the Deserialize trait.
§Type Parameters
T- The message type (must implementDeserialize)
§Examples
use osal_rs::os::QueueStreamed;
use osal_rs::traits::Deserialize;
#[derive(Clone, Copy)]
struct SensorData {
id: u32,
temperature: i16,
humidity: u8,
}
impl Deserialize for SensorData {
fn from_bytes(bytes: &[u8]) -> Result<Self> {
// Deserialization logic
}
}
let queue = QueueStreamed::<SensorData>::new(10, size_of::<SensorData>()).unwrap();
// Producer
let data = SensorData { id: 1, temperature: 235, humidity: 65 };
queue.post(&data, 100).unwrap();
// Consumer
let mut received = SensorData { id: 0, temperature: 0, humidity: 0 };
queue.fetch(&mut received, 100).unwrap();
assert_eq!(received.id, 1);Required Methods§
Sourcefn fetch(&self, buffer: &mut T, time: TickType) -> Result<()>
fn fetch(&self, buffer: &mut T, time: TickType) -> Result<()>
Fetches a typed message from the queue (blocking).
Removes and deserializes the oldest message from the queue. Blocks the calling task if the queue is empty.
§Parameters
buffer- Mutable reference to receive the deserialized messagetime- Maximum ticks to wait for a message:0: Return immediately if emptyn: Wait up tonticksTickType::MAX: Wait forever
§Returns
Ok(())- Message received and deserialized successfullyErr(Error::Timeout)- Queue was empty for entire timeout periodErr(Error)- Deserialization error or other error
§Examples
let mut msg = Message::default();
match queue.fetch(&mut msg, 1000) {
Ok(()) => println!("Received message: {:?}", msg),
Err(_) => println!("No message available"),
}Sourcefn fetch_from_isr(&self, buffer: &mut T) -> Result<()>
fn fetch_from_isr(&self, buffer: &mut T) -> Result<()>
Fetches a typed message from ISR context (non-blocking).
ISR-safe version of fetch(). Returns immediately without blocking.
Must only be called from interrupt context.
§Parameters
buffer- Mutable reference to receive the deserialized message
§Returns
Ok(())- Message received and deserialized successfullyErr(Error)- Queue is empty or deserialization failed
§Examples
// In interrupt handler
let mut msg = Message::default();
if queue.fetch_from_isr(&mut msg).is_ok() {
// Process message
}Sourcefn post(&self, item: &T, time: TickType) -> Result<()>
fn post(&self, item: &T, time: TickType) -> Result<()>
Posts a typed message to the queue (blocking).
Serializes and adds a new message to the end of the queue. Blocks the calling task if the queue is full.
§Parameters
item- Reference to the message to serialize and sendtime- Maximum ticks to wait if queue is full:0: Return immediately if fulln: Wait up tonticks for spaceTickType::MAX: Wait forever
§Returns
Ok(())- Message serialized and sent successfullyErr(Error::Timeout)- Queue was full for entire timeout periodErr(Error)- Serialization error or other error
§Examples
let msg = Message { id: 42, value: 100 };
match queue.post(&msg, 1000) {
Ok(()) => println!("Sent successfully"),
Err(_) => println!("Failed to send"),
}Sourcefn post_from_isr(&self, item: &T) -> Result<()>
fn post_from_isr(&self, item: &T) -> Result<()>
Posts a typed message from ISR context (non-blocking).
ISR-safe version of post(). Returns immediately without blocking.
Must only be called from interrupt context.
§Parameters
item- Reference to the message to serialize and send
§Returns
Ok(())- Message serialized and sent successfullyErr(Error)- Queue is full or serialization failed
§Examples
// In interrupt handler
let msg = Message { id: 1, value: 42 };
if queue.post_from_isr(&msg).is_err() {
// Queue full, message dropped
}Sourcefn delete(&mut self)
fn delete(&mut self)
Deletes the queue and frees its resources.
§Safety
Ensure no tasks are blocked on this queue before deletion. Calling this while tasks are waiting may cause undefined behavior.
§Examples
let mut queue = QueueStreamed::<Message>::new(10, size_of::<Message>()).unwrap();
// Use queue...
queue.delete();Implementors§
impl<T> QueueStreamed<T> for QueueStreamed<T>where
T: StructSerde,
serde only.