Skip to main content

PacketWriter

Struct PacketWriter 

pub struct PacketWriter<B> {
    buf: B,
}
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 stores many coalesced packets. There is no per-packet allocation.

Pair it with ArrayBuffer on no_std targets or GrowBuffer when alloc is available.

Fields§

§buf: B

Implementations§

§

impl PacketWriter<GrowBuffer>

pub const fn new() -> Self

Available on crate feature alloc only.

Creates an empty heap-backed writer.

pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>

Available on crate feature alloc only.

Creates an empty heap-backed writer preallocating capacity bytes.

§Errors

Returns TryReserveError if the allocation fails.

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

Available on crate feature alloc only.

Takes all pending bytes as an owned Vec. The writer is empty afterwards.

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 byte storage buf.

pub fn write<P: Body + ?Sized>( &mut self, packet: &P, ) -> Result<(), WriteError<B::Error>>

Encodes packet, appending its bytes to the pending output.

On error, nothing is buffered.

§Errors

pub fn write_measured<P: Body + ?Sized>( &mut self, measured: &Measured<'_, P>, ) -> Result<(), B::Error>

Appends a packet that was already measured. See write.

§Errors

Returns the buffer’s error if it cannot take the packet.

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 this on connection loss before reusing the writer.

Trait Implementations§

§

impl<B: Debug> Debug for PacketWriter<B>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
§

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> UnsafeUnpin for PacketWriter<B>
where B: UnsafeUnpin,

§

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 = !

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.