Skip to main content

TypedTensorView

Struct TypedTensorView 

pub struct TypedTensorView<'a, T, R = DynRank>
where R: TensorRank,
{ /* private fields */ }
Expand description

Read-only borrowed view of typed tensor storage with arbitrary strides.

TypedTensorView is the typed representation for layout-only tensor transformations. It borrows an existing host or backend allocation and carries a logical shape, strides, and an offset. Slicing, reshaping when stride-compatible, and transpose_view update only metadata and do not copy storage.

Materialize through TensorStructural::to_contiguous_read on the active backend session when a compact owned TypedTensor is required. Use TypedTensorView::as_slice only when the current view is contiguous in the requested layout.

§Examples

use tenferro_tensor::{Rank, TypedTensorView};

let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::<_, Rank<2>>::from_slice_ranked([2, 2], [1, 2], 0, &data)?;
assert_eq!(view.get(&[1, 1]), Some(&4));

Implementations§

§

impl<'a, T> TypedTensorView<'a, T>
where T: 'static,

pub fn from_col_major( shape: &[usize], data: &'a [T], ) -> Result<TypedTensorView<'a, T>, Error>

Create a borrowed dynamic-rank view over compact column-major host data.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::from_col_major(&[2, 2], &data)?;
assert_eq!(view.strides(), &[1, 2]);
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::IntegerOverflow] when compact strides or reachable bounds overflow, or [tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the requested shape reaches beyond data.

pub fn from_slice( shape: impl AsRef<[usize]>, strides: impl AsRef<[isize]>, offset: isize, data: &'a [T], ) -> Result<TypedTensorView<'a, T>, Error>

Create a borrowed host view from explicit layout metadata.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![3], vec![-1], 2, &data)?;
assert_eq!(view.get(&[2]), Some(&1));
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::RankMismatch] when shape and strides have different ranks, [tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the reachable layout exceeds data, or [tenferro_tensor_core::ValidationError::IntegerOverflow] when layout arithmetic overflows.

§

impl<'a, T, R> TypedTensorView<'a, T, R>
where T: 'static, R: TensorRank,

pub fn from_slice_ranked( shape: impl Into<<R as TensorRank>::Shape>, strides: impl Into<<R as TensorRank>::Strides>, offset: isize, data: &'a [T], ) -> Result<TypedTensorView<'a, T, R>, Error>

Create a rank-generic borrowed host view from explicit layout metadata.

§Examples
use tenferro_tensor::{Rank, TypedTensorView};

let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::<_, Rank<2>>::from_slice_ranked([2, 2], [1, 2], 0, &data)?;
assert_eq!(view.get(&[1, 1]), Some(&4));
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::RankMismatch] when the typed rank does not match shape or strides, [tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the reachable layout exceeds data, or [tenferro_tensor_core::ValidationError::IntegerOverflow] when layout arithmetic overflows.

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

Return the logical shape.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [0_i32; 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert_eq!(view.shape(), &[2]);

pub fn rank(&self) -> usize

Return the logical rank carried by this view.

pub fn strides(&self) -> &[isize]

Return strides in element units.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [0_i32; 2];
let view = TypedTensorView::from_slice(vec![2], vec![-1], 1, &data)?;
assert_eq!(view.strides(), &[-1]);

pub fn offset(&self) -> isize

Return the physical element offset.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![1], vec![1], 1, &data)?;
assert_eq!(view.offset(), 1);

pub fn host_storage(&self) -> Result<&'a [T], Error>

Return the borrowed host storage backing this view.

This exposes the entire backing host allocation, not just the logical slice covered by this view. Use TypedTensorView::as_slice when the caller needs the contiguous logical region instead.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert_eq!(view.host_storage()?, &[1, 2]);
§Errors

Returns [crate::Error::RuntimeState] when this view wraps a backend buffer; backend storage must be downloaded before host inspection.

pub fn n_elements(&self) -> usize

Return the number of logical elements in this view.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [0_i32; 6];
let view = TypedTensorView::from_slice(vec![2, 3], vec![1, 2], 0, &data)?;
assert_eq!(view.n_elements(), 6);

pub fn layout(&self) -> &TensorLayout<R>

Return layout metadata for this view.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert!(view.layout().is_compact_col_major().unwrap());

pub fn placement(&self) -> &Placement

Return placement metadata for this view.

§Examples
use tenferro_tensor::{MemoryKind, TypedTensorView};

let data = [1_i32];
let view = TypedTensorView::from_slice(vec![1], vec![1], 0, &data)?;
assert_eq!(view.placement().memory_kind, MemoryKind::UnpinnedHost);

pub fn linear_offset(&self, indices: &[usize]) -> Option<usize>

Compute the physical element offset for a logical index.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![3], vec![-1], 2, &data)?;
assert_eq!(view.linear_offset(&[2]), Some(0));

pub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize, Error>

Compute the physical element offset for a logical index, returning a typed error.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice([3], [-1], 2, &data)?;
assert_eq!(view.layout_linear_offset(&[2])?, 0);
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::RankMismatch] when indices has the wrong rank, [tenferro_tensor_core::ValidationError::InvalidArgument] when an index is outside its axis extent, or [tenferro_tensor_core::ValidationError::IntegerOverflow] when offset arithmetic overflows.

