use super::value_enum::Value;
impl Value {
#[inline]
pub fn truthy(&self) -> bool {
match self {
Self::Float(v) => v.to_bits() != 0,
Self::Tensor(t) => !t.data.is_empty(),
Self::Array(arr) => !arr.is_empty(),
_ => self.try_as_u32().unwrap_or(1) != 0,
}
}
#[inline]
pub fn to_bytes(&self) -> Vec<u8> {
match self {
Self::U32(v) => v.to_le_bytes().to_vec(),
Self::I32(v) => v.to_le_bytes().to_vec(),
Self::U64(v) => v.to_le_bytes().to_vec(),
Self::Bool(v) => u32::from(*v).to_le_bytes().to_vec(),
Self::Bytes(bytes) => bytes.clone(),
Self::Float(v) => v.to_le_bytes().to_vec(),
Self::Tensor(t) => t.data.clone(),
Self::Array(arr) => arr.iter().flat_map(Self::to_bytes).collect(),
}
}
#[inline]
pub fn to_bytes_width(&self, declared_width: usize) -> Vec<u8> {
let mut bytes = self.to_bytes();
if declared_width == 0 {
return bytes;
}
bytes.resize(declared_width, 0);
bytes.truncate(declared_width);
bytes
}
}