Trait packet::BufferView

source ·
pub trait BufferView<B: ByteSlice>: Sized + AsRef<[u8]> {
Show 15 methods // Required methods fn take_front(&mut self, n: usize) -> Option<B>; fn take_back(&mut self, n: usize) -> Option<B>; fn into_rest(self) -> B; // Provided methods fn len(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } fn take_rest_front(&mut self) -> B { ... } fn take_rest_back(&mut self) -> B { ... } fn take_byte_front(&mut self) -> Option<u8> { ... } fn take_byte_back(&mut self) -> Option<u8> { ... } fn peek_obj_front<T>(&mut self) -> Option<&T> where T: FromBytes + NoCell + Unaligned { ... } fn take_obj_front<T>(&mut self) -> Option<Ref<B, T>> where T: Unaligned { ... } fn take_slice_front<T>(&mut self, n: usize) -> Option<Ref<B, [T]>> where T: Unaligned { ... } fn peek_obj_back<T>(&mut self) -> Option<&T> where T: FromBytes + NoCell + Unaligned { ... } fn take_obj_back<T>(&mut self) -> Option<Ref<B, T>> where T: Unaligned { ... } fn take_slice_back<T>(&mut self, n: usize) -> Option<Ref<B, [T]>> where T: Unaligned { ... }
}
Expand description

A view into a ShrinkBuffer.

A BufferView borrows a ShrinkBuffer, and provides methods to consume bytes from the buffer’s body. It is primarily intended to be used for parsing, although it provides methods which are useful for serialization as well.

A BufferView only provides immutable access to the contents of the buffer. For mutable access, see BufferViewMut.

§Notable implementations

BufferView is implemented for mutable references to byte slices (&mut &[u8] and &mut &mut [u8]).

Required Methods§

source

fn take_front(&mut self, n: usize) -> Option<B>

Takes n bytes from the front of the buffer’s body.

take_front consumes n bytes from the front of the buffer’s body. After a successful call to take_front(n), the body is n bytes shorter and, if Self: GrowBuffer, the prefix is n bytes longer. If the body is not at least n bytes in length, take_front returns None.

source

fn take_back(&mut self, n: usize) -> Option<B>

Takes n bytes from the back of the buffer’s body.

take_back consumes n bytes from the back of the buffer’s body. After a successful call to take_back(n), the body is n bytes shorter and, if Self: GrowBuffer, the suffix is n bytes longer. If the body is not at least n bytes in length, take_back returns None.

source

fn into_rest(self) -> B

Converts this view into a reference to the buffer’s body.

into_rest consumes this BufferView by value, and returns a reference to the buffer’s body. Unlike take_rest, the body is not consumed - it is left unchanged.

Provided Methods§

source

fn len(&self) -> usize

The length of the buffer’s body.

source

fn is_empty(&self) -> bool

Is the buffer’s body empty?

source

fn take_rest_front(&mut self) -> B

Takes the rest of the buffer’s body from the front.

take_rest_front consumes the rest of the bytes from the buffer’s body. After a call to take_rest_front, the body is empty and, if Self: GrowBuffer, the bytes which were previously in the body are now in the prefix.

source

fn take_rest_back(&mut self) -> B

Takes the rest of the buffer’s body from the back.

take_rest_back consumes the rest of the bytes from the buffer’s body. After a call to take_rest_back, the body is empty and, if Self: GrowBuffer, the bytes which were previously in the body are now in the suffix.

source

fn take_byte_front(&mut self) -> Option<u8>

Takes a single byte of the buffer’s body from the front.

take_byte_front consumes a single byte from the from of the buffer’s body. It’s equivalent to calling take_front(1) and copying out the single byte on successful return.

source

fn take_byte_back(&mut self) -> Option<u8>

Takes a single byte of the buffer’s body from the back.

take_byte_back consumes a single byte from the fron of the buffer’s body. It’s equivalent to calling take_back(1) and copying out the single byte on successful return.

source

fn peek_obj_front<T>(&mut self) -> Option<&T>
where T: FromBytes + NoCell + Unaligned,

Peeks at an object at the front of the buffer’s body.

peek_obj_front peeks at size_of::<T>() bytes at the front of the buffer’s body, and interprets them as a T. Unlike take_obj_front, peek_obj_front does not modify the body. If the body is not at least size_of::<T>() bytes in length, peek_obj_front returns None.

