pub struct ZeroCopyTensor<T> { /* private fields */ }Expand description
Zero-copy tensor wrapper that avoids unnecessary memory allocation
This struct provides a view into existing memory without copying data. It can work with borrowed slices or take ownership of allocated memory.
Implementations§
Source§impl<T> ZeroCopyTensor<T>
impl<T> ZeroCopyTensor<T>
Sourcepub unsafe fn from_raw_parts(
data_ptr: *const T,
shape: Vec<usize>,
stride: Vec<usize>,
) -> Self
pub unsafe fn from_raw_parts( data_ptr: *const T, shape: Vec<usize>, stride: Vec<usize>, ) -> Self
Create a zero-copy tensor from existing data without copying
§Safety
This function is unsafe because it directly uses raw pointers. The caller must ensure:
data_ptris a valid pointer to a memory region that contains at leastcapacityelements- The memory region remains valid for the lifetime of the ZeroCopyTensor
- The memory is properly aligned for type T
- The shape and stride parameters correctly describe the tensor layout
- No other code mutates the memory region while this tensor exists
Sourcepub fn from_slice(data: &[T], shape: Vec<usize>) -> Self
pub fn from_slice(data: &[T], shape: Vec<usize>) -> Self
Create a zero-copy tensor from a slice
This creates a view into the provided slice without copying data. The slice must remain valid for the lifetime of the tensor.
Sourcepub fn from_vec(data: Vec<T>, shape: Vec<usize>) -> Self
pub fn from_vec(data: Vec<T>, shape: Vec<usize>) -> Self
Create a zero-copy tensor by taking ownership of a Vec
This transfers ownership of the Vec’s memory to the tensor, avoiding the need to copy data.
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Get data as a slice
§Safety
This is safe as long as the tensor was constructed properly and the underlying memory remains valid.
Sourcepub fn slice_view(&self, ranges: &[(usize, usize)]) -> Result<ZeroCopyTensor<T>>
pub fn slice_view(&self, ranges: &[(usize, usize)]) -> Result<ZeroCopyTensor<T>>
Create a view into a subregion without copying data
This creates a new tensor that views a slice of the current tensor. The data is not copied, only the view parameters are adjusted.
Trait Implementations§
Source§impl<T> Drop for ZeroCopyTensor<T>
impl<T> Drop for ZeroCopyTensor<T>
impl<T: Send> Send for ZeroCopyTensor<T>
impl<T: Sync> Sync for ZeroCopyTensor<T>
Auto Trait Implementations§
impl<T> Freeze for ZeroCopyTensor<T>
impl<T> RefUnwindSafe for ZeroCopyTensor<T>where
T: RefUnwindSafe,
impl<T> Unpin for ZeroCopyTensor<T>
impl<T> UnsafeUnpin for ZeroCopyTensor<T>
impl<T> UnwindSafe for ZeroCopyTensor<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.