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: BImplementations§
§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.
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>
Available on crate feature alloc only.
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError>
alloc only.Creates an empty heap-backed writer preallocating capacity bytes.
§Errors
Returns TryReserveError if the allocation fails.
§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 byte storage buf.
pub fn write<P: Body + ?Sized>(
&mut self,
packet: &P,
) -> Result<(), WriteError<B::Error>>
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
WriteError::TooLargeif the packet is too large to encode at all.WriteError::Bufferif the buffer cannot take the packet.
pub fn write_measured<P: Body + ?Sized>(
&mut self,
measured: &Measured<'_, P>,
) -> Result<(), B::Error>
pub fn write_measured<P: Body + ?Sized>( &mut self, measured: &Measured<'_, P>, ) -> Result<(), B::Error>
pub fn clear(&mut self)
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>
impl<B: Debug> Debug for PacketWriter<B>
§impl Default for PacketWriter<GrowBuffer>
Available on crate feature alloc only.
impl Default for PacketWriter<GrowBuffer>
Available on crate feature
alloc only.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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more