Storage

Trait Storage 

Source
pub unsafe trait Storage {
    type Elem;

    const MUTABLE: bool;

    // Required methods
    fn len(&self) -> usize;
    fn as_ptr(&self) -> *const Self::Elem;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    unsafe fn get(&self, offset: usize) -> Option<&Self::Elem> { ... }
    unsafe fn get_unchecked(&self, offset: usize) -> &Self::Elem { ... }
    fn slice(&self, range: Range<usize>) -> ViewData<'_, Self::Elem> { ... }
    fn view(&self) -> ViewData<'_, Self::Elem> { ... }
    unsafe fn as_slice(&self) -> &[Self::Elem] { ... }
}
Expand description

Trait for backing storage used by tensors and views.

Mutable tensors have storage which also implement StorageMut.

This specifies a contiguous array of elements in memory, as a pointer and a length. The storage may be owned or borrowed. For borrowed storage, there may be other storage whose ranges overlap. This is necessary to support mutable views of non-contiguous tensors (eg. independent columns of a matrix, whose data is stored in row-major order).

§Safety

Since different storage objects can have memory ranges that overlap, it is up to the caller to ensure that mutable tensors cannot logically overlap any other tensors. In other words, whenever a mutable tensor is split or sliced or iterated, it should not be possible to get duplicate mutable references to the same elements from those views.

Implementations of this trait must ensure that the as_ptr and len methods define a valid range of memory within the same allocated object, which is correctly aligned for the Elem type. For the case where the storage is contiguous, these requirements are the same as slice::from_raw_parts.

The MUTABLE associated const must be true if the storage also implements StorageMut.

Required Associated Constants§

Source

const MUTABLE: bool

True if this storage allows mutable access via StorageMut. This is used to determine if a layout can be safely used with a storage. Layouts where multiple indices map to the same offset must not be used with mutable storage.

Required Associated Types§

Source

type Elem

The element type.

Required Methods§

Source

fn len(&self) -> usize

Return the number of elements in the storage.

Source

fn as_ptr(&self) -> *const Self::Elem

Return a pointer to the first element in the storage.

Provided Methods§

Source

fn is_empty(&self) -> bool

Return true if the storage contains no elements.

Source

unsafe fn get(&self, offset: usize) -> Option<&Self::Elem>

Return the element at a given offset, or None if offset >= self.len().

§Safety
  • The caller must ensure that no mutable references to the same element can be created.
Source

unsafe fn get_unchecked(&self, offset: usize) -> &Self::Elem

Return a reference to the element at offset.

§Safety

This has the same safety requirements as get plus the caller must ensure that offset < len.

Source

fn slice(&self, range: Range<usize>) -> ViewData<'_, Self::Elem>

Return a view of a sub-region of the storage.

Panics if the range is out of bounds.

Source

fn view(&self) -> ViewData<'_, Self::Elem>

Return an immutable view of this storage.

Source

unsafe fn as_slice(&self) -> &[Self::Elem]

Return the contents of the storage as a slice.

§Safety

The caller must ensure that no mutable references exist to any element in the storage.

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<T> Storage for Vec<T>

Source§

const MUTABLE: bool = true

Source§

type Elem = T

Source§

fn len(&self) -> usize

Source§

fn as_ptr(&self) -> *const T

Implementors§

Source§

impl<T> Storage for CowData<'_, T>

Source§

const MUTABLE: bool = false

Source§

type Elem = T

Source§

impl<T> Storage for ViewData<'_, T>

Source§

const MUTABLE: bool = false

Source§

type Elem = T

Source§

impl<T> Storage for ViewMutData<'_, T>

Source§

const MUTABLE: bool = true

Source§

type Elem = T