Skip to main content

TensorMut

Struct TensorMut 

Source
pub struct TensorMut<'a> { /* private fields */ }
Expand description

A mutable view of a TensorFlow Lite tensor.

TensorMut provides all the read-only operations of Tensor plus mutable data access via TensorMut::as_mut_slice and TensorMut::copy_from_slice.

The pointer is stored as NonNull because the C API returns *mut TfLiteTensor for input tensors, which must be non-null after successful interpreter creation.

Implementations§

Source§

impl TensorMut<'_>

Source

pub fn tensor_type(&self) -> TensorType

Returns the element data type of this tensor.

If the C API returns a type value not represented by TensorType, this method defaults to TensorType::NoType.

Source

pub fn name(&self) -> &str

Returns the name of this tensor as a string slice.

Returns "<invalid-utf8>" if the C API returns a name that is not valid UTF-8.

Source

pub fn num_dims(&self) -> Result<usize>

Returns the number of dimensions (rank) of this tensor.

§Errors

Returns an error if the tensor does not have its dimensions set (the C API returns -1).

Source

pub fn dim(&self, index: usize) -> Result<usize>

Returns the size of the index-th dimension.

§Errors

Returns an error if index is out of bounds (>= num_dims).

Source

pub fn shape(&self) -> Result<Vec<usize>>

Returns the full shape of this tensor as a Vec<usize>.

§Errors

Returns an error if the tensor dimensions are not set.

Source

pub fn byte_size(&self) -> usize

Returns the total number of bytes required to store this tensor’s data.

Source

pub fn volume(&self) -> Result<usize>

Returns the total number of elements in this tensor (product of all dimensions).

§Errors

Returns an error if the tensor dimensions are not set.

Source

pub fn quantization_params(&self) -> QuantizationParams

Returns the affine quantization parameters for this tensor.

Source

pub fn as_slice<T: Copy>(&self) -> Result<&[T]>

Returns an immutable slice over the tensor data, interpreted as elements of type T.

The slice length equals TensorMut::volume. The caller must ensure that T matches the tensor’s actual element type (e.g., f32 for a Float32 tensor, u8 for a UInt8 tensor).

§Errors

Returns an error if:

  • size_of::<T>() * volume exceeds TensorMut::byte_size
  • The underlying data pointer is null (tensor not yet allocated)
Source

pub fn as_mut_slice<T: Copy>(&mut self) -> Result<&mut [T]>

Returns a mutable slice over the tensor data, interpreted as elements of type T.

The slice length equals TensorMut::volume. The caller must ensure that T matches the tensor’s actual element type (e.g., f32 for a Float32 tensor, u8 for a UInt8 tensor).

§Errors

Returns an error if:

  • size_of::<T>() * volume exceeds TensorMut::byte_size
  • The underlying data pointer is null (tensor not yet allocated)
Source

pub fn copy_from_slice<T: Copy>(&mut self, data: &[T]) -> Result<()>

Copies the contents of data into this tensor’s buffer.

This is a convenience wrapper around TensorMut::as_mut_slice that copies elements from the provided slice into the tensor.

§Errors

Returns an error if:

  • The tensor cannot be mapped as a mutable slice of T
  • data.len() does not match TensorMut::volume

Trait Implementations§

Source§

impl Debug for TensorMut<'_>

Formats the tensor as "name: 1x224x224x3 Float32".

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for TensorMut<'_>

Displays the tensor as "name: 1x224x224x3 Float32".

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for TensorMut<'a>

§

impl<'a> !RefUnwindSafe for TensorMut<'a>

§

impl<'a> !Send for TensorMut<'a>

§

impl<'a> !Sync for TensorMut<'a>

§

impl<'a> Unpin for TensorMut<'a>

§

impl<'a> UnsafeUnpin for TensorMut<'a>

§

impl<'a> !UnwindSafe for TensorMut<'a>

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.