DataBuffer

Struct DataBuffer 

Source
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>

Source

pub fn from_vec(v: Vec<T>) -> Self

Create a buffer from an owned Vec<T>.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0, 2.0, 3.0]);
assert_eq!(buf.len(), 3);
assert!(buf.is_owned());
Source

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
  • ptr must point to a valid, properly aligned allocation of at least len elements of type T.
  • The allocation must remain valid until the release callback is invoked (which happens when this DataBuffer is 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());
Source

pub fn as_slice(&self) -> &[T]

Returns the raw data as a slice.

Source

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).

Source

pub fn len(&self) -> usize

Returns the number of elements in the buffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the buffer has no elements.

Source

pub fn is_owned(&self) -> bool

Returns true if the buffer is Rust-owned (backed by Vec<T>).

Source

pub fn as_ptr(&self) -> *const T

Returns a raw pointer to the data.

Trait Implementations§

Source§

impl<T: Copy> Clone for DataBuffer<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Drop for DataBuffer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send> Send for DataBuffer<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.