awint_dag/common/
misc.rs

1#[allow(unused)]
2use std::num::NonZeroU32;
3use std::num::NonZeroUsize;
4
5use awint_ext::{
6    awint_internals::{Location, USIZE_BITS},
7    bw, Awi,
8};
9
10use crate::{
11    common::Op,
12    epoch::{get_nzbw_from_current_epoch, get_op_from_current_epoch, new_pstate_for_current_epoch},
13    triple_arena::ptr_struct,
14};
15
16#[cfg(any(
17    debug_assertions,
18    all(feature = "gen_counter_for_pstate", not(feature = "u32_for_pstate"))
19))]
20ptr_struct!(PState);
21
22#[cfg(all(
23    not(debug_assertions),
24    not(feature = "gen_counter_for_pstate"),
25    not(feature = "u32_for_pstate")
26))]
27ptr_struct!(PState());
28
29#[cfg(all(
30    not(debug_assertions),
31    feature = "gen_counter_for_pstate",
32    feature = "u32_for_pstate"
33))]
34ptr_struct!(PState[NonZeroU32](NonZeroU32));
35
36#[cfg(all(
37    not(debug_assertions),
38    not(feature = "gen_counter_for_pstate"),
39    feature = "u32_for_pstate"
40))]
41ptr_struct!(PState[NonZeroU32]());
42
43#[ignore]
44#[test]
45fn dag_size() {
46    use std::mem;
47
48    println!("PState size: {}", mem::size_of::<PState>());
49
50    // 104 with debug assertions, 72 default release, still 72 with "u32_for_pstate"
51    println!("Op<PState> size: {}", mem::size_of::<Op<PState>>());
52
53    panic!();
54}
55
56impl PState {
57    /// Enters a new `State` from the given components into the thread local
58    /// arena and registers it for the current `EpochCallback`.
59    pub fn new(nzbw: NonZeroUsize, op: Op<PState>, location: Option<Location>) -> Self {
60        new_pstate_for_current_epoch(nzbw, op, location)
61    }
62
63    pub fn get_nzbw(&self) -> NonZeroUsize {
64        get_nzbw_from_current_epoch(*self)
65    }
66
67    pub fn get_op(&self) -> Op<PState> {
68        get_op_from_current_epoch(*self)
69    }
70
71    pub fn try_get_as_usize(&self) -> Option<usize> {
72        if let Op::Literal(lit) = self.get_op() {
73            if lit.bw() == USIZE_BITS {
74                return Some(lit.to_usize())
75            }
76        }
77        None
78    }
79
80    pub fn try_get_as_awi(&self) -> Option<Awi> {
81        if let Op::Literal(lit) = self.get_op() {
82            Some(lit)
83        } else {
84            None
85        }
86    }
87}
88
89/// A trait for mimicking structs that allows access to the internal state
90pub trait Lineage {
91    fn state_nzbw(&self) -> NonZeroUsize {
92        self.state().get_nzbw()
93    }
94
95    /// Get a reference to the `State` of `self`
96    fn state(&self) -> PState;
97}
98
99/// Some types can't implement `Default`, this is a `Default`-like trait for
100/// placeholder values
101#[doc(hidden)]
102pub trait DummyDefault {
103    fn default() -> Self;
104}
105
106impl DummyDefault for NonZeroUsize {
107    fn default() -> Self {
108        bw(1)
109    }
110}
111
112impl DummyDefault for Awi {
113    fn default() -> Self {
114        Awi::zero(bw(1))
115    }
116}
117
118impl DummyDefault for PState {
119    fn default() -> Self {
120        Default::default()
121    }
122}