Skip to main content

rstm_state/impls/
impl_state_ops.rs

1/*
2    Appellation: impl_ops <state>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::state::State;
6use num_traits::{Num, One, Zero};
7
8contained::fmt_wrapper! {
9    impl State<T> {
10        Debug,
11        Display,
12        LowerExp,
13        LowerHex,
14        Octal,
15        UpperExp,
16        UpperHex,
17    }
18
19}
20contained::unary_wrapper! {
21    impl State {
22        Neg.neg,
23        Not.not,
24    }
25}
26
27contained::binary_wrapper! {
28    impl State {
29        Add.add,
30        Sub.sub,
31        Mul.mul,
32        Div.div,
33        Rem.rem,
34        BitAnd.bitand,
35        BitOr.bitor,
36        BitXor.bitxor,
37        Shl.shl,
38        Shr.shr
39    }
40}
41
42impl<Q> Num for State<Q>
43where
44    Q: Num,
45{
46    type FromStrRadixErr = Q::FromStrRadixErr;
47
48    fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
49        Q::from_str_radix(str, radix).map(State)
50    }
51}
52
53impl<Q> One for State<Q>
54where
55    Q: PartialEq + One,
56{
57    fn one() -> Self {
58        State(Q::one())
59    }
60
61    fn is_one(&self) -> bool {
62        self.0.is_one()
63    }
64}
65
66impl<Q> Zero for State<Q>
67where
68    Q: Zero,
69{
70    fn zero() -> Self {
71        State(Q::zero())
72    }
73
74    fn is_zero(&self) -> bool {
75        self.0.is_zero()
76    }
77}