pub struct DataBuffer<T> { /* private fields */ }Expand description
Data storage for tensor elements.
Abstracts over ownership: data may be Rust-owned (Vec<T>) or
externally-owned (e.g., imported via DLPack with a release callback).
Shape and stride metadata are NOT stored here — they live on
Tensor<T>.
§Clone behavior
Cloning an externally-owned buffer performs a deep copy into a new
Rust-owned Vec<T>. The release callback cannot be cloned; the clone
is always Rust-owned.
Implementations§
Source§impl<T> DataBuffer<T>
impl<T> DataBuffer<T>
Sourcepub unsafe fn from_external(
ptr: *const T,
len: usize,
release: impl FnOnce() + Send + 'static,
) -> Self
pub unsafe fn from_external( ptr: *const T, len: usize, release: impl FnOnce() + Send + 'static, ) -> Self
Create a buffer from externally-owned data with a release callback.
§Safety
ptrmust point to a valid, properly aligned allocation of at leastlenelements of typeT.- The allocation must remain valid until the release callback is invoked
(which happens when this
DataBufferis dropped). - The release callback must correctly notify the external owner.
§Examples
ⓘ
use tenferro_tensor::DataBuffer;
let data = vec![1.0, 2.0, 3.0];
let ptr = data.as_ptr();
let len = data.len();
let buf = unsafe {
DataBuffer::from_external(ptr, len, move || drop(data))
};
assert!(!buf.is_owned());Sourcepub fn as_mut_slice(&mut self) -> Option<&mut [T]>
pub fn as_mut_slice(&mut self) -> Option<&mut [T]>
Returns the raw data as a mutable slice, if Rust-owned.
Returns None for externally-owned buffers (they are read-only
through tenferro).
Trait Implementations§
Source§impl<T: Copy> Clone for DataBuffer<T>
impl<T: Copy> Clone for DataBuffer<T>
Source§impl<T> Drop for DataBuffer<T>
impl<T> Drop for DataBuffer<T>
impl<T: Send> Send for DataBuffer<T>
impl<T: Sync> Sync for DataBuffer<T>
Auto Trait Implementations§
impl<T> Freeze for DataBuffer<T>
impl<T> !RefUnwindSafe for DataBuffer<T>
impl<T> Unpin for DataBuffer<T>where
T: Unpin,
impl<T> !UnwindSafe for DataBuffer<T>
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
Mutably borrows from an owned value. Read more