pub fn is_col_major_contiguous(&self) -> Result<bool, Error>

Return whether this view is compact column-major.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice([2], [1], 0, &data)?;
assert!(view.is_col_major_contiguous()?);
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::IntegerOverflow] if compactness arithmetic overflows.

pub fn layout_summary(&self) -> String

Return a compact string summary of this view’s layout metadata.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice([2], [1], 0, &data)?;
assert!(view.layout_summary().contains("shape=[2]"));

pub fn assert_col_major_contiguous(&self) -> Result<(), Error>

Assert this view is compact column-major.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice([2], [1], 0, &data)?;
view.assert_col_major_contiguous()?;
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::IntegerOverflow] when compactness arithmetic overflows, or [tenferro_tensor_core::ValidationError::InvalidArgument] when the view is not compact column-major.

pub fn get(&self, indices: &[usize]) -> Option<&T>

Borrow one host element by logical index.

Returns None for out-of-bounds indices and backend buffers.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
assert_eq!(view.get(&[1]), Some(&2));

pub fn as_slice(&self) -> Result<&'a [T], Error>

Borrow the contiguous host slice covered by this view.

Returns an explicit error for backend buffers and for non-contiguous layouts. This method never downloads or materializes backend data.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![2], vec![1], 1, &data)?;
assert_eq!(view.as_slice()?, &[2, 3]);
§Errors

Returns [crate::Error::RuntimeState] when this view wraps a backend buffer, [tenferro_tensor_core::ValidationError::InvalidArgument] when the layout is not slice-contiguous or has a negative offset, or [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::ViewOutOfBounds] or [tenferro_tensor_core::ValidationError::IntegerOverflow] when the requested host range is invalid.

pub fn duplicate(&self) -> Result<TypedTensor<T, R>, Error>
where T: TensorScalar,

Explicitly duplicate a compact host view into a new owner.

Backend views require an explicit provider canonicalization or download boundary; this method never transfers or materializes them implicitly.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2];
let view = TypedTensorView::from_slice(vec![2], vec![1], 0, &data)?;
let copy = view.duplicate()?;
assert_eq!(copy.as_slice()?, &[1, 2]);
§Errors

Returns [crate::Error::HostAccess] or [ValidationError::NonContiguousViewAsSlice] when the view is backend owned or not contiguous, and [ValidationError::InvalidArgument] when the static-rank shape cannot be reconstructed.

pub fn transpose_view( &self, axes: impl AsRef<[usize]>, ) -> Result<TypedTensorView<'a, T, R>, Error>

Return a metadata-only axis permutation.

§Examples
use tenferro_tensor::{Rank, TypedTensorView};

let data = [1_i32, 2, 3, 4, 5, 6];
let view = TypedTensorView::<_, Rank<2>>::from_slice_ranked([2, 3], [1, 2], 0, &data)?;
let transposed = view.transpose_view([1, 0])?;
assert_eq!(transposed.shape(), &[3, 2]);
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::InvalidPermutationLength], [tenferro_tensor_core::ValidationError::AxisOutOfBounds], or [tenferro_tensor_core::ValidationError::DuplicateAxis] when axes is not a valid permutation of the view rank.

pub fn try_slice( &self, slices: &[StridedSliceSpec], ) -> Result<TypedTensorView<'a, T, R>, Error>

Return a metadata-only slice using one [StridedSliceSpec] per axis.

§Examples
use tenferro_tensor::{StridedSliceSpec, TypedTensorView};

