Struct PacketWriter
pub struct PacketWriter<B> {
buf: B,
max_packet_size: Option<NonZeroU32>,
}Expand description
A buffering, sans-io packet serializer.
Packets are encoded in place into a ByteBuffer with
write; the accumulated bytes are then drained by an I/O
driver with pending + consume. This is
the outbound mirror of PacketReader: one reusable
buffer holds many coalesced packets, so there is no per-packet allocation.
Pair it with ArrayBuffer on no_std targets or
GrowBuffer when alloc is available.
Fields§
§buf: B§max_packet_size: Option<NonZeroU32>Implementations§
§impl PacketWriter<GrowBuffer>
impl PacketWriter<GrowBuffer>
pub const fn new() -> Self
Available on crate feature alloc only.
pub const fn new() -> Self
alloc only.Creates an empty heap-backed writer with no packet-size limit.
pub fn with_capacity(capacity: usize) -> Self
Available on crate feature alloc only.
pub fn with_capacity(capacity: usize) -> Self
alloc only.Creates an empty heap-backed writer preallocating capacity bytes.
§impl<B: ByteBuffer> PacketWriter<B>
impl<B: ByteBuffer> PacketWriter<B>
pub const fn from_buffer(buf: B) -> Self
pub const fn from_buffer(buf: B) -> Self
Creates a writer over the given byte storage with no packet-size limit.
pub const fn set_max_packet_size(&mut self, max: NonZeroU32)
pub const fn set_max_packet_size(&mut self, max: NonZeroU32)
Rejects any packet whose encoded size exceeds max bytes, before it is
written into the buffer (§3.2.2.3.6).
pub fn write(&mut self, packet: &Packet<'_>) -> Result<(), WriteError>
pub fn write(&mut self, packet: &Packet<'_>) -> Result<(), WriteError>
Encodes packet, appending its bytes to the pending output.
§Errors
WriteError::TooLargeif the encoded packet exceeds the configured maximum packet size, or is too large to encode at all (its Remaining Length exceeds the VBI maximum); nothing is buffered.WriteError::BufferFullif a fixed-capacity buffer cannot fit it.WriteError::Encodeif encoding fails (a bug; nothing is buffered).
pub(crate) fn write_with(
&mut self,
total_len: usize,
encode: impl FnOnce(&mut Writer<'_>) -> Result<(), EncodeError>,
) -> Result<(), WriteError>
pub(crate) fn write_with( &mut self, total_len: usize, encode: impl FnOnce(&mut Writer<'_>) -> Result<(), EncodeError>, ) -> Result<(), WriteError>
Encodes a packet produced incrementally by encode into the pending
output; encode must write exactly total_len bytes. Building block
for packets assembled from non-contiguous session storage (the
SUBSCRIBE/UNSUBSCRIBE drain). Same size checks as
write.
pub fn clear(&mut self)
pub fn clear(&mut self)
Drops all pending bytes; call on connection loss before reusing.
Trait Implementations§
§impl Default for PacketWriter<GrowBuffer>
Available on crate feature alloc only.
impl Default for PacketWriter<GrowBuffer>
alloc only.