Trait Storage

Source
pub trait Storage<T> {
    // Required methods
    fn capacity(&self) -> usize;
    fn slice(&self, range: Range<usize>) -> &[T];
    unsafe fn slice_mut_unchecked(&self, range: Range<usize>) -> &mut [T];

    // Provided method
    fn slice_mut(&mut self, range: Range<usize>) -> &mut [T] { ... }
}
Expand description

Storage for a contiguous array of items allowing mutable access to multiple ranges as long as they don’t overlap.

This is implemented for any type that implements AsRef<[UnsafeCell<T>]>. No matter the backing store, elements must be wrapped in some kind of cell, as this is the only safe way to get a mutable reference to something through a non-mutable reference.

This is a low-level trait, and must be implemented carefully.

Required Methods§

Source

fn capacity(&self) -> usize

Gets the capacity of the array, or in other words the upper bound for ranges passed into slice and slice_mut before they will panic.

Source

fn slice(&self, range: Range<usize>) -> &[T]

Gets a slice of elements in the range provided. The length of the slice will always match the length of the range.

§Panics

This function may panic if the range extends beyond the length of the array returned by capacity.

Source

unsafe fn slice_mut_unchecked(&self, range: Range<usize>) -> &mut [T]

Gets a mutable slice of elements in the range provided. The length of the slice will always match the length of the range. This function is unchecked and unsafe because it does not ensure there are no other references overlapping with the range, which is against Rust’s borrowing rules and is very unsafe!

This function does ensure the range is valid, however.

§Safety

No other slices that overlap with the range must exist for the duration of this slice’s lifetime.

§Panics

This function may panic if the range extends beyond the length of the array returned by capacity.

Provided Methods§

Source

fn slice_mut(&mut self, range: Range<usize>) -> &mut [T]

Gets a mutable slice of elements in the range provided. The length of the slice will always match the length of the range.

§Panics

This function may panic if the range extends beyond the length of the array returned by capacity.

Implementors§

Source§

impl<T, A: AsRef<[UnsafeCell<T>]>> Storage<T> for A