Struct Slice

Source
pub struct Slice {
    pub index: u8,
    pub shared_state: SharedState,
    pub owned: Option<Vec<u8>>,
    /* private fields */
}
Expand description

A contiguous block of memory, either preallocated or dynamically allocated.

It serves as a lightweight handle to a memory buffer, allowing for direct manipulation and shared access. It can either hold a reference to a preallocated memory block or own a dynamically allocated buffer (via Vec<u8>).

Fields§

§index: u8

Unique identifier (index) of the slice in the shared memory pool.

When in back or front mode, tracks the slice within the pool and manages memory reuse. It allows for quick identification of slices when freeing or reassigning memory. If in alloc mode, it is set to IGNORE_INDEX.

§shared_state: SharedState

Shared state of the memory pool.

When in back or front mode, tracks how many slices are currently in use and ensures proper synchronization of memory access across multiple contexts.

§owned: Option<Vec<u8>>

Optional dynamically allocated buffer.

If present, the slice owns the memory and is responsible for managing its lifecycle. If None, the buffer pool is in back or front mode and the slice points to memory managed by the memory pool. Is Some(Vec<u8>) when in alloc mode.

Implementations§

Source§

impl Slice

Source

pub fn len(&self) -> usize

Returns the length of the slice in bytes.

If the slice owns its memory (owned), it returns the length of the owned buffer. If the slice does not own the memory, it returns 0.

Source

pub fn is_empty(&self) -> bool

Checks if the slice is empty.

Returns true if the slice is empty, i.e., it has no data. Otherwise, returns false.

Trait Implementations§

Source§

impl AsMut<[u8]> for Slice

Source§

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

Converts the Slice into a mutable slice of bytes (&mut [u8]).

Returns the owned buffer if present, otherwise converts the raw pointer and length into a mutable slice.

Source§

impl AsRef<[u8]> for Slice

Source§

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

Converts the Slice into an immutable slice of bytes (&[u8]).

Returns the owned buffer if present, otherwise converts the raw pointer and length into an immutable slice.

Source§

impl Clone for Slice

Source§

fn clone(&self) -> Slice

Returns a duplicate 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 Debug for Slice

Source§

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

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

impl Drop for Slice

Source§

fn drop(&mut self)

Toggles the shared state when the slice is dropped, allowing the memory to be reused.

In debug mode, it also tracks the mode of the slice when it is dropped.

Source§

impl From<Vec<u8>> for Slice

Source§

fn from(v: Vec<u8>) -> Self

Creates a Slice from a Vec<u8>, taking ownership of the vector.

Initializes the Slice with the vector’s pointer and sets the length to 0.

Source§

impl Index<Range<usize>> for Slice

Source§

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

Provides immutable slicing access to the specified range within the Slice.

Uses as_ref to get a reference to the underlying buffer and returns the specified range.

Source§

type Output = [u8]

The returned type after indexing.
Source§

impl Index<RangeFrom<usize>> for Slice

Source§

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

Provides immutable slicing access to a range starting from the given index.

Uses as_ref to get a reference to the underlying buffer and returns the range.

Source§

type Output = [u8]

The returned type after indexing.
Source§

impl Index<RangeFull> for Slice

Source§

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

Provides immutable access to the entire range of the Slice.

Uses as_ref to get a reference to the entire underlying buffer.

Source§

type Output = [u8]

The returned type after indexing.
Source§

impl Index<usize> for Slice

Source§

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

Provides immutable indexing access to the Slice at the specified position.

Uses as_ref to get a reference to the underlying buffer and returns the byte at the index.

Source§

type Output = u8

The returned type after indexing.
Source§

impl IndexMut<Range<usize>> for Slice

Source§

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

Provides mutable slicing access to the specified range within the Slice.

Uses as_mut to get a mutable reference to the underlying buffer and returns the specified range.

Source§

impl IndexMut<RangeFrom<usize>> for Slice

Source§

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

Provides mutable slicing access to a range starting from the given index.

Uses as_mut to get a mutable reference to the underlying buffer and returns the range.

Source§

impl IndexMut<usize> for Slice

Source§

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

Provides mutable indexing access to the Slice at the specified position.

Uses as_mut to get a mutable reference to the underlying buffer and returns the byte at the index.

Source§

impl Send for Slice

Allows Slice to be safely transferred between threads.

Slice contains a raw pointer (*mut u8), so Rust cannot automatically implement Send. The unsafe block asserts that memory access is thread-safe, relaying on SharedState and atomic operations to prevent data races.

Auto Trait Implementations§

§

impl Freeze for Slice

§

impl RefUnwindSafe for Slice

§

impl !Sync for Slice

§

impl Unpin for Slice

§

impl UnwindSafe for Slice

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> Same for T

Source§

type Output = T

Should always be Self
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.