pub fn scalar_operand<'a, T: TensorScalar>(
tensor: &'a Tensor,
op: &'static str,
backend: impl Display,
) -> Result<&'a TypedTensor<T>>Expand description
Dispatch a dtype-erased Tensor to a typed tensor body.
The dtype-set guard keeps unsupported dtype rejection at the boundary where the backend and operation name are still visible.
§Examples
use tenferro_tensor::{BackendId, Tensor};
let tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let shape = tenferro_tensor::with_scalar!(
&tensor,
float_only,
backend = BackendId::Cpu,
op = "shape_probe",
|typed| -> tenferro_tensor::Result<Vec<usize>> { Ok(typed.shape().to_vec()) }
)?;
assert_eq!(shape, vec![2]);The typed tensor behind tensor when its dtype is the requested scalar.
The exported dispatch macros match on the dtype first, so a mismatch here means the table and the runtime dtype disagree; the refusal names the operation and backend the caller supplied.
§Errors
Returns Error::UnsupportedDType when the tensor’s dtype is not T,
which for an externally defined scalar reports the caller-owned payload.
§Examples
use tenferro_tensor::{DType, Tensor};
let tensor = Tensor::from_vec_col_major(vec![1], vec![1.0_f64])?;
assert_eq!(tensor.dtype(), DType::F64);