Skip to main content

catalejo_memory/
primitive.rs

1//! Primitive types in respect to a memory operation.
2
3use core::mem;
4
5use crate::behavior::Unassociated;
6
7/// A primitive that can be read with machine-word coherence.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
9pub enum Primitive {
10    /// [`prim@u8`]-level coherence.
11    U8 = mem::size_of::<u8>().cast_signed(),
12
13    /// [`prim@u16`]-level coherence.
14    U16 = mem::size_of::<u16>().cast_signed(),
15
16    /// [`prim@u32`]-level coherence.
17    U32 = mem::size_of::<u32>().cast_signed(),
18
19    /// [`prim@u64`]-level coherence.
20    U64 = mem::size_of::<u64>().cast_signed(),
21}
22
23impl Primitive {
24    /// Determine an appropriate [`Primitive`] type variant for the target type.
25    #[inline]
26    pub const fn appropriate<T>() -> Option<Self>
27    where
28        T: Unassociated,
29    {
30        match mem::size_of::<T>() {
31            1 => Some(Self::U8),
32            2 => Some(Self::U16),
33            4 => Some(Self::U32),
34            8 => Some(Self::U64),
35            _ => None,
36        }
37    }
38}
39
40/// An union of all primitives.
41///
42/// This is used for primitive-independent operations.
43#[derive(Clone, Copy)]
44#[repr(C)]
45pub union PrimitiveUnion {
46    /// [`prim@u8`]
47    pub u8: u8,
48
49    /// [`prim@u16`]
50    pub u16: u16,
51
52    /// [`prim@u32`]
53    pub u32: u32,
54
55    /// [`prim@u64`]
56    pub u64: u64,
57}