use crate::spec::types::DataType;
use super::tensor::TensorValue;
use super::value_enum::Value;
impl Value {
#[deny(unreachable_patterns, unused_variables)]
#[must_use]
#[inline]
pub fn try_zero_for(ty: DataType) -> Option<Self> {
match ty {
DataType::U32 => Some(Self::U32(0)),
DataType::I32 => Some(Self::I32(0)),
DataType::U64 => Some(Self::U64(0)),
DataType::Bool => Some(Self::Bool(false)),
DataType::Bytes | DataType::Vec2U32 | DataType::Vec4U32 => {
Some(Self::Bytes(vec![0; ty.min_bytes()]))
}
DataType::F16 | DataType::BF16 | DataType::F32 | DataType::F64 => {
Some(Self::Float(0.0))
}
DataType::Tensor => Some(Self::Tensor(TensorValue {
shape: vec![1, 1],
element_type: DataType::F32,
data: vec![0; 4],
})),
DataType::Array { element_size } => {
if element_size == 0 {
Some(Self::Array(Vec::new()))
} else {
Some(Self::Array(vec![Self::Bytes(vec![0; element_size])]))
}
}
}
}
#[deny(unreachable_patterns, unused_variables)]
#[inline]
pub fn zero_for(ty: DataType) -> Self {
Self::try_zero_for(ty.clone()).unwrap_or_else(|| {
panic!(
"no zero value mapping exists for type {ty:?}. \
Fix: add an explicit Value::zero_for mapping before certifying this type."
)
})
}
}