pub trait Serialize {
// Required method
fn to_bytes(&self) -> &[u8];
}Expand description
Trait for converting types to byte slices.
Enables serialization of structured data for transmission through queues or other byte-oriented communication channels.
§Safety
When implementing this trait, ensure that the returned byte slice is a valid representation of the type and lives at least as long as the value itself.
§Examples
ⓘ
use osal_rs::traits::Serialize;
struct SensorData {
temperature: i16,
humidity: u8,
}
impl Serialize for SensorData {
fn to_bytes(&self) -> &[u8] {
// Convert struct to bytes
// Safety: ensure proper memory layout
unsafe {
core::slice::from_raw_parts(
self as *const Self as *const u8,
core::mem::size_of::<Self>()
)
}
}
}Required Methods§
Implementors§
impl<const SIZE: usize> Serialize for Bytes<SIZE>
Available on non-crate feature
serde only.Serialization implementation for Bytes<SIZE> when the serde feature is disabled.
This implementation provides basic serialization by directly returning a reference
to the underlying byte array. It’s used when the library is compiled without the
serde feature, providing a lightweight alternative serialization mechanism.