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>;
fn typed_tensor_into_tensor(tensor: TypedTensor<Self>) -> Tensor;
fn tensor_read(tensor: &TypedTensor<Self>) -> TensorRead<'_>;
fn tensor_view<'a>(view: TypedTensorView<'a, Self>) -> TensorView<'a>;
fn tensor_write(tensor: &mut TypedTensor<Self>) -> TensorWrite<'_>;
fn as_slice(tensor: &Tensor) -> Result<&[Self]>;
fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self]>;
fn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>>;
}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>
fn into_tensor(shape: Vec<usize>, data: Vec<Self>) -> Result<Tensor>
Wrap typed column-major data into a Tensor enum variant.
Sourcefn typed_tensor_into_tensor(tensor: TypedTensor<Self>) -> Tensor
fn typed_tensor_into_tensor(tensor: TypedTensor<Self>) -> Tensor
Wrap a typed tensor into its dynamic Tensor enum variant.
§Examples
use tenferro_tensor::{Tensor, TensorScalar, TypedTensor};
let typed = TypedTensor::<f64>::from_vec_col_major(vec![1], vec![3.0])?;
let tensor = <f64 as TensorScalar>::typed_tensor_into_tensor(typed);
assert!(matches!(tensor, Tensor::F64(_)));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 tensor_view<'a>(view: TypedTensorView<'a, Self>) -> TensorView<'a>
fn tensor_view<'a>(view: TypedTensorView<'a, Self>) -> TensorView<'a>
Wrap a typed borrowed view as a dtype-erased TensorView.
§Examples
use tenferro_tensor::{DType, TensorScalar, TypedTensorView};
let data = [1.0_f64];
let view = TypedTensorView::from_col_major(&[1], &data)?;
assert_eq!(f64::tensor_view(view).dtype(), DType::F64);Sourcefn tensor_write(tensor: &mut TypedTensor<Self>) -> TensorWrite<'_>
fn tensor_write(tensor: &mut TypedTensor<Self>) -> TensorWrite<'_>
Mutably borrow a typed tensor as a dtype-erased TensorWrite view.
This keeps the typed output borrowed instead of wrapping it in a temporary dynamic tensor.
§Examples
use tenferro_tensor::{DType, TensorScalar, TypedTensor};
let mut tensor = TypedTensor::<f64>::from_vec_col_major(vec![1], vec![0.0]).unwrap();
let write = f64::tensor_write(&mut tensor);
assert_eq!(write.dtype(), DType::F64);Sourcefn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self]>
fn as_slice_mut(tensor: &mut Tensor) -> Result<&mut [Self]>
Sourcefn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>>
fn into_typed(tensor: Tensor) -> Result<TypedTensor<Self>>
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.