pub struct MessageBuilder { /* private fields */ }Expand description
Submessage aggregator.
Initialized on open with a pre-allocated byte list starting at the
RTPS header. Submessages are appended via try_add_submessage;
when full the caller must finish() + open a new builder.
Implementations§
Source§impl MessageBuilder
impl MessageBuilder
Sourcepub fn open(header: RtpsHeader, targets: Rc<Vec<Locator>>, mtu: usize) -> Self
pub fn open(header: RtpsHeader, targets: Rc<Vec<Locator>>, mtu: usize) -> Self
Opens a new builder with the given RTPS header, targets and MTU budget.
Panics: if mtu is smaller than the RTPS header (20 bytes).
Sourcepub fn submsg_count(&self) -> usize
pub fn submsg_count(&self) -> usize
Number of submessages inserted so far.
Sourcepub fn try_add_submessage(
&mut self,
id: SubmessageId,
flags: u8,
body: &[u8],
) -> Result<(), AddError>
pub fn try_add_submessage( &mut self, id: SubmessageId, flags: u8, body: &[u8], ) -> Result<(), AddError>
Tries to append a submessage. Returns
AddError::WouldExceedMtu if it no longer fits —
then finish() + a new builder is due.
flags contains only the submessage-specific flags
(F, L, Q, H, K, N etc.). The E bit (little-endian) is set by the
builder itself, consistently for the whole datagram.
§Errors
AddError::WouldExceedMtuon size overflow.AddError::BodyTooLargeifbody.len() > u16::MAX.
Sourcepub fn try_add_submessage_split(
&mut self,
id: SubmessageId,
flags: u8,
header_body: &[u8],
payload_tail: &[u8],
) -> Result<(), AddError>
pub fn try_add_submessage_split( &mut self, id: SubmessageId, flags: u8, header_body: &[u8], payload_tail: &[u8], ) -> Result<(), AddError>
Like Self::try_add_submessage, but the body in two slices —
avoids an intermediate Vec when the caller already has header_body
and payload_tail separately. Exactly the hot-
path case for the DATA submessage: 20-byte fixed header (the caller
writes into a stack buffer) + N byte serializedData (borrowed from
the cache Arc<[u8]>). Without this variant the caller needs
Vec::with_capacity(20+N) + extend(header) + extend(payload) —
a heap alloc + copy of N bytes, which is fully eliminated here.
octets_to_next_header = header_body.len() + payload_tail.len().
§Errors
Same as Self::try_add_submessage.
Sourcepub fn finish(self) -> Option<OutboundDatagram>
pub fn finish(self) -> Option<OutboundDatagram>
Converts into a finished OutboundDatagram.
Returns None for an empty builder (only the RTPS header without
submessages) — this lets callers simply discard unused builders
without having to check is_empty()
first.