Slab

Trait Slab 

Source
pub unsafe trait Slab {
    // Required methods
    fn base_ptr(&self) -> *const u8;
    fn base_ptr_mut(&mut self) -> *mut u8;
    fn size(&self) -> usize;

    // Provided methods
    fn as_maybe_uninit_bytes(&self) -> &[MaybeUninit<u8>] { ... }
    fn as_maybe_uninit_bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] { ... }
    unsafe fn assume_initialized_as_bytes(&self) -> &[u8]  { ... }
    unsafe fn assume_initialized_as_bytes_mut(&mut self) -> &mut [u8]  { ... }
    unsafe fn assume_range_initialized_as_bytes<R>(&self, range: R) -> &[u8] 
       where R: SliceIndex<[MaybeUninit<u8>], Output = [MaybeUninit<u8>]> { ... }
    unsafe fn assume_range_initialized_as_bytes_mut<R>(
        &mut self,
        range: R,
    ) -> &mut [u8] 
       where R: SliceIndex<[MaybeUninit<u8>], Output = [MaybeUninit<u8>]> { ... }
}
Expand description

Represents a contiguous piece of a single allocation with some layout that is used as a data copying destination. May be wholly or partially uninitialized.

This trait is basically equivalent to implementing Deref/DerefMut with Target = [MaybeUninit<u8>] in terms of safety requirements. It is a separate trait for the extra flexibility having a trait we own provides: namely, the ability to implement it on foreign types.

§Safety

Implementors of this trait must ensure these guarantees:

  • The memory range represented by base_ptr and size may be wholly or partially uninitialized
  • base_ptr must point to a valid, single allocation of at least size bytes.
  • size must not be greater than isize::MAX

Assume the lifetime of a shared borrow of self is named 'a:

  • base_ptr must be valid for 'a
  • base_ptr must not be mutably aliased for 'a
    • It is necessary but not sufficient for this requirement that no outside mutable references may exist to its data, even if they are unused by user code.

Assume the lifetime of a mutable borrow of self is named 'a:

  • base_ptr_mut must be valid for 'a
  • base_ptr_mut must not be aliased at all for 'a
    • It is necessary but not sufficient for this requirement that no outside references may exist to its data, even if they are unused by user code.

Also see the crate-level safety documentation.

Required Methods§

Source

fn base_ptr(&self) -> *const u8

Get a pointer to the beginning of the allocation represented by self.

Source

fn base_ptr_mut(&mut self) -> *mut u8

Get a pointer to the beginning of the allocation represented by self.

Source

fn size(&self) -> usize

Get the size of the allocation represented by self.

Provided Methods§

Source

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

Interpret a portion of self as a slice of MaybeUninit<u8>. This is likely not incredibly useful, you probably want to use Slab::as_maybe_uninit_bytes_mut

Source

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

Interpret a portion of self as a mutable slice of MaybeUninit<u8>.

Source

unsafe fn assume_initialized_as_bytes(&self) -> &[u8]

Interpret self as a byte slice. This assumes that all bytes in self are initialized.

§Safety

Assuming that the safety guarantees for creating self were followed, the only extra requirement for this to be safe is that all memory within the range of self must be initialized. If any bytes within this range are not initialized, using this function is instantly undefined behavior, even if you do noting with the result.

Also see the crate-level Safety documentation for more.

Source

unsafe fn assume_initialized_as_bytes_mut(&mut self) -> &mut [u8]

Interpret self as a mutable byte slice. This assumes that all bytes in self are initialized.

§Safety

Assuming that the safety guarantees for creating self were followed, the only extra requirement for this to be safe is that all memory within the range of self must be initialized. If any bytes within this range are not initialized, using this function is instantly undefined behavior, even if you do noting with the result.

Also see the crate-level Safety documentation for more.

Source

unsafe fn assume_range_initialized_as_bytes<R>(&self, range: R) -> &[u8]
where R: SliceIndex<[MaybeUninit<u8>], Output = [MaybeUninit<u8>]>,

Interpret a range of self as a byte slice. This assumes that all bytes within range are initialized.

In the future, this will hopefully not be needed as this operation will be equivalent to something like self.as_maybe_uninit_bytes_mut()[range].assume_init(), but the core/std implementation for this is still being scaffolded.

§Safety

Assuming that the safety guarantees for creating self were followed, the only extra requirement for this to be safe is that all memory within range must be initialized. If any bytes within this range are not initialized, using this function is instantly undefined behavior, even if you do noting with the result.

Also see the crate-level Safety documentation for more.

Source

unsafe fn assume_range_initialized_as_bytes_mut<R>( &mut self, range: R, ) -> &mut [u8]
where R: SliceIndex<[MaybeUninit<u8>], Output = [MaybeUninit<u8>]>,

Interpret a range of self as a mutable byte slice. This assumes that all bytes within range are initialized.

In the future, this will hopefully not be needed as this operation will be equivalent to something like self.as_maybe_uninit_bytes_mut()[range].assume_init(), but the core/std implementation for this is still being scaffolded.

§Safety

Assuming that the safety guarantees for creating self were followed, the only extra requirement for this to be safe is that all memory within range must be initialized. If any bytes within this range are not initialized, using this function is instantly undefined behavior, even if you do noting with the result.

Also see the crate-level Safety documentation for more.

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 Slab for [MaybeUninit<u8>]

Source§

fn base_ptr(&self) -> *const u8

Source§

fn base_ptr_mut(&mut self) -> *mut u8

Source§

fn size(&self) -> usize

Implementors§