Skip to main content

StructSerde

Trait StructSerde 

Source
pub trait StructSerde:
    Serialize
    + BytesHasLen
    + Deserialize { }
Expand description

Queue types for inter-task communication. Marker trait bundling the bounds required to send a type through a QueueStreamed instead of a raw Queue: it must be serializable to bytes, deserializable from bytes, and know its own byte length.

Blanket-implemented below for every type satisfying Serialize + BytesHasLen + Deserialize (or their osal-rs-serde equivalents when the serde feature is enabled) - never implement it by hand.

§Examples

use osal_rs::os::*;

struct Reading(u32);

impl BytesHasLen for Reading {
    fn len(&self) -> usize { core::mem::size_of::<u32>() }
}

impl Serialize for Reading {
    fn to_bytes(&self) -> &[u8] {
        unsafe {
            core::slice::from_raw_parts(&self.0 as *const u32 as *const u8, core::mem::size_of::<u32>())
        }
    }
}

impl Deserialize for Reading {
    fn from_bytes(bytes: &[u8]) -> osal_rs::utils::Result<Self> {
        let mut buf = [0u8; 4];
        buf.copy_from_slice(&bytes[..4]);
        Ok(Reading(u32::from_le_bytes(buf)))
    }
}

// `Reading` satisfies `Serialize + BytesHasLen + Deserialize`, so it
// automatically implements `StructSerde` via the blanket impl below.
fn accepts_queue_payload<T: StructSerde>(_: &T) {}
accepts_queue_payload(&Reading(42));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§