Skip to main content

AsView

Trait AsView 

Source
pub trait AsView: Sealed {
    type Repr: Repr;

    // Required method
    fn as_read(&self) -> TensorRead<'_>;
}
Expand description

Any tensor input that can be read as a borrowed view without copying.

§Examples

use tenferro_tensor::{AsView, Tensor, TensorRead, TypedTensor, TypedTensorView};

let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let typed = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![1.0, 2.0])?;
let read = TensorRead::from_tensor(&tensor);

assert_eq!(tensor.as_read().shape(), typed.as_read().shape());
assert_eq!(read.as_read().dtype(), typed.as_read().dtype());
assert_eq!((&&tensor).as_read().shape(), &[2]);

let data = [1.0_f64, 2.0];
let view = TypedTensorView::from_col_major(&[2], &data)?;
assert_eq!(view.as_read().shape(), &[2]);
assert_eq!(view.as_read().as_slice::<f64>()?.as_ptr(), data.as_ptr());

// No element data is copied.
assert_eq!(
    tensor.as_read().as_slice::<f64>()?.as_ptr(),
    tensor.as_read().as_slice::<f64>()?.as_ptr(),
);
assert_eq!(
    typed.as_read().as_slice::<f64>()?.as_ptr(),
    typed.as_slice()?.as_ptr(),
);

Required Associated Types§

Source

type Repr: Repr

The representation results take for this input.

Required Methods§

Source

fn as_read(&self) -> TensorRead<'_>

Borrow this input as a TensorRead. Metadata only; never copies element data.

§Examples
use tenferro_tensor::{AsView, Tensor};

let t = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let read = t.as_read();
assert_eq!(read.shape(), &[2]);
assert_eq!(read.as_slice::<f64>()?, &[1.0, 2.0]);

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<X: AsView + ?Sized> AsView for &X

Source§

type Repr = <X as AsView>::Repr

Source§

fn as_read(&self) -> TensorRead<'_>

Implementors§