atomiq/
atomizable.rs

1use crate::atom::{Atom, BitAtom, IntAtom};
2use crate::Atomic;
3
4/// Trait for types that may be represented as atomic values.
5pub trait Atomizable {
6    /// The primitive representation of the type.
7    type Atom: Atom;
8    
9    /// Packs the value into its primitive representation.
10    fn pack(self) -> Self::Atom;
11    
12    /// Unpacks the value from its primitive representation.
13    fn unpack(atom: Self::Atom) -> Self;
14}
15
16impl<T: Atom> Atomizable for T {
17    type Atom = T;
18
19    fn pack(self) -> Self::Atom {
20        self
21    }
22    
23    fn unpack(atom: Self::Atom) -> Self {
24        atom
25    }
26}
27
28/// Trait for types that may be represented as atomic bit values.
29pub trait BitAtomizable: Atomizable<Atom: BitAtom> {}
30
31impl<T: BitAtom> BitAtomizable for T {}
32
33/// Trait for types that may be represented as atomic integer values.
34pub trait IntAtomizable: Atomizable<Atom: IntAtom> {}
35
36impl<T: IntAtom> IntAtomizable for T {}
37
38/// Extension trait for converting values into atomic.
39/// 
40/// This trait is implemented for all types that implement `Atomizable`.
41pub trait Atomize: Atomizable + Sized {
42    /// The primitive representation of the type.
43    type Atom: Atom;
44
45    /// Converts the value into an atomic.
46    fn atomize(self) -> Atomic<Self>;
47}
48
49impl<T: Atomizable> Atomize for T {
50    type Atom = T::Atom;
51    
52    fn atomize(self) -> Atomic<Self> {
53        Atomic::from(self)
54    }
55}