pub trait Slice: MemoryDescriptor + 'static {
// Required method
unsafe fn as_slice(&self) -> Result<&[u8], StorageError>;
// Provided methods
fn slice(&self, offset: usize, len: usize) -> Result<&[u8], StorageError> { ... }
fn as_slice_typed<T: Sized>(&self) -> Result<&[T], StorageError> { ... }
fn slice_typed<T: Sized>(
&self,
offset: usize,
len: usize,
) -> Result<&[T], StorageError> { ... }
}Expand description
Extension trait for storage types that support slicing operations
Required Methods§
Sourceunsafe fn as_slice(&self) -> Result<&[u8], StorageError>
unsafe fn as_slice(&self) -> Result<&[u8], StorageError>
Returns an immutable byte slice view of the entire storage region
§Safety
This is an unsafe method. The caller must ensure:
- The memory region remains valid for the lifetime of the returned slice
- The memory region is properly initialized
- No concurrent mutable access occurs while the slice is in use
- The memory backing this storage remains valid (implementors with owned memory satisfy this, but care must be taken with unowned memory regions)
Provided Methods§
Sourcefn slice(&self, offset: usize, len: usize) -> Result<&[u8], StorageError>
fn slice(&self, offset: usize, len: usize) -> Result<&[u8], StorageError>
Returns an immutable byte slice view of a subregion
§Arguments
offset- Offset in bytes from the start of the storagelen- Number of bytes to slice
§Safety
The caller must ensure:
- offset + len <= self.size()
- The memory region is valid and initialized
- No concurrent mutable access occurs while the slice is in use
Sourcefn as_slice_typed<T: Sized>(&self) -> Result<&[T], StorageError>
fn as_slice_typed<T: Sized>(&self) -> Result<&[T], StorageError>
Returns a typed immutable slice view of the entire storage region
§Safety
The caller must ensure:
- The memory region is valid and initialized
- The memory is properly aligned for type T
- The size is a multiple of
size_of::<T>() - No concurrent mutable access occurs while the slice is in use
- The data represents valid values of type T
Sourcefn slice_typed<T: Sized>(
&self,
offset: usize,
len: usize,
) -> Result<&[T], StorageError>
fn slice_typed<T: Sized>( &self, offset: usize, len: usize, ) -> Result<&[T], StorageError>
Returns a typed immutable slice view of a subregion
§Arguments
offset- Offset in bytes from the start of the storagelen- Number of elements of type T to slice
§Safety
The caller must ensure:
- offset + (len * size_of::
()) <= self.size() - offset is properly aligned for type T
- The memory region is valid and initialized
- No concurrent mutable access occurs while the slice is in use
- The data represents valid values of type T
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.