Skip to main content

HostTensorView

Struct HostTensorView 

pub struct HostTensorView<'a, T> { /* private fields */ }
Expand description

Borrowed host tensor view with shape, strides, and offset metadata.

This type intentionally does not implement PartialEq because view equality is ambiguous between metadata identity, storage identity, and logical element equality.

§Examples

use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let view = tensor.as_view();
assert_eq!(view.shape(), &[2]);
let a = tensor.as_view();
let b = tensor.as_view();
let _ = a == b;

Implementations§

§

impl<'a, T> HostTensorView<'a, T>

pub fn from_slice( shape: impl Into<SmallVec<[usize; 8]>>, strides: impl Into<SmallVec<[isize; 8]>>, offset: isize, data: &'a [T], ) -> Result<HostTensorView<'a, T>, ValidationError>

Create a typed view from explicit metadata and validate bounds eagerly.

§Examples
use tenferro_tensor_core::HostTensorView;

let data = [1.0_f64, 2.0, 3.0, 4.0];
let view = HostTensorView::from_slice(vec![2], vec![1], 1, &data)?;
assert_eq!(view.as_slice()?, &[2.0, 3.0]);
§Errors

Returns ValidationError::RankMismatch for shape/stride rank disagreement, ValidationError::ViewOutOfBounds for an unreachable view, or ValidationError::IntegerOverflow when bounds arithmetic overflows.

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

Borrow this view’s shape.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert_eq!(tensor.as_view().shape(), &[2]);

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

Borrow this view’s signed element strides.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![2, 3], vec![0_i32; 6])?;
assert_eq!(tensor.as_view().strides(), &[1, 2]);

pub fn offset(&self) -> isize

Return this view’s signed element offset into the backing slice.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![1], vec![true])?;
assert_eq!(tensor.as_view().offset(), 0);

pub fn rank(&self) -> usize

Return the view rank.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
assert_eq!(tensor.as_view().rank(), 2);

pub fn is_empty(&self) -> bool

Returns true when this view has zero logical elements.

§Examples
use tenferro_tensor_core::HostTensorView;

let data = [1.0_f64];
let view = HostTensorView::from_slice(vec![0], vec![1], 0, &data)?;
assert!(view.is_empty());

pub fn is_compact_col_major(&self) -> Result<bool, ValidationError>

Return whether this view has compact column-major logical strides.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![2, 2], vec![0_i32; 4])?;
assert!(tensor.as_view().is_compact_col_major()?);
§Errors

Returns ValidationError::IntegerOverflow if compactness validation overflows metadata arithmetic.

pub fn is_zero_offset_col_major(&self) -> Result<bool, ValidationError>

Return whether this view is compact column-major and starts at offset zero.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![1], vec![1_i64])?;
assert!(tensor.as_view().is_zero_offset_col_major()?);
§Errors

Returns ValidationError::IntegerOverflow if compactness validation overflows metadata arithmetic.

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

Borrow the slice-contiguous backing region for this view.

§Examples
use tenferro_tensor_core::HostTensorView;

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

Returns ValidationError::NonContiguousViewAsSlice for a view that is not slice-contiguous, ValidationError::ViewOutOfBounds for an invalid backing range, or ValidationError::IntegerOverflow when range arithmetic overflows.

pub fn reshape_view( &self, shape: impl Into<SmallVec<[usize; 8]>>, ) -> Result<HostTensorView<'a, T>, ValidationError>

Return a metadata-only reshape of this compact column-major view.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![4], vec![1.0_f64, 2.0, 3.0, 4.0])?;
assert_eq!(tensor.as_view().reshape_view(vec![2, 2])?.shape(), &[2, 2]);
§Errors

Returns ValidationError::NonContiguousViewAsSlice for a view that is not slice-contiguous, ValidationError::ShapeMismatch for a different element count, or ValidationError::IntegerOverflow when shape arithmetic overflows.

pub fn transpose_view( &self, axes: &[usize], ) -> Result<HostTensorView<'a, T>, ValidationError>

Return a metadata-only transposed view with axes in the requested order.

§Examples
use tenferro_tensor_core::HostTensor;

let tensor = HostTensor::from_vec_col_major(vec![2, 3], vec![0_i32; 6])?;
let view = tensor.as_view().transpose_view(&[1, 0])?;
assert_eq!(view.shape(), &[3, 2]);
assert_eq!(view.strides(), &[2, 1]);
§Errors

Returns ValidationError::InvalidPermutationLength, ValidationError::AxisOutOfBounds, or ValidationError::DuplicateAxis when axes is not a permutation of the view rank; it may also return ValidationError::ViewOutOfBounds or ValidationError::IntegerOverflow while validating the result.

pub fn slice_view( &self, spec: &[SliceSpec], ) -> Result<HostTensorView<'a, T>, ValidationError>

Return a metadata-only positive-step slice of this view.

§Examples
use tenferro_tensor_core::{SliceSpec, HostTensor};

let tensor = HostTensor::from_vec_col_major(vec![4], vec![1_i64, 2, 3, 4])?;
let view = tensor
    .as_view()
    .slice_view(&[SliceSpec { start: 1, end: 4, step: 2 }])?;
assert_eq!(view.shape(), &[2]);
§Errors

Returns ValidationError::RankMismatch when spec does not cover every axis, ValidationError::InvalidSliceStep or ValidationError::InvalidSliceBounds for invalid slice parameters, or ValidationError::ViewOutOfBounds for an invalid result view.

Trait Implementations§

§

impl<'a, T> Clone for HostTensorView<'a, T>
where T: Clone,

§

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

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> Debug for HostTensorView<'a, T>
where T: Debug,

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for HostTensorView<'a, T>

§

impl<'a, T> RefUnwindSafe for HostTensorView<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for HostTensorView<'a, T>
where T: Sync,

§

impl<'a, T> Sync for HostTensorView<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for HostTensorView<'a, T>

§

impl<'a, T> UnsafeUnpin for HostTensorView<'a, T>

§

impl<'a, T> UnwindSafe for HostTensorView<'a, T>
where T: RefUnwindSafe,

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

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,