Skip to main content

CdrWriter

Struct CdrWriter 

Source
pub struct CdrWriter<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> CdrWriter<'a>

Source

pub fn new(buf: &'a mut [u8]) -> CdrWriter<'a>

Create a new CDR writer

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn version(&self) -> EncodingVersion

The CDR encoding version this writer emits.

Source

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.

Source

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.

Source

pub fn position(&self) -> usize

Get current position in buffer

Source

pub fn remaining(&self) -> usize

Get remaining capacity

Source

pub fn as_slice(&self) -> &[u8]

Get the written bytes

Source

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).

Source

pub fn write_u8(&mut self, value: u8) -> Result<(), SerError>

Write a single byte without alignment

Source

pub fn write_bool(&mut self, value: bool) -> Result<(), SerError>

Write a boolean (serialized as a single byte: 0 = false, 1 = true)

Source

pub fn write_i8(&mut self, value: i8) -> Result<(), SerError>

Write i8 without alignment

Source

pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), SerError>

Write bytes without alignment

Source

pub fn write_u16(&mut self, value: u16) -> Result<(), SerError>

Write u16 with alignment (little-endian)

Source

pub fn write_u32(&mut self, value: u32) -> Result<(), SerError>

Write u32 with alignment (little-endian)

Source

pub fn write_u64(&mut self, value: u64) -> Result<(), SerError>

Write u64 with alignment (little-endian)

Source

pub fn write_i16(&mut self, value: i16) -> Result<(), SerError>

Write i16 with alignment (little-endian)

Source

pub fn write_i32(&mut self, value: i32) -> Result<(), SerError>

Write i32 with alignment (little-endian)

Source

pub fn write_i64(&mut self, value: i64) -> Result<(), SerError>

Write i64 with alignment (little-endian)

Source

pub fn write_f32(&mut self, value: f32) -> Result<(), SerError>

Write f32 with alignment (little-endian)

Source

pub fn write_f64(&mut self, value: f64) -> Result<(), SerError>

Write f64 with alignment (little-endian)

Source

pub fn write_string(&mut self, s: &str) -> Result<(), SerError>

Write a CDR string (4-byte length including null + data + null terminator)

Source

pub fn write_sequence_len(&mut self, len: usize) -> Result<(), SerError>

Write a sequence length (4-byte count)

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for CdrWriter<'a>

§

impl<'a> Freeze for CdrWriter<'a>

§

impl<'a> RefUnwindSafe for CdrWriter<'a>

§

impl<'a> Send for CdrWriter<'a>

§

impl<'a> Sync for CdrWriter<'a>

§

impl<'a> Unpin for CdrWriter<'a>

§

impl<'a> UnsafeUnpin for CdrWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.