Struct Buffer

Source
pub struct Buffer<T: Sized> { /* private fields */ }
Expand description

Static buffer to raw bytes

The size of the storage must be known at compile time, therefore it is suitable only for non-dynamic storages.

While write semantics are pretty obvious, read behaviour is more complicated due to it being a static buffer.

When performing ReadBuf::read memory is always being read from the beginning. So as with any other implementation read leads to consumption. But as this buffer is single chunk of static memory, such operation will require to shift already written bytes to the beginning (meaning each ReadBuf::consume involves a memmove unless consumed len is not equal to current len)

In general it would be more effective to access memory as slice and then consume it, if needed.

Implementations§

Source§

impl<S: Sized> Buffer<S>

Source

pub const fn new() -> Self

Creates new instance

Source

pub const fn into_circular(self) -> Ring<S>

Transforms buffer into ring buffer.

Source

pub const unsafe fn from_parts(inner: MaybeUninit<S>, cursor: usize) -> Self

Creates new instance from parts.

cursor - number of elements written. It is user responsibility to make sure it is not over actual capacity

Source

pub const fn into_parts(self) -> (MaybeUninit<S>, usize)

Splits buffer into parts.

Source

pub const fn as_ptr(&self) -> *const u8

Returns pointer to the beginning of underlying buffer

Source

pub const fn remaining(&self) -> usize

Returns number of bytes left (not written yet)

Source

pub fn as_slice(&self) -> &[u8]

Returns slice to already written data.

Source

pub fn as_mut_slice(&mut self) -> &mut [u8]

Returns mutable slice to already written data.

Source

pub fn truncate(&mut self, cursor: usize)

Shortens the buffer.

Does nothing if new cursor is after current position.

Source

pub unsafe fn set_len(&mut self, cursor: usize)

Changes written length, without writing.

When used, user must guarantee that these bytes are written.

Source

pub const fn capacity() -> usize

Returns buffer overall capacity.

Source

pub const fn len(&self) -> usize

Returns number of bytes written.

Trait Implementations§

Source§

impl<S: Sized> AsRef<[u8]> for Buffer<S>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<S: Sized> Buf for Buffer<S>

Source§

fn capacity(&self) -> usize

Returns size of the underlying memory in the buffer.
Source§

fn len(&self) -> usize

Returns number of elements inside the buffer.
Source§

fn iter(&self) -> Iter<'_, Self>

Returns iterator over elements inside the buffer.
Source§

fn iter_mut(&mut self) -> IterMut<'_, Self>

Returns mutable iterator over elements inside the buffer.
Source§

impl<S: Sized> ContBuf for Buffer<S>

Source§

fn as_read_slice(&self) -> &[u8]

Returns slice of bytes that can be read.
Source§

fn as_read_slice_mut(&mut self) -> &mut [u8]

Returns mutable slice of bytes that can be read.
Source§

fn as_write_slice(&mut self) -> &mut [MaybeUninit<u8>]

Returns slice of bytes that can be written (i.e. not written yet).
Source§

impl<S: Sized> Debug for Buffer<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Sized> Index<usize> for Buffer<S>

Source§

type Output = u8

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<S: Sized> IndexMut<usize> for Buffer<S>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<S: Sized> ReadBuf for Buffer<S>

Source§

unsafe fn consume(&mut self, step: usize)

Moves cursor, considering bytes as consumed.
Source§

unsafe fn read(&mut self, ptr: *mut u8, size: usize)

Low level read function, that consumes available bytes up to size. Read more
Source§

fn available(&self) -> usize

Returns number of bytes left Read more
Source§

fn read_slice(&mut self, bytes: &mut [u8]) -> usize

Reads available bytes into slice
Source§

impl<S: Sized> WriteBuf for Buffer<S>

Source§

fn remaining(&self) -> usize

Returns number of bytes left Read more
Source§

unsafe fn advance(&mut self, step: usize)

Moves cursor, considering bytes written.
Source§

unsafe fn write(&mut self, ptr: *const u8, size: usize)

Low level write method, which copies data from pointer up to size. Read more
Source§

fn write_slice(&mut self, bytes: &[u8]) -> usize

Writes supplied slice into the buffer, returning number of written bytes. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Buffer<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Buffer<T>
where T: RefUnwindSafe,

§

impl<T> Send for Buffer<T>
where T: Send,

§

impl<T> Sync for Buffer<T>
where T: Sync,

§

impl<T> Unpin for Buffer<T>
where T: Unpin,

§

impl<T> UnwindSafe for Buffer<T>
where T: UnwindSafe,

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> ReadBufExt for T
where T: ReadBuf,

Source§

fn read_value<T: Copy + Sized>(&mut self, val: &mut MaybeUninit<T>) -> usize

Reads value into storage. 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<T> WriteBufExt for T
where T: WriteBuf,

Source§

fn write_value<T: Copy + Sized>(&mut self, val: &T) -> usize

Writes supplied value by performing bit copy, advancing length and returning number of bytes written. Read more