Struct shadow_rs::utility::byte_queue::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
impl ByteQueue
pub fn new(default_chunk_capacity: usize) -> Self
sourcepub fn num_bytes(&self) -> usize
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.
sourcepub fn has_chunks(&self) -> bool
pub fn has_chunks(&self) -> bool
Returns true if the queue has data/chunks, which may include packets with 0 bytes.
sourcepub fn push_stream<R: Read>(&mut self, src: R) -> Result<usize>
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.
sourcepub fn push_packet<R: Read>(&mut self, src: R, size: usize) -> Result<()>
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.
sourcepub fn push_chunk(
&mut self,
data: impl Into<BytesWrapper>,
chunk_type: ChunkType,
) -> usize
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.
sourcepub fn pop<W: Write>(
&mut self,
dst: W,
) -> Result<Option<(usize, usize, ChunkType)>>
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.
sourcepub fn pop_chunk(&mut self, size_hint: usize) -> Option<(Bytes, ChunkType)>
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.
sourcepub fn peek<W: Write>(
&self,
dst: W,
) -> Result<Option<(usize, usize, ChunkType)>>
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§
Auto Trait Implementations§
impl Freeze for ByteQueue
impl RefUnwindSafe for ByteQueue
impl Send for ByteQueue
impl Sync for ByteQueue
impl Unpin for ByteQueue
impl UnwindSafe for ByteQueue
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
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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