Skip to main content

TensorDyn

Enum TensorDyn 

Source
#[non_exhaustive]
pub enum TensorDyn { U8(Tensor<u8>), I8(Tensor<i8>), U16(Tensor<u16>), I16(Tensor<i16>), U32(Tensor<u32>), I32(Tensor<i32>), U64(Tensor<u64>), I64(Tensor<i64>), F16(Tensor<f16>), F32(Tensor<f32>), F64(Tensor<f64>), }
Expand description

Type-erased tensor. Wraps a Tensor<T> with runtime element type.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

U8(Tensor<u8>)

Unsigned 8-bit integer tensor.

§

I8(Tensor<i8>)

Signed 8-bit integer tensor.

§

U16(Tensor<u16>)

Unsigned 16-bit integer tensor.

§

I16(Tensor<i16>)

Signed 16-bit integer tensor.

§

U32(Tensor<u32>)

Unsigned 32-bit integer tensor.

§

I32(Tensor<i32>)

Signed 32-bit integer tensor.

§

U64(Tensor<u64>)

Unsigned 64-bit integer tensor.

§

I64(Tensor<i64>)

Signed 64-bit integer tensor.

§

F16(Tensor<f16>)

16-bit floating-point tensor.

§

F32(Tensor<f32>)

32-bit floating-point tensor.

§

F64(Tensor<f64>)

64-bit floating-point tensor.

Implementations§

Source§

impl TensorDyn

Source

pub fn dtype(&self) -> DType

Return the runtime element type discriminant.

Source

pub fn shape(&self) -> &[usize]

Return the tensor shape.

Source

pub fn name(&self) -> String

Return the tensor name.

Source

pub fn format(&self) -> Option<PixelFormat>

Return the pixel format (None if not an image tensor).

Source

pub fn width(&self) -> Option<usize>

Return the image width (None if not an image tensor).

Source

pub fn height(&self) -> Option<usize>

Return the image height (None if not an image tensor).

Source

pub fn size(&self) -> usize

Return the total size of this tensor in bytes.

Source

pub fn memory(&self) -> TensorMemory

Return the memory allocation type.

Source

pub fn reshape(&mut self, shape: &[usize]) -> Result<()>

Reshape this tensor. Total element count must remain the same.

Source

pub fn set_format(&mut self, format: PixelFormat) -> Result<()>

Attach pixel format metadata to this tensor.

Validates that the tensor’s shape is compatible with the format’s layout (packed, planar, or semi-planar).

§Arguments
  • format - The pixel format to attach
§Returns

Ok(()) on success, with the format stored as metadata on the tensor.

§Errors

Returns Error::InvalidShape if the tensor shape doesn’t match the expected layout for the given format.

Source

pub fn with_format(self, format: PixelFormat) -> Result<Self>

Attach pixel format metadata, consuming and returning self.

Enables builder-style chaining.

§Arguments
  • format - The pixel format to attach
§Returns

The tensor with format metadata attached.

§Errors

Returns Error::InvalidShape if the tensor shape doesn’t match the expected layout for the given format.

Source

pub fn row_stride(&self) -> Option<usize>

Row stride in bytes (None = tightly packed).

Source

pub fn effective_row_stride(&self) -> Option<usize>

Effective row stride: stored stride or computed from format and width.

Source

pub fn set_row_stride(&mut self, stride: usize) -> Result<()>

Set the row stride in bytes for externally allocated buffers with row padding.

Must be called before the tensor is first used for rendering. The format must be set before calling this method.

Source

pub fn with_row_stride(self, stride: usize) -> Result<Self>

Builder-style: set row stride, consuming and returning self.

Source

pub fn plane_offset(&self) -> Option<usize>

Byte offset within the DMA-BUF where image data starts (None = 0).

Source

pub fn set_plane_offset(&mut self, offset: usize)

Set the byte offset within the DMA-BUF where image data starts.

Source

pub fn with_plane_offset(self, offset: usize) -> Self

Builder-style: set plane offset, consuming and returning self.

Source

pub fn clone_fd(&self) -> Result<OwnedFd>

Clone the file descriptor associated with this tensor.

Source

pub fn dmabuf_clone(&self) -> Result<OwnedFd>

Clone the DMA-BUF file descriptor backing this tensor (Linux only).

§Returns

An owned duplicate of the DMA-BUF file descriptor.

§Errors
  • Error::NotImplemented if the tensor is not DMA-backed (Mem/Shm/Pbo)
  • Error::IoError if the fd clone syscall fails (e.g., fd limit reached)
Source

pub fn dmabuf(&self) -> Result<BorrowedFd<'_>>

Borrow the DMA-BUF file descriptor backing this tensor (Linux only).

§Returns

A borrowed reference to the DMA-BUF file descriptor, tied to self’s lifetime.

§Errors
  • Error::NotImplemented if the tensor is not DMA-backed
Source

pub fn is_multiplane(&self) -> bool

Return true if this tensor uses separate plane allocations.

Source

pub fn buffer_identity(&self) -> &BufferIdentity

Return the BufferIdentity of the underlying allocation.

Two TensorDyn values share a [BufferIdentity::id] iff they were produced by cloning the same allocation (e.g. through DmaTensor::try_clone). Separate imports of the same physical buffer (e.g. two from_fd calls on the same dmabuf fd) have distinct identities — use aliases if you need to detect that case.

Source

pub fn aliases(&self, other: &Self) -> bool

Return true if self and other reference the same underlying buffer.

This is the correct check for APIs that require distinct input and output tensors (e.g. ImageProcessor::draw_decoded_masks, where aliasing dst and background would cause the GL backend to read and write the same texture — undefined behaviour on most drivers).

