PacketWriter

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>

pub const fn new() -> Self

Available on crate feature 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.

Creates an empty heap-backed writer preallocating capacity bytes.

pub fn take(&mut self) -> Vec<u8>

Available on crate feature alloc only.

Takes all pending bytes as an owned Vec, leaving the writer empty.

Lets an I/O driver hand a batch of coalesced packets to a writer task by value without copying.

§

impl<B: ByteBuffer> PacketWriter<B>

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)

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>

Encodes packet, appending its bytes to the pending output.

§Errors
  • WriteError::TooLarge if 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::BufferFull if a fixed-capacity buffer cannot fit it.
  • WriteError::Encode if 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>

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 pending(&self) -> &[u8]

The encoded bytes waiting to be written to the transport.

pub fn is_empty(&self) -> bool

Returns true if no bytes are pending.

pub fn consume(&mut self, n: usize)

Marks the leading n pending bytes as written to the transport.

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

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<B> Freeze for PacketWriter<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for PacketWriter<B>
where B: RefUnwindSafe,

§

impl<B> Send for PacketWriter<B>
where B: Send,

§

impl<B> Sync for PacketWriter<B>
where B: Sync,

§

impl<B> Unpin for PacketWriter<B>
where B: Unpin,

§

impl<B> UnwindSafe for PacketWriter<B>
where B: UnwindSafe,

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.