1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use core::ops::{Add, BitAnd, BitAndAssign, BitOr, BitXor};

use num_traits::CheckedAdd;

use super::{
    super::{mask_types::Type, Mask},
    Address,
};
use crate::traits::Afi;

impl<A: Afi, T: Type> BitAnd<Mask<T, A>> for Address<A> {
    type Output = Self;

    fn bitand(self, mask: Mask<T, A>) -> Self::Output {
        Self::new(self.into_primitive().bitand(mask.into_primitive()))
    }
}

impl<A: Afi, T> BitAndAssign<T> for Address<A>
where
    Self: BitAnd<T, Output = Self>,
{
    fn bitand_assign(&mut self, rhs: T) {
        *self = self.bitand(rhs);
    }
}

impl<A: Afi, T: Type> BitOr<Mask<T, A>> for Address<A> {
    type Output = Self;

    fn bitor(self, mask: Mask<T, A>) -> Self::Output {
        Self::new(self.into_primitive().bitor(mask.into_primitive()))
    }
}

impl<A: Afi, T: Type> Add<Mask<T, A>> for Address<A> {
    type Output = Option<Self>;

    fn add(self, mask: Mask<T, A>) -> Self::Output {
        self.into_primitive()
            .checked_add(&mask.into_primitive())
            .map(Self::new)
    }
}

impl<A: Afi> BitXor<Self> for Address<A> {
    type Output = A::Primitive;

    fn bitxor(self, rhs: Self) -> Self::Output {
        self.into_primitive() ^ rhs.into_primitive()
    }
}