let data = [1_i32, 2, 3];
let view = TypedTensorView::from_slice(vec![3], vec![1], 0, &data)?;
let reversed = view.try_slice(&[StridedSliceSpec::reverse()])?;
assert_eq!(reversed.get(&[0]), Some(&3));
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::RankMismatch] when the slice count differs from the view rank, [tenferro_tensor_core::ValidationError::InvalidSliceStep] or [tenferro_tensor_core::ValidationError::InvalidSliceBounds] for an invalid slice, [tenferro_tensor_core::ValidationError::IntegerOverflow] for slice arithmetic overflow, or [tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the resulting layout exceeds the backing buffer.

pub fn try_slice_axis( &self, axis: usize, slice: StridedSliceSpec, ) -> Result<TypedTensorView<'a, T, R>, Error>

Return a metadata-only slice along one axis.

§Examples
use tenferro_tensor::{StridedSliceSpec, TypedTensorView};

let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::from_slice(vec![2, 2], vec![1, 2], 0, &data)?;
assert_eq!(view.try_slice_axis(1, StridedSliceSpec::reverse())?.get(&[0, 0]), Some(&3));
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::AxisOutOfBounds] when axis is outside the view rank, [tenferro_tensor_core::ValidationError::InvalidSliceStep] or [tenferro_tensor_core::ValidationError::InvalidSliceBounds] for an invalid slice, [tenferro_tensor_core::ValidationError::IntegerOverflow] for slice arithmetic overflow, or [tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the resulting layout exceeds the backing buffer.

pub fn try_reshape( &self, shape: &[usize], ) -> Result<TypedTensorView<'a, T>, Error>

Return a metadata-only dynamic-rank reshape for contiguous column-major views.

§Examples
use tenferro_tensor::TypedTensorView;

let data = [1_i32, 2, 3, 4];
let view = TypedTensorView::from_slice(vec![2, 2], vec![1, 2], 0, &data)?;
assert_eq!(view.try_reshape(&[4])?.shape(), &[4]);
§Errors

Returns [crate::Error::Validation] with [tenferro_tensor_core::ValidationError::NonContiguousViewAsSlice] when the source is not compact column-major, [tenferro_tensor_core::ValidationError::ShapeMismatch] (whose [tenferro_tensor_core::ShapeMismatch::ReshapeElementCount] source records the counts) when element counts differ, [tenferro_tensor_core::ValidationError::IntegerOverflow] for shape arithmetic overflow, or [tenferro_tensor_core::ValidationError::ViewOutOfBounds] when the reshaped view exceeds the backing buffer.

§

impl<'a, R> TypedTensorView<'a, Complex<f32>, R>
where R: TensorRank,

pub fn as_real_view(&self) -> Result<TypedTensorView<'a, f32>, Error>

Borrow this complex view as an interleaved real view without copying.

The result has dynamic rank because reinterpretation prepends the component axis [2, ...]. Only Complex32 <-> f32 is sealed in this API; this is representation reinterpretation, not numeric conversion.

§Errors

Returns an error when the view layout is not a valid sealed representation or when backend reinterpretation is unsupported.

§

impl<'a, R> TypedTensorView<'a, Complex<f64>, R>
where R: TensorRank,

pub fn as_real_view(&self) -> Result<TypedTensorView<'a, f64>, Error>

Borrow this complex view as an interleaved real view without copying.

The result has dynamic rank because reinterpretation prepends the component axis [2, ...]. Only Complex64 <-> f64 is sealed in this API; this is representation reinterpretation, not numeric conversion.

§Errors

Returns an error when the view layout is not a valid sealed representation or when backend reinterpretation is unsupported.

§

impl<'a, R> TypedTensorView<'a, f32, R>
where R: TensorRank,

pub fn as_complex_view( &self, ) -> Result<TypedTensorView<'a, Complex<f32>>, Error>

Borrow this interleaved real view as a complex view without copying.

The source must have a leading extent and stride of 2 and 1, and every remaining stride plus the offset must be divisible by 2.

§Errors

Returns an error when the view layout is not a valid sealed representation or when backend reinterpretation is unsupported.

§

impl<'a, R> TypedTensorView<'a, f64, R>
where R: TensorRank,

pub fn as_complex_view( &self, ) -> Result<TypedTensorView<'a, Complex<f64>>, Error>

Borrow this interleaved real view as a complex view without copying.

The source must have a leading extent and stride of 2 and 1, and every remaining stride plus the offset must be divisible by 2.

§Errors

Returns an error when the view layout is not a valid sealed representation or when backend reinterpretation is unsupported.

Trait Implementations§

§

impl<'a, T, R> Clone for TypedTensorView<'a, T, R>
where T: Clone, R: Clone + TensorRank,

§

fn clone(&self) -> TypedTensorView<'a, T, R>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
§

impl<'a, T, R> Debug for TypedTensorView<'a, T, R>
where T: Debug, R: Debug + TensorRank,

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T, R = DynRank> !RefUnwindSafe for TypedTensorView<'a, T, R>

§

impl<'a, T, R = DynRank> !UnwindSafe for TypedTensorView<'a, T, R>

§

impl<'a, T, R> Freeze for TypedTensorView<'a, T, R>
where <R as TensorRank>::Shape: Freeze, <R as TensorRank>::Strides: Freeze,

§

impl<'a, T, R> Send for TypedTensorView<'a, T, R>
where <R as TensorRank>::Shape: Send, <R as TensorRank>::Strides: Send, T: Send + Sync,

§

impl<'a, T, R> Sync for TypedTensorView<'a, T, R>
where <R as TensorRank>::Shape: Sync, <R as TensorRank>::Strides: Sync, T: Sync,

§

impl<'a, T, R> Unpin for TypedTensorView<'a, T, R>
where <R as TensorRank>::Shape: Unpin, <R as TensorRank>::Strides: Unpin, T: Unpin, R: Unpin,

§

impl<'a, T, R> UnsafeUnpin for TypedTensorView<'a, T, R>
where <R as TensorRank>::Shape: UnsafeUnpin, <R as TensorRank>::Strides: UnsafeUnpin,

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

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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