packet

Trait BufferView

Source
pub trait BufferView<B: SplitByteSlice>: 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 + KnownLayout + Immutable + Unaligned { ... } fn take_obj_front<T>(&mut self) -> Option<Ref<B, T>> where T: KnownLayout + Immutable + Unaligned { ... } fn take_slice_front<T>(&mut self, n: usize) -> Option<Ref<B, [T]>> where T: Immutable + Unaligned { ... } fn peek_obj_back<T>(&mut self) -> Option<&T> where T: FromBytes + KnownLayout + Immutable + Unaligned { ... } fn take_obj_back<T>(&mut self) -> Option<Ref<B, T>> where T: Immutable + KnownLayout + Unaligned { ... } fn take_slice_back<T>(&mut self, n: usize) -> Option<Ref<B, [T]>> where T: Immutable + 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 + KnownLayout + Immutable + 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: KnownLayout + Immutable + 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: Immutable + 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 + KnownLayout + Immutable + 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: Immutable + KnownLayout + 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: Immutable + 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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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

Source§

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