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§
Sourceconst MUTABLE: bool
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§
Required Methods§
Provided Methods§
Sourceunsafe fn get(&self, offset: usize) -> Option<&Self::Elem>
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.
Sourceunsafe fn get_unchecked(&self, offset: usize) -> &Self::Elem
unsafe fn get_unchecked(&self, offset: usize) -> &Self::Elem
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.