use alloc::{boxed::Box, vec::Vec};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Scalar {
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
F32,
F64,
}
impl Scalar {
pub(crate) const ALL: [Self; 10] = [
Self::U8,
Self::U16,
Self::U32,
Self::U64,
Self::I8,
Self::I16,
Self::I32,
Self::I64,
Self::F32,
Self::F64,
];
pub const fn size(self) -> u64 {
match self {
Self::U8 | Self::I8 => 1,
Self::U16 | Self::I16 => 2,
Self::U32 | Self::I32 | Self::F32 => 4,
Self::U64 | Self::I64 | Self::F64 => 8,
}
}
pub(crate) const fn index(self) -> usize {
self as usize
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Type {
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
F32,
F64,
Pointer,
Array(Box<Type>, u64),
Struct(Vec<Type>),
Union(Vec<Type>),
}
impl Type {
pub fn array(element: Self, count: u64) -> Self {
Self::Array(Box::new(element), count)
}
pub(crate) fn scalar(&self) -> Option<Scalar> {
Some(match self {
Self::U8 => Scalar::U8,
Self::U16 => Scalar::U16,
Self::U32 => Scalar::U32,
Self::U64 => Scalar::U64,
Self::I8 => Scalar::I8,
Self::I16 => Scalar::I16,
Self::I32 => Scalar::I32,
Self::I64 => Scalar::I64,
Self::F32 => Scalar::F32,
Self::F64 => Scalar::F64,
Self::Pointer | Self::Array(..) | Self::Struct(..) | Self::Union(..) => return None,
})
}
}