Skip to main content

dag/
delegate.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8/// Macro rules to delegate trait implementations
9
10#[macro_export]
11macro_rules! delegate {
12    {IdConvert { impl $($impl:tt)* } => self.$($t:tt)*} => {
13        impl $($impl)* {
14            fn vertex_id<'a: 's, 's>(&'a self, name: $crate::Vertex)
15                -> std::pin::Pin<Box<dyn std::future::Future<Output=
16                        $crate::Result<$crate::Id>
17                    > + Send + 's>> where Self: 's
18            {
19                self.$($t)*.vertex_id(name)
20            }
21            fn vertex_id_with_max_group<'a: 's, 'b: 's, 's>(&'a self, name: &'b $crate::Vertex, max_group: $crate::Group)
22                -> std::pin::Pin<Box<dyn std::future::Future<Output=
23                        $crate::Result<Option<$crate::Id>>
24                    > + Send + 's>> where Self: 's
25            {
26                self.$($t)*.vertex_id_with_max_group(name, max_group)
27            }
28            fn vertex_name<'a: 's, 's>(&'a self, id: $crate::Id)
29                -> std::pin::Pin<Box<dyn std::future::Future<Output=
30                        $crate::Result<$crate::Vertex>
31                    > + Send + 's>> where Self: 's
32            {
33                self.$($t)*.vertex_name(id)
34            }
35            fn contains_vertex_name<'a: 's, 'b: 's, 's>(&'a self, name: &'b $crate::Vertex)
36                -> std::pin::Pin<Box<dyn std::future::Future<Output=
37                        $crate::Result<bool>
38                    > + Send + 's>> where Self: 's
39            {
40                self.$($t)*.contains_vertex_name(name)
41            }
42            fn vertex_id_optional<'a: 's, 'b: 's, 's>(&'a self, name: &'b $crate::Vertex)
43                -> std::pin::Pin<Box<dyn std::future::Future<Output=
44                        $crate::Result<Option<$crate::Id>>
45                    > + Send + 's>> where Self: 's
46            {
47                self.$($t)*.vertex_id_optional(name)
48            }
49            fn contains_vertex_id_locally<'a: 's, 'b: 's, 's>(&'a self, ids: &'b [$crate::Id])
50                -> std::pin::Pin<Box<dyn std::future::Future<Output=
51                        $crate::Result<Vec<bool>>
52                    > + Send + 's>> where Self: 's
53            {
54                self.$($t)*.contains_vertex_id_locally(ids)
55            }
56            fn contains_vertex_name_locally<'a: 's, 'b: 's, 's>(&'a self, names: &'b [$crate::Vertex])
57                -> std::pin::Pin<Box<dyn std::future::Future<Output=
58                        $crate::Result<Vec<bool>>
59                    > + Send + 's>> where Self: 's
60            {
61                self.$($t)*.contains_vertex_name_locally(names)
62            }
63            fn vertex_name_batch<'a: 's, 'b: 's, 's>(&'a self, ids: &'b [$crate::Id])
64                -> std::pin::Pin<Box<dyn std::future::Future<Output=
65                        $crate::Result<Vec<$crate::Result<$crate::Vertex>>>
66                    > + Send + 's>> where Self: 's
67            {
68                self.$($t)*.vertex_name_batch(ids)
69            }
70            fn vertex_id_batch<'a: 's, 'b: 's, 's>(&'a self, names: &'b [$crate::Vertex])
71                -> std::pin::Pin<Box<dyn std::future::Future<Output=
72                        $crate::Result<Vec<$crate::Result<$crate::Id>>>
73                    > + Send + 's>> where Self: 's
74            {
75                self.$($t)*.vertex_id_batch(names)
76            }
77            fn map_id(&self) -> &str {
78                self.$($t)*.map_id()
79            }
80            fn map_version(&self) -> &$crate::VerLink {
81                self.$($t)*.map_version()
82            }
83        }
84    };
85
86    (IdConvert, $type:ty => self.$($t:tt)*) => {
87        delegate! { IdConvert { impl $crate::ops::IdConvert for $type } => self.$($t)* }
88    };
89
90    {PrefixLookup { impl $($impl:tt)* } => self.$($t:tt)*} => {
91        impl $($impl)* {
92            fn vertexes_by_hex_prefix<'a: 'c, 'b: 'c, 'c>(&'a self, hex_prefix: &'b [u8], limit: usize) -> std::pin::Pin<Box<dyn std::future::Future<Output=$crate::Result<Vec<$crate::Vertex>>> + Send + 'c>> where Self: 'c {
93                self.$($t)*.vertexes_by_hex_prefix(hex_prefix, limit)
94            }
95        }
96    };
97
98    (PrefixLookup, $type:ty => self.$($t:tt)*) => {
99        delegate! { PrefixLookup { impl $crate::ops::PrefixLookup for $type } => self.$($t)* }
100    };
101
102    (ToIdSet, $type:ty => self.$($t:tt)*) => {
103        impl $crate::ops::ToIdSet for $type {
104            fn to_id_set(&self, set: &$crate::Set) -> $crate::Result<$crate::IdSet> {
105                self.$($t)*.to_id_set(set)
106            }
107        }
108    };
109
110    (ToSet, $type:ty => self.$($t:tt)*) => {
111        impl $crate::ops::ToSet for $type {
112            fn to_set(&self, set: &$crate::IdSet) -> $crate::Result<$crate::Set> {
113                self.$($t)*.to_set(set)
114            }
115            fn id_list_to_set(&self, list: &$crate::IdList) -> $crate::Result<$crate::Set> {
116                self.$($t)*.id_list_to_set(list)
117            }
118        }
119    };
120
121    (DagAlgorithm, $type:ty => self.$($t:tt)*) => {
122        impl $crate::DagAlgorithm for $type {
123            fn sort<'a: 'c, 'b: 'c, 'c>(&'a self, set: &'b $crate::Set)
124                -> std::pin::Pin<Box<dyn std::future::Future<Output=
125                        $crate::Result<$crate::Set>
126                    > + Send + 'c>> where Self: 'c
127            {
128                self.$($t)*.sort(set)
129            }
130            fn parent_names<'a: 'c, 'c>(&'a self, name: $crate::Vertex)
131                -> std::pin::Pin<Box<dyn std::future::Future<Output=
132                        $crate::Result<Vec<$crate::Vertex>>
133                    > + Send + 'c>> where Self: 'c
134            {
135                self.$($t)*.parent_names(name)
136            }
137            fn all<'a: 's, 's>(&'a self)
138                -> std::pin::Pin<Box<dyn std::future::Future<Output=
139                        $crate::Result<$crate::Set>
140                    > + Send + 's>> where Self: 's
141            {
142                self.$($t)*.all()
143            }
144            fn master_group<'a: 's, 's>(&'a self)
145                -> std::pin::Pin<Box<dyn std::future::Future<Output=
146                        $crate::Result<$crate::Set>
147                    > + Send + 's>> where Self: 's
148            {
149                self.$($t)*.master_group()
150            }
151            fn virtual_group<'a: 's, 's>(&'a self)
152                -> std::pin::Pin<Box<dyn std::future::Future<Output=
153                        $crate::Result<$crate::Set>
154                    > + Send + 's>> where Self: 's
155            {
156                self.$($t)*.virtual_group()
157            }
158            fn ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
159                -> std::pin::Pin<Box<dyn std::future::Future<Output=
160                        $crate::Result<$crate::Set>
161                    > + Send + 's>> where Self: 's
162            {
163                self.$($t)*.ancestors(set)
164            }
165            fn first_ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
166                -> std::pin::Pin<Box<dyn std::future::Future<Output=
167                        $crate::Result<$crate::Set>
168                    > + Send + 's>> where Self: 's
169            {
170                self.$($t)*.first_ancestors(set)
171            }
172            fn parents<'a: 's, 's>(&'a self, set: $crate::Set)
173                -> std::pin::Pin<Box<dyn std::future::Future<Output=
174                        $crate::Result<$crate::Set>
175                    > + Send + 's>> where Self: 's
176            {
177                self.$($t)*.parents(set)
178            }
179            fn merges<'a: 's, 's>(&'a self, set: $crate::Set)
180                -> std::pin::Pin<Box<dyn std::future::Future<Output=
181                        $crate::Result<$crate::Set>
182                    > + Send + 's>> where Self: 's
183            {
184                self.$($t)*.merges(set)
185            }
186            fn first_ancestor_nth<'a: 's, 's>(&'a self, name: $crate::Vertex, n: u64)
187                -> std::pin::Pin<Box<dyn std::future::Future<Output=
188                        $crate::Result<Option<$crate::Vertex>>
189                    > + Send + 's>> where Self: 's
190            {
191                self.$($t)*.first_ancestor_nth(name, n)
192            }
193            fn heads<'a: 's, 's>(&'a self, set: $crate::Set)
194                -> std::pin::Pin<Box<dyn std::future::Future<Output=
195                        $crate::Result<$crate::Set>
196                    > + Send + 's>> where Self: 's
197            {
198                self.$($t)*.heads(set)
199            }
200            fn children<'a: 's, 's>(&'a self, set: $crate::Set)
201                -> std::pin::Pin<Box<dyn std::future::Future<Output=
202                        $crate::Result<$crate::Set>
203                    > + Send + 's>> where Self: 's
204            {
205                self.$($t)*.children(set)
206            }
207            fn roots<'a: 's, 's>(&'a self, set: $crate::Set)
208                -> std::pin::Pin<Box<dyn std::future::Future<Output=
209                        $crate::Result<$crate::Set>
210                    > + Send + 's>> where Self: 's
211            {
212                self.$($t)*.roots(set)
213            }
214            fn gca_one<'a: 's, 's>(&'a self, set: $crate::Set)
215                -> std::pin::Pin<Box<dyn std::future::Future<Output=
216                        $crate::Result<Option<$crate::Vertex>>
217                    > + Send + 's>> where Self: 's
218            {
219                self.$($t)*.gca_one(set)
220            }
221            fn gca_all<'a: 's, 's>(&'a self, set: $crate::Set)
222                -> std::pin::Pin<Box<dyn std::future::Future<Output=
223                        $crate::Result<$crate::Set>
224                    > + Send + 's>> where Self: 's
225            {
226                self.$($t)*.gca_all(set)
227            }
228            fn common_ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
229                -> std::pin::Pin<Box<dyn std::future::Future<Output=
230                        $crate::Result<$crate::Set>
231                    > + Send + 's>> where Self: 's
232            {
233                self.$($t)*.common_ancestors(set)
234            }
235            fn is_ancestor<'a: 's, 's>(&'a self, ancestor: $crate::Vertex, descendant: $crate::Vertex)
236                -> std::pin::Pin<Box<dyn std::future::Future<Output=
237                        $crate::Result<bool>
238                    > + Send + 's>> where Self: 's
239            {
240                self.$($t)*.is_ancestor(ancestor, descendant)
241            }
242            fn heads_ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
243                -> std::pin::Pin<Box<dyn std::future::Future<Output=
244                        $crate::Result<$crate::Set>
245                    > + Send + 's>> where Self: 's
246            {
247                self.$($t)*.heads_ancestors(set)
248            }
249            fn range<'a: 's, 's>(&'a self, roots: $crate::Set, heads: $crate::Set)
250                -> std::pin::Pin<Box<dyn std::future::Future<Output=
251                        $crate::Result<$crate::Set>
252                    > + Send + 's>> where Self: 's
253            {
254                self.$($t)*.range(roots, heads)
255            }
256            fn only<'a: 's, 's>(&'a self, reachable: $crate::Set, unreachable: $crate::Set)
257                -> std::pin::Pin<Box<dyn std::future::Future<Output=
258                        $crate::Result<$crate::Set>
259                    > + Send + 's>> where Self: 's
260            {
261                self.$($t)*.only(reachable, unreachable)
262            }
263            fn only_both<'a: 's, 's>(&'a self, reachable: $crate::Set, unreachable: $crate::Set)
264                -> std::pin::Pin<Box<dyn std::future::Future<Output=
265                        $crate::Result<($crate::Set, $crate::Set)>
266                    > + Send + 's>> where Self: 's
267            {
268                self.$($t)*.only_both(reachable, unreachable)
269            }
270            fn descendants<'a: 's, 's>(&'a self, set: $crate::Set)
271                -> std::pin::Pin<Box<dyn std::future::Future<Output=
272                        $crate::Result<$crate::Set>
273                    > + Send + 's>> where Self: 's
274            {
275                self.$($t)*.descendants(set)
276            }
277            fn reachable_roots<'a: 's, 's>(&'a self, roots: $crate::Set, heads: $crate::Set)
278                -> std::pin::Pin<Box<dyn std::future::Future<Output=
279                        $crate::Result<$crate::Set>
280                    > + Send + 's>> where Self: 's
281            {
282                self.$($t)*.reachable_roots(roots, heads)
283            }
284            fn suggest_bisect<'a: 's, 's>(
285                &'a self,
286                roots: $crate::Set,
287                heads: $crate::Set,
288                skip: $crate::Set,
289            ) -> std::pin::Pin<Box<dyn std::future::Future<Output=
290                    $crate::Result<(Option<$crate::Vertex>, $crate::Set, $crate::Set)>
291                > + Send + 's>> where Self: 's
292            {
293                self.$($t)*.suggest_bisect(roots, heads, skip)
294            }
295            fn dirty<'a: 's, 's>(&'a self)
296                -> std::pin::Pin<Box<dyn std::future::Future<Output=
297                        $crate::Result<$crate::Set>
298                    > + Send + 's>> where Self: 's
299            {
300                self.$($t)*.dirty()
301            }
302            fn is_vertex_lazy(&self) -> bool {
303                self.$($t)*.is_vertex_lazy()
304            }
305            fn dag_snapshot(&self)
306                -> $crate::Result<std::sync::Arc<dyn $crate::DagAlgorithm + Send + Sync>>
307            {
308                self.$($t)*.dag_snapshot()
309            }
310            fn id_dag_snapshot(&self)
311                -> $crate::Result<std::sync::Arc<dyn $crate::IdDagAlgorithm + Send + Sync>>
312            {
313                self.$($t)*.id_dag_snapshot()
314            }
315            fn dag_id(&self) -> &str {
316                self.$($t)*.dag_id()
317            }
318            fn dag_version(&self) -> &$crate::VerLink {
319                self.$($t)*.dag_version()
320            }
321        }
322    };
323
324    (IdMapSnapshot, $type:ty => self.$($t:tt)*) => {
325        impl $crate::ops::IdMapSnapshot for $type {
326            fn id_map_snapshot(&self) -> $crate::Result<std::sync::Arc<dyn $crate::ops::IdConvert + Send + Sync>> {
327                self.$($t)*.id_map_snapshot()
328            }
329        }
330    };
331
332    (CheckIntegrity, $type:ty => self.$($t:tt)*) => {
333        impl $crate::ops::CheckIntegrity for $type {
334            fn check_universal_ids<'a: 's, 's>(&'a self)
335                -> std::pin::Pin<Box<dyn std::future::Future<Output=
336                        $crate::Result<Vec<$crate::Id>>
337                    > + Send + 's>> where Self: 's
338            {
339                self.$($t)*.check_universal_ids()
340            }
341            fn check_segments<'a: 's, 's>(&'a self)
342                -> std::pin::Pin<Box<dyn std::future::Future<Output=
343                        $crate::Result<Vec<String>>
344                    > + Send + 's>> where Self: 's
345            {
346                self.$($t)*.check_segments()
347            }
348            fn check_isomorphic_graph<'a: 's, 'b: 's, 's> (
349                &'a self,
350                other: &'b dyn $crate::ops::DagAlgorithm,
351                heads: $crate::Set,
352            )
353                -> std::pin::Pin<Box<dyn std::future::Future<Output=
354                        $crate::Result<Vec<String>>
355                    > + Send + 's>> where Self: 's
356            {
357                self.$($t)*.check_isomorphic_graph(other, heads)
358            }
359        }
360    };
361
362    ($name:ident | $name2:ident $(| $name3:ident )*, $type:ty => self.$($t:tt)*) => {
363        delegate!($name, $type => self.$($t)*);
364        delegate!($name2 $(| $name3 )*, $type => self.$($t)*);
365    };
366}
367
368mod impls {
369    use std::ops::Deref;
370    use std::sync::Arc;
371
372    use crate::ops::DagAlgorithm;
373    use crate::ops::IdConvert;
374
375    delegate!(IdConvert | PrefixLookup, Arc<dyn IdConvert + Send + Sync> => self.deref());
376    delegate!(DagAlgorithm, Arc<dyn DagAlgorithm + Send + Sync> => self.deref());
377}