CDR (Common Data Representation) codec for ROS 2 / DDS messages.
re_cdr decodes and encodes CDR.
It is aimed at ROS 2 reflection: decoding messages whose layout is only known at runtime, as when reading MCAP.
Inspired by cdr-encoding and hiroz-cdr.
CdrSerializer and CdrDeserializer integrate with serde for static Rust types.
CdrReader and CdrWriter read and write aligned primitives for runtime schemas. CdrReader::read_numeric_vec handles numeric arrays (float32[], uint32[], ...), using a byte-copy path when the message byte order matches the host and per-element reads otherwise.
Readers and writers are generic over byteorder::ByteOrder. Choose LittleEndian or BigEndian from the message encapsulation header.
use re_cdr::{from_bytes, to_vec, LittleEndian};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Vector3 { x: f64, y: f64, z: f64 }
let v = Vector3 { x: 1.0, y: 2.0, z: 3.0 };
let bytes = to_vec::<Vector3, LittleEndian>(&v).unwrap();
let (decoded, consumed) = from_bytes::<Vector3, LittleEndian>(&bytes).unwrap();
assert_eq!(decoded, v);
assert_eq!(consumed, bytes.len());Apache-2.0 or MIT