pub trait TensorScalar:
Copy
+ Clone
+ Send
+ Sync
+ 'static
+ Sealed {
type Real: TensorScalar;
// Required methods
fn dtype() -> DType;
fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Result<Tensor, Error>;
fn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>;
fn as_slice(tensor: &Tensor) -> Result<&[Self], Error>;
fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self], Error>;
fn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>, Error>;
}Expand description
Sealed trait for scalar types that can be stored in a Tensor.
This trait is implemented for f64, f32, i32, i64, bool,
Complex64, and Complex32.
§Examples
use tenferro_tensor::TensorScalar;
let tensor = <f64 as TensorScalar>::into_tensor(vec![2], vec![1.0, 2.0])?;
assert_eq!(tensor.as_slice::<f64>()?, [1.0, 2.0].as_slice());Required Associated Types§
Sourcetype Real: TensorScalar
type Real: TensorScalar
Real-valued counterpart of this scalar type.
Required Methods§
Sourcefn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Result<Tensor, Error>
fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Result<Tensor, Error>
Wrap typed column-major data into a Tensor enum variant.
Sourcefn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>
fn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>
Borrow a typed tensor as a dtype-erased TensorRead view.
This keeps the typed tensor borrowed instead of copying host data into a new dynamic tensor.
§Examples
use tenferro_tensor::{DType, TensorScalar, TypedTensor};
let tensor = TypedTensor::<f64>::from_vec_col_major(vec![2], vec![1.0, 2.0]).unwrap();
let read = f64::tensor_read(&tensor);
assert_eq!(read.dtype(), DType::F64);
assert_eq!(read.shape(), &[2]);Sourcefn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self], Error>
fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self], Error>
Sourcefn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>, Error>
fn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>, Error>
Extract a TypedTensor<Self> from a dynamic Tensor.
§Examples
use tenferro_tensor::{Tensor, TensorScalar};
let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let typed = <f64 as TensorScalar>::into_typed(tensor)?;
assert_eq!(typed.as_slice()?, &[1.0, 2.0]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.