Struct Buffer

Source
pub struct Buffer { /* private fields */ }
Expand description

Buffer is a mutable byte container that is aligned to crate::ALIGNMENT bytes, and has a padding so the size of the underlying allocation is a multiple of crate::ALIGNMENT.

The alignment is because we want it to be aligned to the cacheline. The padding is because we want it to be easily usable with SIMD instructions without checking length.

Implementations§

Source§

impl Buffer

Source

pub fn new(len: usize) -> Self

Create a new buffer. This function will allocate a padded and aligned memory chunk that is able to hold the given len number of bytes. The allocated memory will be zeroed.

Doesn’t allocate if len is zero. In this case the buffer will have a dangling pointer.

§Panics

Panics if len overflows isize when padded to crate::ALIGNMENT bytes. Or if memory can’t be allocated.

Source

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

Get a pointer to the underlying memory.

The underlying memory is aligned to crate::ALIGNMENT bytes and padded to crate::ALIGNMENT bytes.

Source

pub fn as_mut_ptr(&mut self) -> *mut u8

Get a mutable pointer to the underlying memory.

The underlying memory is aligned to crate::ALIGNMENT bytes and padded to crate::ALIGNMENT bytes.

Source

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

Get a slice to the underlying memory

Source

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

Get a mutable slice to the underlying memory

Source

pub fn cold_load(&mut self, src: &[u8])

Loads data from src to the underlying memory. Lenghth of self must be greater than or equal to the length of src

This might be faster than the regular memcopy, especially for large copies. Because it bypasses the CPU cache when writing if possible. This is only advantageus if the user won’t be reading the memory stored in self, for example it can be good when reading bulk data from disk but not so much if summing small/medium (fits in CPU cache) sized integer arrays and then doing other computation with them.

§Panics

Panics if self.len() < src.len()

Source

pub fn len(&self) -> usize

Length of the buffer. Keep in mind that the underlying memory is padded to crate::ALIGNMENT bytes so might be bigger than the returned value.

Source

pub fn is_empty(&self) -> bool

Returns if length of buffer is zero

Source

pub fn from_slice(src: &[u8]) -> Self

Create a Buffer from given slice.

Source

pub fn from_slice_cold(src: &[u8]) -> Self

Similar to Self::from_slice but bypasses CPU cache for writes if possible.

See Self::cold_load for tradeoffs.

Source

pub fn into_ref(self) -> BufferRef

Convert this buffer into a reference for zero copy shared usage.

Trait Implementations§

Source§

impl Clone for Buffer

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Drop for Buffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Buffer

Source§

impl Sync for Buffer

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.