shadow_rs::utility::byte_queue

Struct ByteQueue

source
pub struct ByteQueue { /* private fields */ }
Expand description

A queue of bytes that supports reading and writing stream and/or packet data.

Both stream and packet data can be pushed onto the buffer and their order will be preserved. Data is stored internally as a linked list of chunks. Each chunk stores either stream or packet data. Consecutive stream data may be merged into a single chunk, but consecutive packets will always be contained in their own chunks.

To avoid memory copies when moving bytes from one ByteQueue to another, you can use pop_chunk() to remove a chunk from the queue, and use push_chunk() to add it to another queue.

Implementations§

source§

impl ByteQueue

source

pub fn new(default_chunk_capacity: usize) -> Self

source

pub fn num_bytes(&self) -> usize

The number of bytes in the queue. If the queue has 0 bytes, it does not mean that the queue is empty since there may be 0-length packets in the queue.

source

pub fn has_bytes(&self) -> bool

Returns true if the queue has bytes.

source

pub fn has_chunks(&self) -> bool

Returns true if the queue has data/chunks, which may include packets with 0 bytes.

source

pub fn push_stream<R: Read>(&mut self, src: R) -> Result<usize>

Push stream data onto the queue. The data may be merged into the previous stream chunk.

source

pub fn push_packet<R: Read>(&mut self, src: R, size: usize) -> Result<()>

Push packet data onto the queue in a single chunk. Exactly size bytes will be read into the packet.

source

pub fn push_chunk( &mut self, data: impl Into<BytesWrapper>, chunk_type: ChunkType, ) -> usize

Push a chunk of stream or packet data onto the queue.

source

pub fn pop<W: Write>( &mut self, dst: W, ) -> Result<Option<(usize, usize, ChunkType)>>

Pop data from the queue. Only a single type of data will be popped per invocation. To read all data from the queue, you must call this method until the returned chunk type is None. Zero-length packets may be returned. If packet data is returned but dst did not have enough space, the remaining bytes in the packet will be dropped. Returns a tuple containing the number of bytes copied, the number of bytes removed from the queue (including dropped bytes), and the chunk type.

source

pub fn pop_chunk(&mut self, size_hint: usize) -> Option<(Bytes, ChunkType)>

Pop a single chunk of data from the queue. The size_hint argument is used to limit the number of bytes in the returned chunk iff the next chunk has stream data. If the returned chunk has packet data, the size_hint is ignored and the entire packet is returned.

source

pub fn peek<W: Write>( &self, dst: W, ) -> Result<Option<(usize, usize, ChunkType)>>

Peek data from the queue. Only a single type of data will be peeked per invocation. Zero-length packets may be returned. If packet data is returned but dst did not have enough space, the packet written to dst will be truncated. Returns a tuple containing the number of bytes copied, the number of bytes that would have been copied if dst had enough space (for packet chunks, the size of the packet), and the chunk type.

Trait Implementations§

source§

impl Drop for ByteQueue

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> NoTypeInference for T

source§

type This = T

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V