splinter_rs/splinter/
union.rs1use crate::{
2 cow::CowSplinter,
3 ops::{Merge, Union},
4 util::CopyToOwned,
5};
6
7use super::{Splinter, SplinterRef};
8
9impl Union for Splinter {
11 type Output = Splinter;
12
13 fn union(&self, rhs: &Self) -> Self::Output {
14 let mut out = self.clone();
15 out.merge(rhs);
16 out
17 }
18}
19
20impl<T: AsRef<[u8]>> Union<SplinterRef<T>> for Splinter {
22 type Output = Splinter;
23
24 fn union(&self, rhs: &SplinterRef<T>) -> Self::Output {
25 let mut out = self.clone();
26 out.merge(rhs);
27 out
28 }
29}
30
31impl<T: AsRef<[u8]>> Union<Splinter> for SplinterRef<T> {
33 type Output = Splinter;
34
35 fn union(&self, rhs: &Splinter) -> Self::Output {
36 rhs.union(self)
37 }
38}
39
40impl<T1, T2> Union<SplinterRef<T2>> for SplinterRef<T1>
42where
43 T1: AsRef<[u8]>,
44 T2: AsRef<[u8]>,
45{
46 type Output = Splinter;
47
48 fn union(&self, rhs: &SplinterRef<T2>) -> Self::Output {
49 let mut out = self.copy_to_owned();
50 out.merge(rhs);
51 out
52 }
53}
54
55impl<T1: AsRef<[u8]>> Union<Splinter> for CowSplinter<T1> {
57 type Output = Splinter;
58
59 fn union(&self, rhs: &Splinter) -> Self::Output {
60 match self {
61 CowSplinter::Owned(splinter) => splinter.union(rhs),
62 CowSplinter::Ref(splinter_ref) => rhs.union(splinter_ref),
63 }
64 }
65}
66
67impl<T1: AsRef<[u8]>, T2: AsRef<[u8]>> Union<SplinterRef<T2>> for CowSplinter<T1> {
69 type Output = Splinter;
70
71 fn union(&self, rhs: &SplinterRef<T2>) -> Self::Output {
72 match self {
73 CowSplinter::Owned(splinter) => splinter.union(rhs),
74 CowSplinter::Ref(splinter_ref) => splinter_ref.union(rhs),
75 }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use crate::{
82 Splinter,
83 ops::Union,
84 testutil::{TestSplinter, check_combinations},
85 };
86
87 impl Union for TestSplinter {
88 type Output = Splinter;
89
90 fn union(&self, rhs: &Self) -> Self::Output {
91 use TestSplinter::*;
92 match (self, rhs) {
93 (Splinter(lhs), Splinter(rhs)) => lhs.union(rhs),
94 (Splinter(lhs), SplinterRef(rhs)) => lhs.union(rhs),
95 (SplinterRef(lhs), Splinter(rhs)) => lhs.union(rhs),
96 (SplinterRef(lhs), SplinterRef(rhs)) => lhs.union(rhs),
97 }
98 }
99 }
100
101 #[test]
102 fn test_sanity() {
103 check_combinations(0..0, 0..0, 0..0, |lhs, rhs| lhs.union(&rhs));
104 check_combinations(0..100, 30..150, 0..150, |lhs, rhs| lhs.union(&rhs));
105 }
106}