Expand description
Derive macros for osal-rs-serde
This crate provides #[derive(Serialize, Deserialize)] macros for automatic
implementation of serialization traits. Enable them through the derive
feature of osal-rs-serde rather than depending on this crate directly.
Named-field structs, tuple structs and unit structs are supported. Fields are
serialized in declaration order; tuple struct fields are named "0", "1", …
§Examples
use osal_rs_serde::{Serialize, Deserialize, to_bytes, from_bytes};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct SensorData {
temperature: i16,
humidity: u8,
pressure: u32,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Color(u8, u8, u8);
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Marker;
let mut buffer = [0u8; 16];
let data = SensorData { temperature: -5, humidity: 60, pressure: 1013 };
let len = to_bytes(&data, &mut buffer).unwrap();
assert_eq!(len, 7);
assert_eq!(from_bytes::<SensorData>(&buffer[..len]).unwrap(), data);
let color = Color(255, 128, 0);
let len = to_bytes(&color, &mut buffer).unwrap();
assert_eq!(&buffer[..len], &[255, 128, 0]);
assert_eq!(from_bytes::<Color>(&buffer[..len]).unwrap(), color);
// Unit structs carry no data
assert_eq!(to_bytes(&Marker, &mut buffer).unwrap(), 0);
assert_eq!(from_bytes::<Marker>(&[]).unwrap(), Marker);§Limitations
Enums are not supported yet:
ⓘ
use osal_rs_serde::Serialize;
#[derive(Serialize)]
enum Status {
Active,
Inactive,
}Unions are not supported:
ⓘ
use osal_rs_serde::Serialize;
#[derive(Serialize)]
union Data {
integer: i32,
float: f32,
}Generic structs are not supported yet:
ⓘ
use osal_rs_serde::Serialize;
#[derive(Serialize)]
struct Container<T> {
value: T,
}Derive Macros§
- Deserialize
- Derive macro for the
Deserializetrait. - Serialize
- Derive macro for the
Serializetrait.