source

fn take_obj_front<T>(&mut self) -> Option<Ref<B, T>>
where T: Unaligned,

Takes an object from the front of the buffer’s body.

take_obj_front consumes size_of::<T>() bytes from the front of the buffer’s body, and interprets them as a T. After a successful call to take_obj_front::<T>(), the body is size_of::<T>() bytes shorter and, if Self: GrowBuffer, the prefix is size_of::<T>() bytes longer. If the body is not at least size_of::<T>() bytes in length, take_obj_front returns None.

source

fn take_slice_front<T>(&mut self, n: usize) -> Option<Ref<B, [T]>>
where T: Unaligned,

Takes a slice of objects from the front of the buffer’s body.

take_slice_front consumes n * size_of::<T>() bytes from the front of the buffer’s body, and interprets them as a [T] with n elements. After a successful call to take_slice_front::<T>(), the body is n * size_of::<T>() bytes shorter and, if Self: GrowBuffer, the prefix is n * size_of::<T>() bytes longer. If the body is not at least n * size_of::<T>() bytes in length, take_slice_front returns None.

§Panics

Panics if T is a zero-sized type.

source

fn peek_obj_back<T>(&mut self) -> Option<&T>
where T: FromBytes + NoCell + Unaligned,

Peeks at an object at the back of the buffer’s body.

peek_obj_back peeks at size_of::<T>() bytes at the back of the buffer’s body, and interprets them as a T. Unlike take_obj_back, peek_obj_back does not modify the body. If the body is not at least size_of::<T>() bytes in length, peek_obj_back returns None.

source

fn take_obj_back<T>(&mut self) -> Option<Ref<B, T>>
where T: Unaligned,

Takes an object from the back of the buffer’s body.

take_obj_back consumes size_of::<T>() bytes from the back of the buffer’s body, and interprets them as a T. After a successful call to take_obj_back::<T>(), the body is size_of::<T>() bytes shorter and, if Self: GrowBuffer, the suffix is size_of::<T>() bytes longer. If the body is not at least size_of::<T>() bytes in length, take_obj_back returns None.

source

fn take_slice_back<T>(&mut self, n: usize) -> Option<Ref<B, [T]>>
where T: Unaligned,

Takes a slice of objects from the back of the buffer’s body.

take_slice_back consumes n * size_of::<T>() bytes from the back of the buffer’s body, and interprets them as a [T] with n elements. After a successful call to take_slice_back::<T>(), the body is n * size_of::<T>() bytes shorter and, if Self: GrowBuffer, the suffix is n * size_of::<T>() bytes longer. If the body is not at least n * size_of::<T>() bytes in length, take_slice_back returns None.

§Panics

Panics if T is a zero-sized type.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'b, 'a: 'b> BufferView<&'a [u8]> for &'b mut &'a [u8]

source§

fn len(&self) -> usize

source§

fn take_front(&mut self, n: usize) -> Option<&'a [u8]>

source§

fn take_back(&mut self, n: usize) -> Option<&'a [u8]>

source§

fn into_rest(self) -> &'a [u8]

source§

impl<'b, 'a: 'b> BufferView<&'b [u8]> for &'b mut &'a mut [u8]

source§

fn len(&self) -> usize

source§

fn take_front(&mut self, n: usize) -> Option<&'b [u8]>

source§

fn take_back(&mut self, n: usize) -> Option<&'b [u8]>

source§

fn into_rest(self) -> &'b [u8]

source§

impl<'b, 'a: 'b> BufferView<&'b mut [u8]> for &'b mut &'a mut [u8]

source§

fn len(&self) -> usize

source§

fn take_front(&mut self, n: usize) -> Option<&'b mut [u8]>

source§

fn take_back(&mut self, n: usize) -> Option<&'b mut [u8]>

source§

fn into_rest(self) -> &'b mut [u8]

Implementors§

source§

impl<'a> BufferView<&'a [u8]> for BufView<'a>

source§

impl<'a> BufferView<&'a [u8]> for EmptyBuf

source§

impl<'a> BufferView<&'a mut [u8]> for BufViewMut<'a>

source§

impl<'a> BufferView<&'a mut [u8]> for EmptyBuf