Matching is conservative:

  1. Matching [BufferIdentity::id] → same buffer (always).
  2. Matching backing type + matching dmabuf fd number (Linux, DMA tensors only) → same buffer, even across separate from_fd imports in the same process.

Two distinct dup’d fds pointing at the same kernel dma-buf are not detected — there is no cheap way to resolve that without a round-trip through the kernel.

Source

pub fn as_u8(&self) -> Option<&Tensor<u8>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_u8_mut(&mut self) -> Option<&mut Tensor<u8>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_u8(self) -> Result<Tensor<u8>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_i8(&self) -> Option<&Tensor<i8>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_i8_mut(&mut self) -> Option<&mut Tensor<i8>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_i8(self) -> Result<Tensor<i8>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_u16(&self) -> Option<&Tensor<u16>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_u16_mut(&mut self) -> Option<&mut Tensor<u16>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_u16(self) -> Result<Tensor<u16>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_i16(&self) -> Option<&Tensor<i16>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_i16_mut(&mut self) -> Option<&mut Tensor<i16>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_i16(self) -> Result<Tensor<i16>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_u32(&self) -> Option<&Tensor<u32>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_u32_mut(&mut self) -> Option<&mut Tensor<u32>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_u32(self) -> Result<Tensor<u32>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_i32(&self) -> Option<&Tensor<i32>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_i32_mut(&mut self) -> Option<&mut Tensor<i32>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_i32(self) -> Result<Tensor<i32>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_u64(&self) -> Option<&Tensor<u64>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_u64_mut(&mut self) -> Option<&mut Tensor<u64>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_u64(self) -> Result<Tensor<u64>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_i64(&self) -> Option<&Tensor<i64>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_i64_mut(&mut self) -> Option<&mut Tensor<i64>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_i64(self) -> Result<Tensor<i64>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_f16(&self) -> Option<&Tensor<f16>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_f16_mut(&mut self) -> Option<&mut Tensor<f16>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_f16(self) -> Result<Tensor<f16>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_f32(&self) -> Option<&Tensor<f32>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_f32_mut(&mut self) -> Option<&mut Tensor<f32>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_f32(self) -> Result<Tensor<f32>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn as_f64(&self) -> Option<&Tensor<f64>>

Returns a shared reference to the inner tensor if the type matches.

Source

pub fn as_f64_mut(&mut self) -> Option<&mut Tensor<f64>>

Returns a mutable reference to the inner tensor if the type matches.

Source

pub fn into_f64(self) -> Result<Tensor<f64>, Self>

Unwraps the inner tensor if the type matches, otherwise returns self as Err. The Err variant is necessarily large (returns the unconsumed TensorDyn).

Source

pub fn new( shape: &[usize], dtype: DType, memory: Option<TensorMemory>, name: Option<&str>, ) -> Result<Self>

Create a type-erased tensor with the given shape and element type.

Source

pub fn from_fd( fd: OwnedFd, shape: &[usize], dtype: DType, name: Option<&str>, ) -> Result<Self>

Create a type-erased tensor from a file descriptor.

Source

pub fn image( width: usize, height: usize, format: PixelFormat, dtype: DType, memory: Option<TensorMemory>, ) -> Result<Self>

Create a type-erased image tensor.

§Arguments
  • width - Image width in pixels
  • height - Image height in pixels
  • format - Pixel format
  • dtype - Element type discriminant
  • memory - Optional memory backend (None selects the best available)
§Returns

A new TensorDyn wrapping an image tensor of the requested element type.

§Errors

Returns an error if the underlying Tensor::image call fails.

Source

pub fn image_with_stride( width: usize, height: usize, format: PixelFormat, dtype: DType, row_stride_bytes: usize, memory: Option<TensorMemory>, ) -> Result<Self>

Create a DMA-backed image tensor with an explicit row stride that may exceed the natural width * channels * sizeof(T) pitch.

See Tensor::image_with_stride for the detailed contract and constraints. The TensorDyn wrapper dispatches to the appropriate monomorphised Tensor<T> based on dtype.

§Example
use edgefirst_tensor::{TensorDyn, PixelFormat, DType, TensorMemory};
// Allocate a 3004×1688 RGBA8 canvas with 64-byte pitch alignment
// (12032 bytes per row instead of the natural 12016).
let img = TensorDyn::image_with_stride(
    3004, 1688,
    PixelFormat::Rgba, DType::U8,
    12032,
    Some(TensorMemory::Dma),
)?;
assert_eq!(img.width(), Some(3004));       // logical, unchanged
assert_eq!(img.effective_row_stride(), Some(12032)); // padded

Trait Implementations§

Source§

impl Debug for TensorDyn

Source§

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

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

impl From<Tensor<f16>> for TensorDyn

Source§

fn from(t: Tensor<f16>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<f32>> for TensorDyn

Source§

fn from(t: Tensor<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<f64>> for TensorDyn

Source§

fn from(t: Tensor<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<i16>> for TensorDyn

Source§

fn from(t: Tensor<i16>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<i32>> for TensorDyn

Source§

fn from(t: Tensor<i32>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<i64>> for TensorDyn

Source§

fn from(t: Tensor<i64>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<i8>> for TensorDyn

Source§

fn from(t: Tensor<i8>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<u16>> for TensorDyn

Source§

fn from(t: Tensor<u16>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<u32>> for TensorDyn

Source§

fn from(t: Tensor<u32>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<u64>> for TensorDyn

Source§

fn from(t: Tensor<u64>) -> Self

Converts to this type from the input type.
Source§

impl From<Tensor<u8>> for TensorDyn

Source§

fn from(t: Tensor<u8>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.