Debian Codegraph @

src/macros.rs

raw symbols SCIP
1 //! Macros for internal implementations.
2
3 macro_rules! leaf {
4 ($($typ:ty),*) => {
5 $(
6 impl $crate::Diffable for $typ {
7 type Diff<'daft> = $crate::Leaf<&'daft Self>;
8
9 fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> {
10 $crate::Leaf {
11 before: self,
12 after: other
13 }
14 }
15 }
16 )*
17 }
18 }
19
20 #[cfg(feature = "alloc")]
21 macro_rules! leaf_deref {
22 ($($typ:ty => $target:ty),*) => {
23 $(
24 impl $crate::Diffable for $typ {
25 type Diff<'daft> = $crate::Leaf<&'daft $target>;
26
27 fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> {
28 $crate::Leaf {
29 before: &**self,
30 after: &**other
31 }
32 }
33 }
34 )*
35 };
36 }
37
38 /// Create a type `<MapType>Diff` and `impl Diffable` on it.
39 ///
40 /// This is supported for `BTreeMap` and `HashMap`
41 #[cfg(feature = "alloc")]
42 macro_rules! map_diff {
43 ($(#[$doc:meta])* $typ:ident, $key_constraint:ident) => {
44 paste::paste! {
45 $(#[$doc])*
46 #[derive(Debug, PartialEq, Eq)]
47 pub struct [<$typ Diff>]<'daft, K: $key_constraint + Eq, V> {
48 /// Entries common to both maps.
49 ///
50 /// Values are stored as `Leaf`s to references.
51 pub common: $typ<&'daft K, $crate::Leaf<&'daft V>>,
52
53 /// Entries present in the `after` map, but not in `before`.
54 pub added: $typ<&'daft K, &'daft V>,
55
56 /// Entries present in the `before` map, but not in `after`.
57 pub removed: $typ<&'daft K, &'daft V>,
58 }
59
60 impl<'daft, K: $key_constraint + Eq, V> [<$typ Diff>]<'daft, K, V> {
61 #[doc = "Create a new, empty `" $typ "Diff` instance."]
62 pub fn new() -> Self {
63 Self {
64 common: $typ::new(),
65 added: $typ::new(),
66 removed: $typ::new(),
67 }
68 }
69 }
70
71 impl<'daft, K: $key_constraint + Eq, V: Eq> [<$typ Diff>]<'daft, K, V> {
72 /// Return an iterator over unchanged keys and values.
73 pub fn unchanged(&self) -> impl Iterator<Item = (&'daft K, &'daft V)> + '_ {
74 self.common.iter().filter_map(|(k, leaf)| {
75 leaf.is_unchanged().then_some((*k, leaf.before))
76 })
77 }
78
79 /// Return true if the value corresponding to the key is
80 /// unchanged.
81 pub fn is_unchanged(&self, key: &K) -> bool {
82 self.common.get(key).is_some_and(|leaf| leaf.is_unchanged())
83 }
84
85 /// Return the value associated with the key if it is unchanged,
86 /// otherwise `None`.
87 pub fn get_unchanged(&self, key: &K) -> Option<&'daft V> {
88 self.common.get(key).and_then(|leaf| {
89 leaf.is_unchanged().then_some(leaf.before)
90 })
91 }
92
93 /// Return an iterator over unchanged keys.
94 pub fn unchanged_keys(&self) -> impl Iterator<Item = &'daft K> + '_ {
95 self.common.iter().filter_map(|(k, leaf)| {
96 leaf.is_unchanged().then_some(*k)
97 })
98 }
99
100 /// Return an iterator over unchanged values.
101 pub fn unchanged_values(&self) -> impl Iterator<Item = &'daft V> + '_ {
102 self.common.iter().filter_map(|(_, leaf)| {
103 leaf.is_unchanged().then_some(leaf.before)
104 })
105 }
106
107 /// Return an iterator over modified keys and values.
108 pub fn modified(&self) -> impl Iterator<Item = (&'daft K, $crate::Leaf<&'daft V>)> + '_ {
109 self.common.iter().filter_map(|(k, leaf)| {
110 leaf.is_modified().then_some((*k, *leaf))
111 })
112 }
113
114 /// Return true if the value corresponding to the key is
115 /// modified.
116 pub fn is_modified(&self, key: &K) -> bool {
117 self.common.get(key).is_some_and(|leaf| leaf.is_modified())
118 }
119
120 /// Return the `Leaf` associated with the key if it is modified,
121 /// otherwise `None`.
122 pub fn get_modified(&self, key: &K) -> Option<$crate::Leaf<&'daft V>> {
123 self.common.get(key).and_then(|leaf| {
124 leaf.is_modified().then_some(*leaf)
125 })
126 }
127
128 /// Return an iterator over modified keys.
129 pub fn modified_keys(&self) -> impl Iterator<Item = &'daft K> + '_ {
130 self.common.iter().filter_map(|(k, leaf)| {
131 leaf.is_modified().then_some(*k)
132 })
133 }
134
135 /// Return an iterator over modified values.
136 pub fn modified_values(&self) -> impl Iterator<Item = $crate::Leaf<&'daft V>> + '_ {
137 self.common.iter().filter_map(|(_, leaf)| {
138 leaf.is_modified().then_some(*leaf)
139 })
140 }
141
142 /// Return an iterator over modified keys and values, performing
143 /// a diff on the values.
144 ///
145 /// This is useful when `V::Diff` is a complex type, not just a
146 /// [`Leaf`](crate::Leaf).
147 #[allow(rustdoc::redundant_explicit_links)] // some macro use sites have Leaf available, some don't
148 pub fn modified_diff(&self) -> impl Iterator<Item = (&'daft K, V::Diff<'daft>)> + '_
149 where
150 V: Diffable,
151 {
152 self.modified().map(|(k, leaf)| (k, leaf.before.diff(&leaf.after)))
153 }
154
155 /// Return an iterator over modified values, performing a diff on
156 /// them.
157 ///
158 /// This is useful when `V::Diff` is a complex type, not just a
159 /// [`Leaf`](crate::Leaf).
160 #[allow(rustdoc::redundant_explicit_links)] // some macro use sites have Leaf available, some don't
161 pub fn modified_values_diff(&self) -> impl Iterator<Item = V::Diff<'daft>> + '_
162 where
163 V: Diffable,
164 {
165 self.modified_values().map(|leaf| leaf.before.diff(&leaf.after))
166 }
167 }
168
169 // Note: not deriving Default here because we don't want to require
170 // K or V to be Default.
171 impl<'daft, K: $key_constraint + Eq, V> Default for [<$typ Diff>]<'daft, K, V> {
172 fn default() -> Self {
173 Self::new()
174 }
175 }
176
177 impl<K: $key_constraint + Eq, V> $crate::Diffable for $typ<K, V>
178 {
179 type Diff<'daft> = [<$typ Diff>]<'daft, K, V> where K: 'daft, V: 'daft;
180
181 fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> {
182 let mut diff = [<$typ Diff>]::new();
183 for (k, v) in self {
184 if let Some(other_v) = other.get(k) {
185 diff.common.insert(k, $crate::Leaf { before: v, after: other_v });
186 } else {
187 diff.removed.insert(k, v);
188 }
189 }
190 for (k, v) in other {
191 if !self.contains_key(k) {
192 diff.added.insert(k, v);
193 }
194 }
195 diff
196 }
197 }
198 }
199 }
200 }
201
202 /// Create a type `<SetType>Diff` and `impl Diffable` on it.
203 ///
204 /// This is supported for `BTreeSet` and `HashSet`.
205 #[cfg(feature = "alloc")]
206 macro_rules! set_diff {
207 ($(#[$doc:meta])* $typ:ident, $key_constraint:ident) => {
208 paste::paste! {
209 $(#[$doc])*
210 #[derive(Debug, PartialEq, Eq)]
211 pub struct [<$typ Diff>]<'daft, K: $key_constraint + Eq> {
212 /// Entries common to both sets.
213 pub common: $typ<&'daft K>,
214
215 /// Entries present in the `after` set, but not in `before`.
216 pub added: $typ<&'daft K>,
217
218 /// Entries present in the `before` set, but not in `after`.
219 pub removed: $typ<&'daft K>,
220 }
221
222 impl<'daft, K: $key_constraint + Eq> [<$typ Diff>]<'daft, K> {
223 #[doc = "Create a new, empty `" $typ "Diff` instance."]
224 pub fn new() -> Self {
225 Self {
226 common: $typ::new(),
227 added: $typ::new(),
228 removed: $typ::new(),
229 }
230 }
231 }
232
233 // Note: not deriving Default here because we don't want to require
234 // K to be Default.
235 impl<'daft, K: $key_constraint + Eq> Default for [<$typ Diff>]<'daft, K> {
236 fn default() -> Self {
237 Self::new()
238 }
239 }
240
241 impl<K: $key_constraint + Eq>
242 $crate::Diffable for $typ<K>
243 {
244 type Diff<'daft> = [<$typ Diff>]<'daft, K> where K: 'daft;
245
246 fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft> {
247 let mut diff = [<$typ Diff>]::new();
248 diff.removed = self.difference(other).collect();
249 diff.added = other.difference(self).collect();
250 diff.common = self.intersection(other).collect();
251 diff
252 }
253 }
254 }
255 }
256 }