1mod lthash16;
2mod lthash32;
3mod utils;
4
5pub use lthash16::*;
6pub use lthash32::*;
7
8use digest::ExtendableOutput;
9
10pub trait LtHash {
12 fn insert(&mut self, element: impl AsRef<[u8]>);
14 fn remove(&mut self, element: impl AsRef<[u8]>);
16 fn to_hex_string(&self) -> String;
18 fn union(&self, rhs: &Self) -> Self;
24 fn difference(&self, rhs: &Self) -> Self;
30 fn reset(&mut self);
32 fn into_bytes(self) -> Vec<u8>;
34}
35
36macro_rules! common {
37 ($lthash:ty) => {
38 impl<A, H> Extend<A> for $lthash
39 where
40 A: AsRef<[u8]>,
41 H: ExtendableOutput + Default,
42 {
43 fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
44 for item in iter {
45 self.insert(item);
46 }
47 }
48 }
49
50 impl<H> PartialEq for $lthash {
51 fn eq(&self, other: &Self) -> bool {
52 subtle::ConstantTimeEq::ct_eq(
53 &self.checksum[..],
54 &other.checksum[..],
55 )
56 .into()
57 }
58 }
59
60 impl<H> core::fmt::Debug for $lthash {
61 fn fmt(
62 &self,
63 f: &mut core::fmt::Formatter<'_>,
64 ) -> core::fmt::Result {
65 write!(f, "{} {:?}", self.name(), &self.checksum)
66 }
67 }
68
69 impl<A, H> FromIterator<A> for $lthash
70 where
71 A: AsRef<[u8]>,
72 H: ExtendableOutput + Default,
73 {
74 fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
75 let mut this = Self::default();
76 this.extend(iter);
77 this
78 }
79 }
80
81 impl<'a, H> std::ops::BitOr for &'a $lthash
82 where
83 H: ExtendableOutput + Default,
84 {
85 type Output = $lthash;
86
87 fn bitor(self, rhs: Self) -> Self::Output {
88 self.union(rhs)
89 }
90 }
91
92 impl<H> std::ops::BitOr for $lthash
93 where
94 H: ExtendableOutput + Default,
95 {
96 type Output = Self;
97
98 fn bitor(self, rhs: Self) -> Self::Output {
99 self.union(&rhs)
100 }
101 }
102
103 impl<'a, H> std::ops::Sub for &'a $lthash
104 where
105 H: ExtendableOutput + Default,
106 {
107 type Output = $lthash;
108
109 fn sub(self, rhs: Self) -> Self::Output {
110 self.difference(rhs)
111 }
112 }
113
114 impl<H> std::ops::Sub for $lthash
115 where
116 H: ExtendableOutput + Default,
117 {
118 type Output = Self;
119
120 fn sub(self, rhs: Self) -> Self::Output {
121 self.difference(&rhs)
122 }
123 }
124 };
125}
126
127common!(LtHash16<H>);
128common!(LtHash32<H>);