pub struct CdrWriter<'a> { /* private fields */ }Implementations§
Source§impl<'a> CdrWriter<'a>
impl<'a> CdrWriter<'a>
Sourcepub fn new_at(buf: &'a mut [u8], pos: usize) -> Result<CdrWriter<'a>, SerError>
pub fn new_at(buf: &'a mut [u8], pos: usize) -> Result<CdrWriter<'a>, SerError>
Create a CDR writer positioned at pos bytes into buf.
origin stays at 0, so alignment is computed relative to the start
of buf. Used by FFI bridges that hand us a (origin, cursor, end)
triple where buf = origin..end and the caller’s cursor is pos.
Sourcepub fn new_at_xcdr2(
buf: &'a mut [u8],
pos: usize,
) -> Result<CdrWriter<'a>, SerError>
pub fn new_at_xcdr2( buf: &'a mut [u8], pos: usize, ) -> Result<CdrWriter<'a>, SerError>
Like new_at but XCDR2 (DELIMITED_CDR2) — 8-byte
primitives align to 4 and begin_dheader emits a
DHEADER. Used by the C FFI (nros-c) tx path, which manages the
encapsulation header + cursor itself (phase-303 W4 / #0267).
Sourcepub fn new_with_header(buf: &'a mut [u8]) -> Result<CdrWriter<'a>, SerError>
pub fn new_with_header(buf: &'a mut [u8]) -> Result<CdrWriter<'a>, SerError>
Create a new CDR writer with the 4-byte encapsulation header.
Writes [0x00, 0x01, 0x00, 0x00] (CDR little-endian) at the start
and sets origin = 4 so subsequent alignment is relative to the
payload, not the header. This is the normal entry point for
serialising ROS 2 messages.
Sourcepub fn new_with_header_xcdr2(
buf: &'a mut [u8],
) -> Result<CdrWriter<'a>, SerError>
pub fn new_with_header_xcdr2( buf: &'a mut [u8], ) -> Result<CdrWriter<'a>, SerError>
Create an XCDR2 (DELIMITED_CDR2) writer with the 0x0009 encapsulation
header (phase-303 W2 / #0267). Each struct — top-level and nested — must
be wrapped in begin_dheader /
end_dheader; 8-byte primitives align to 4.
Sourcepub fn measuring(buf: &'a mut [u8], version: EncodingVersion) -> CdrWriter<'a>
pub fn measuring(buf: &'a mut [u8], version: EncodingVersion) -> CdrWriter<'a>
Phase 380 W3 — a writer that counts bytes and stores none.
serialized_size needs the EXACT size of one message, which a type-level
bound cannot give for an unbounded type and which a second walk of the
schema could only approximate. Running the real writer with its stores
disabled makes the count exact by construction: same alignment, same
DHEADERs, same len + 1 string prefix, because it is the same code.
Pass an empty slice — the buffer is never touched:
let mut w = CdrWriter::measuring(&mut [], EncodingVersion::Xcdr1);
value.serialize(&mut w)?;
let bytes = w.position();The encapsulation header is NOT counted: like the real constructors’
origin, a measuring writer starts at 0. Add
crate::size::ENCAPSULATION_HEADER_BYTES for the payload a publisher
hands the transport.
Sourcepub fn version(&self) -> EncodingVersion
pub fn version(&self) -> EncodingVersion
The CDR encoding version this writer emits.
Sourcepub fn begin_dheader(&mut self) -> Result<DHeaderMark, SerError>
pub fn begin_dheader(&mut self) -> Result<DHeaderMark, SerError>
Begin a DHEADER-delimited struct. Under XCDR2, aligns to 4, reserves a
4-byte size slot (backpatched by end_dheader), and
returns its position. Under XCDR1 this is a NO-OP (returns an empty mark),
so generated serialize bodies can wrap every struct unconditionally
while XCDR1 output stays byte-identical.
Sourcepub fn end_dheader(&mut self, mark: DHeaderMark) -> Result<(), SerError>
pub fn end_dheader(&mut self, mark: DHeaderMark) -> Result<(), SerError>
Finish a DHEADER-delimited struct: backpatch the reserved slot with the
serialized size of the member block written since
begin_dheader. No-op under XCDR1.
Sourcepub fn align(&mut self, alignment: usize) -> Result<(), SerError>
pub fn align(&mut self, alignment: usize) -> Result<(), SerError>
Align to the given boundary (relative to origin). Under XCDR2 the alignment is capped at 4 — 8-byte primitives align to 4, not 8 (the one primitive-layout difference from XCDR1).
Sourcepub fn write_u8(&mut self, value: u8) -> Result<(), SerError>
pub fn write_u8(&mut self, value: u8) -> Result<(), SerError>
Write a single byte without alignment
Sourcepub fn write_bool(&mut self, value: bool) -> Result<(), SerError>
pub fn write_bool(&mut self, value: bool) -> Result<(), SerError>
Write a boolean (serialized as a single byte: 0 = false, 1 = true)
Sourcepub fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), SerError>
pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), SerError>
Write bytes without alignment
Sourcepub fn write_u16(&mut self, value: u16) -> Result<(), SerError>
pub fn write_u16(&mut self, value: u16) -> Result<(), SerError>
Write u16 with alignment (little-endian)
Sourcepub fn write_u32(&mut self, value: u32) -> Result<(), SerError>
pub fn write_u32(&mut self, value: u32) -> Result<(), SerError>
Write u32 with alignment (little-endian)
Sourcepub fn write_u64(&mut self, value: u64) -> Result<(), SerError>
pub fn write_u64(&mut self, value: u64) -> Result<(), SerError>
Write u64 with alignment (little-endian)
Sourcepub fn write_i16(&mut self, value: i16) -> Result<(), SerError>
pub fn write_i16(&mut self, value: i16) -> Result<(), SerError>
Write i16 with alignment (little-endian)
Sourcepub fn write_i32(&mut self, value: i32) -> Result<(), SerError>
pub fn write_i32(&mut self, value: i32) -> Result<(), SerError>
Write i32 with alignment (little-endian)
Sourcepub fn write_i64(&mut self, value: i64) -> Result<(), SerError>
pub fn write_i64(&mut self, value: i64) -> Result<(), SerError>
Write i64 with alignment (little-endian)
Sourcepub fn write_f32(&mut self, value: f32) -> Result<(), SerError>
pub fn write_f32(&mut self, value: f32) -> Result<(), SerError>
Write f32 with alignment (little-endian)
Sourcepub fn write_f64(&mut self, value: f64) -> Result<(), SerError>
pub fn write_f64(&mut self, value: f64) -> Result<(), SerError>
Write f64 with alignment (little-endian)