pub trait StorageScalar:
TensorElement
+ Default
+ PartialEq
+ Num {
type Complex: StorageScalar;
const KIND: &'static str;
const IS_COMPLEX: bool;
Show 14 methods
// Required methods
fn widened_parts(self) -> (f64, f64);
fn from_widened_parts(re: f64, im: f64) -> Self;
fn real_part(self) -> Self::Real;
fn imag_part(self) -> Self::Real;
fn into_complex(self) -> Self::Complex;
fn conjugate(self) -> Self;
fn wrap_payload(payload: StructuredStorage<Self>) -> Storage;
fn to_backend_scalar(self) -> BackendScalar;
fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>;
fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>;
fn dense_values(
storage: &Storage,
logical_dims: &[usize],
) -> StorageResult<Vec<Self>>;
// Provided methods
fn build_dense_storage(
data: Vec<Self>,
logical_dims: &[usize],
) -> Result<Storage> { ... }
fn build_diag_storage(
diag_data: Vec<Self>,
logical_rank: usize,
) -> Result<Storage> { ... }
fn build_structured_storage(
data: Vec<Self>,
payload_dims: Vec<usize>,
strides: Vec<isize>,
axis_classes: Vec<usize>,
) -> Result<Storage> { ... }
}Expand description
Trait for scalar types that can be stored in Storage.
This enables generic constructors such as Storage::from_dense_col_major
and Storage::from_diag_col_major, and it is the single seam through
which dtype-erased storage code reaches a concrete payload. Implemented for
f64, Complex64, f32, and Complex32.
Compact storage never widens a payload: f32 stays f32 and Complex32
stays Complex32 in memory and on every accessor. The associated items
describe the real/complex structure so that dtype-agnostic scans can be
written once; explicit width promotion happens only in the storage algebra
that documents it (Storage::axpby, Storage::scale).
§Examples
use tensor4all_tensorbackend::{Storage, StorageScalar};
// Using the generic constructor -- scalar type is inferred from data
let s = Storage::from_dense_col_major(vec![1.0_f64, 2.0, 3.0], &[3]).unwrap();
assert!(s.is_f64());
use num_complex::Complex64;
let c = Storage::from_dense_col_major(
vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 1.0)],
&[2],
).unwrap();
assert!(c.is_c64());
// Narrow payloads keep their dtype instead of widening.
let narrow = Storage::from_dense_col_major(vec![1.0_f32, 2.0], &[2]).unwrap();
assert!(narrow.is_f32());
assert_eq!(f32::KIND, "f32");
assert_eq!(Complex64::KIND, "Complex64");Required Associated Constants§
Sourceconst KIND: &'static str
const KIND: &'static str
Display name of this scalar type in diagnostics ("f32", "Complex64", …).
Sourceconst IS_COMPLEX: bool
const IS_COMPLEX: bool
Whether values carry an imaginary component.
Required Associated Types§
Sourcetype Complex: StorageScalar
type Complex: StorageScalar
The complex counterpart of this scalar type (f32 -> Complex32).
Required Methods§
Sourcefn widened_parts(self) -> (f64, f64)
fn widened_parts(self) -> (f64, f64)
Widen this value to (real, imaginary) parts for dtype-agnostic scans.
Sourcefn from_widened_parts(re: f64, im: f64) -> Self
fn from_widened_parts(re: f64, im: f64) -> Self
Narrow widened parts back to this scalar type.
Sourcefn imag_part(self) -> Self::Real
fn imag_part(self) -> Self::Real
Imaginary part as this scalar’s real counterpart (0 for real scalars).
Sourcefn into_complex(self) -> Self::Complex
fn into_complex(self) -> Self::Complex
Embed this value, including its imaginary part, in Self::Complex.
Sourcefn wrap_payload(payload: StructuredStorage<Self>) -> Storage
fn wrap_payload(payload: StructuredStorage<Self>) -> Storage
Wrap a compact payload of this scalar type as a Storage.
Sourcefn to_backend_scalar(self) -> BackendScalar
fn to_backend_scalar(self) -> BackendScalar
Copy this value into a dynamic BackendScalar.
Sourcefn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>
fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>
Copy the compact payload of this scalar type, rejecting other dtypes.
§Errors
Returns StorageError::ScalarKindMismatch when storage holds a
different scalar type.
Sourcefn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>
fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>
Borrow the compact payload of this scalar type when it is contiguous.
§Errors
Returns StorageError::ScalarKindMismatch when storage holds a
different scalar type.
Sourcefn dense_values(
storage: &Storage,
logical_dims: &[usize],
) -> StorageResult<Vec<Self>>
fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>
Materialize the logical dense values of this scalar type.
§Errors
Returns StorageError::ScalarKindMismatch when storage holds a
different scalar type, and
StorageError::InvalidStructuredStorage when logical_dims does not
match the stored logical dimensions or the logical element count
overflows usize.
Provided Methods§
Sourcefn build_structured_storage(
data: Vec<Self>,
payload_dims: Vec<usize>,
strides: Vec<isize>,
axis_classes: Vec<usize>,
) -> Result<Storage>
fn build_structured_storage( data: Vec<Self>, payload_dims: Vec<usize>, strides: Vec<isize>, axis_classes: Vec<usize>, ) -> Result<Storage>
Build a structured Storage from explicit payload metadata.
§Errors
Returns an error when axis_classes is not in canonical
first-appearance order, when the payload_dims or strides ranks do
not match axis_classes, when a stride is negative, when a size
product would overflow usize, or when data.len() does not match the
required payload length.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".