splinter_rs/
splinter_ref_ops.rs

1use std::ops::{
2    BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Deref, Sub, SubAssign,
3};
4
5use crate::{CowSplinter, Splinter, SplinterRef};
6
7impl<B> PartialEq<Splinter> for SplinterRef<B>
8where
9    B: Deref<Target = [u8]>,
10{
11    #[inline]
12    fn eq(&self, other: &Splinter) -> bool {
13        other == self
14    }
15}
16
17impl<B, B2> PartialEq<SplinterRef<B2>> for SplinterRef<B>
18where
19    B: Deref<Target = [u8]>,
20    B2: Deref<Target = [u8]>,
21{
22    fn eq(&self, other: &SplinterRef<B2>) -> bool {
23        self.load_unchecked() == other.load_unchecked()
24    }
25}
26
27impl<B: Deref<Target = [u8]>> Eq for SplinterRef<B> {}
28
29macro_rules! binary_bitop {
30    ($a:ty, $b:ty) => {
31        binary_bitop!($a, $b, BitAnd, bitand, BitAndAssign::bitand_assign);
32        binary_bitop!($a, $b, BitOr, bitor, BitOrAssign::bitor_assign);
33        binary_bitop!($a, $b, BitXor, bitxor, BitXorAssign::bitxor_assign);
34        binary_bitop!($a, $b, Sub, sub, SubAssign::sub_assign);
35    };
36    ($a:ty, $b:ty, $BitOp:ident, $bitop:ident, $bitassign:path) => {
37        impl<B1: Deref<Target = [u8]>, B2: Deref<Target = [u8]>> $BitOp<$b> for $a {
38            type Output = Splinter;
39            fn $bitop(self, rhs: $b) -> Self::Output {
40                let mut out = self.decode_to_splinter();
41                $bitassign(&mut out, rhs);
42                out
43            }
44        }
45    };
46}
47
48binary_bitop!(SplinterRef<B1>, SplinterRef<B2>);
49binary_bitop!(SplinterRef<B1>, &SplinterRef<B2>);
50binary_bitop!(&SplinterRef<B1>, SplinterRef<B2>);
51binary_bitop!(&SplinterRef<B1>, &SplinterRef<B2>);
52
53binary_bitop!(SplinterRef<B1>, CowSplinter<B2>);
54binary_bitop!(SplinterRef<B1>, &CowSplinter<B2>);
55binary_bitop!(&SplinterRef<B1>, CowSplinter<B2>);
56binary_bitop!(&SplinterRef<B1>, &CowSplinter<B2>);