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_with_max_group(name, $crate::Group::NON_MASTER)
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::VertexName])
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::VertexName>>>
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::VertexName])
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        }
116    };
117
118    (DagAlgorithm, $type:ty => self.$($t:tt)*) => {
119        impl $crate::DagAlgorithm for $type {
120            fn sort<'a: 'c, 'b: 'c, 'c>(&'a self, set: &'b $crate::Set)
121                -> std::pin::Pin<Box<dyn std::future::Future<Output=
122                        $crate::Result<$crate::Set>
123                    > + Send + 'c>> where Self: 'c
124            {
125                self.$($t)*.sort(set)
126            }
127            fn parent_names<'a: 'c, 'c>(&'a self, name: $crate::Vertex)
128                -> std::pin::Pin<Box<dyn std::future::Future<Output=
129                        $crate::Result<Vec<$crate::Vertex>>
130                    > + Send + 'c>> where Self: 'c
131            {
132                self.$($t)*.parent_names(name)
133            }
134            fn all<'a: 's, 's>(&'a self)
135                -> std::pin::Pin<Box<dyn std::future::Future<Output=
136                        $crate::Result<$crate::Set>
137                    > + Send + 's>> where Self: 's
138            {
139                self.$($t)*.all()
140            }
141            fn master_group<'a: 's, 's>(&'a self)
142                -> std::pin::Pin<Box<dyn std::future::Future<Output=
143                        $crate::Result<$crate::Set>
144                    > + Send + 's>> where Self: 's
145            {
146                self.$($t)*.master_group()
147            }
148            fn ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
149                -> std::pin::Pin<Box<dyn std::future::Future<Output=
150                        $crate::Result<$crate::Set>
151                    > + Send + 's>> where Self: 's
152            {
153                self.$($t)*.ancestors(set)
154            }
155            fn first_ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
156                -> std::pin::Pin<Box<dyn std::future::Future<Output=
157                        $crate::Result<$crate::Set>
158                    > + Send + 's>> where Self: 's
159            {
160                self.$($t)*.first_ancestors(set)
161            }
162            fn parents<'a: 's, 's>(&'a self, set: $crate::Set)
163                -> std::pin::Pin<Box<dyn std::future::Future<Output=
164                        $crate::Result<$crate::Set>
165                    > + Send + 's>> where Self: 's
166            {
167                self.$($t)*.parents(set)
168            }
169            fn merges<'a: 's, 's>(&'a self, set: $crate::Set)
170                -> std::pin::Pin<Box<dyn std::future::Future<Output=
171                        $crate::Result<$crate::Set>
172                    > + Send + 's>> where Self: 's
173            {
174                self.$($t)*.merges(set)
175            }
176            fn first_ancestor_nth<'a: 's, 's>(&'a self, name: $crate::Vertex, n: u64)
177                -> std::pin::Pin<Box<dyn std::future::Future<Output=
178                        $crate::Result<Option<$crate::Vertex>>
179                    > + Send + 's>> where Self: 's
180            {
181                self.$($t)*.first_ancestor_nth(name, n)
182            }
183            fn heads<'a: 's, 's>(&'a self, set: $crate::Set)
184                -> std::pin::Pin<Box<dyn std::future::Future<Output=
185                        $crate::Result<$crate::Set>
186                    > + Send + 's>> where Self: 's
187            {
188                self.$($t)*.heads(set)
189            }
190            fn children<'a: 's, 's>(&'a self, set: $crate::Set)
191                -> std::pin::Pin<Box<dyn std::future::Future<Output=
192                        $crate::Result<$crate::Set>
193                    > + Send + 's>> where Self: 's
194            {
195                self.$($t)*.children(set)
196            }
197            fn roots<'a: 's, 's>(&'a self, set: $crate::Set)
198                -> std::pin::Pin<Box<dyn std::future::Future<Output=
199                        $crate::Result<$crate::Set>
200                    > + Send + 's>> where Self: 's
201            {
202                self.$($t)*.roots(set)
203            }
204            fn gca_one<'a: 's, 's>(&'a self, set: $crate::Set)
205                -> std::pin::Pin<Box<dyn std::future::Future<Output=
206                        $crate::Result<Option<$crate::Vertex>>
207                    > + Send + 's>> where Self: 's
208            {
209                self.$($t)*.gca_one(set)
210            }
211            fn gca_all<'a: 's, 's>(&'a self, set: $crate::Set)
212                -> std::pin::Pin<Box<dyn std::future::Future<Output=
213                        $crate::Result<$crate::Set>
214                    > + Send + 's>> where Self: 's
215            {
216                self.$($t)*.gca_all(set)
217            }
218            fn common_ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
219                -> std::pin::Pin<Box<dyn std::future::Future<Output=
220                        $crate::Result<$crate::Set>
221                    > + Send + 's>> where Self: 's
222            {
223                self.$($t)*.common_ancestors(set)
224            }
225            fn is_ancestor<'a: 's, 's>(&'a self, ancestor: $crate::Vertex, descendant: $crate::Vertex)
226                -> std::pin::Pin<Box<dyn std::future::Future<Output=
227                        $crate::Result<bool>
228                    > + Send + 's>> where Self: 's
229            {
230                self.$($t)*.is_ancestor(ancestor, descendant)
231            }
232            fn heads_ancestors<'a: 's, 's>(&'a self, set: $crate::Set)
233                -> std::pin::Pin<Box<dyn std::future::Future<Output=
234                        $crate::Result<$crate::Set>
235                    > + Send + 's>> where Self: 's
236            {
237                self.$($t)*.heads_ancestors(set)
238            }
239            fn range<'a: 's, 's>(&'a self, roots: $crate::Set, heads: $crate::Set)
240                -> std::pin::Pin<Box<dyn std::future::Future<Output=
241                        $crate::Result<$crate::Set>
242                    > + Send + 's>> where Self: 's
243            {
244                self.$($t)*.range(roots, heads)
245            }
246            fn only<'a: 's, 's>(&'a self, reachable: $crate::Set, unreachable: $crate::Set)
247                -> std::pin::Pin<Box<dyn std::future::Future<Output=
248                        $crate::Result<$crate::Set>
249                    > + Send + 's>> where Self: 's
250            {
251                self.$($t)*.only(reachable, unreachable)
252            }
253            fn only_both<'a: 's, 's>(&'a self, reachable: $crate::Set, unreachable: $crate::Set)
254                -> std::pin::Pin<Box<dyn std::future::Future<Output=
255                        $crate::Result<($crate::Set, $crate::Set)>
256                    > + Send + 's>> where Self: 's
257            {
258                self.$($t)*.only_both(reachable, unreachable)
259            }
260            fn descendants<'a: 's, 's>(&'a self, set: $crate::Set)
261                -> std::pin::Pin<Box<dyn std::future::Future<Output=
262                        $crate::Result<$crate::Set>
263                    > + Send + 's>> where Self: 's
264            {
265                self.$($t)*.descendants(set)
266            }
267            fn reachable_roots<'a: 's, 's>(&'a self, roots: $crate::Set, heads: $crate::Set)
268                -> std::pin::Pin<Box<dyn std::future::Future<Output=
269                        $crate::Result<$crate::Set>
270                    > + Send + 's>> where Self: 's
271            {
272                self.$($t)*.reachable_roots(roots, heads)
273            }
274            fn dirty<'a: 's, 's>(&'a self)
275                -> std::pin::Pin<Box<dyn std::future::Future<Output=
276                        $crate::Result<$crate::Set>
277                    > + Send + 's>> where Self: 's
278            {
279                self.$($t)*.dirty()
280            }
281            fn is_vertex_lazy(&self) -> bool {
282                self.$($t)*.is_vertex_lazy()
283            }
284            fn dag_snapshot(&self)
285                -> $crate::Result<std::sync::Arc<dyn $crate::DagAlgorithm + Send + Sync>>
286            {
287                self.$($t)*.dag_snapshot()
288            }
289            fn id_dag_snapshot(&self)
290                -> $crate::Result<std::sync::Arc<dyn $crate::IdDagAlgorithm + Send + Sync>>
291            {
292                self.$($t)*.id_dag_snapshot()
293            }
294            fn dag_id(&self) -> &str {
295                self.$($t)*.dag_id()
296            }
297            fn dag_version(&self) -> &$crate::VerLink {
298                self.$($t)*.dag_version()
299            }
300        }
301    };
302
303    (IdMapSnapshot, $type:ty => self.$($t:tt)*) => {
304        impl $crate::ops::IdMapSnapshot for $type {
305            fn id_map_snapshot(&self) -> $crate::Result<std::sync::Arc<dyn $crate::ops::IdConvert + Send + Sync>> {
306                self.$($t)*.id_map_snapshot()
307            }
308        }
309    };
310
311    (CheckIntegrity, $type:ty => self.$($t:tt)*) => {
312        impl $crate::ops::CheckIntegrity for $type {
313            fn check_universal_ids<'a: 's, 's>(&'a self)
314                -> std::pin::Pin<Box<dyn std::future::Future<Output=
315                        $crate::Result<Vec<$crate::Id>>
316                    > + Send + 's>> where Self: 's
317            {
318                self.$($t)*.check_universal_ids()
319            }
320            fn check_segments<'a: 's, 's>(&'a self)
321                -> std::pin::Pin<Box<dyn std::future::Future<Output=
322                        $crate::Result<Vec<String>>
323                    > + Send + 's>> where Self: 's
324            {
325                self.$($t)*.check_segments()
326            }
327            fn check_isomorphic_graph<'a: 's, 'b: 's, 's> (
328                &'a self,
329                other: &'b dyn $crate::ops::DagAlgorithm,
330                heads: $crate::NameSet,
331            )
332                -> std::pin::Pin<Box<dyn std::future::Future<Output=
333                        $crate::Result<Vec<String>>
334                    > + Send + 's>> where Self: 's
335            {
336                self.$($t)*.check_isomorphic_graph(other, heads)
337            }
338        }
339    };
340
341    ($name:ident | $name2:ident $(| $name3:ident )*, $type:ty => self.$($t:tt)*) => {
342        delegate!($name, $type => self.$($t)*);
343        delegate!($name2 $(| $name3 )*, $type => self.$($t)*);
344    };
345}
346
347mod impls {
348    use std::ops::Deref;
349    use std::sync::Arc;
350
351    use crate::ops::DagAlgorithm;
352    use crate::ops::IdConvert;
353
354    delegate!(IdConvert | PrefixLookup, Arc<dyn IdConvert + Send + Sync> => self.deref());
355    delegate!(DagAlgorithm, Arc<dyn DagAlgorithm + Send + Sync> => self.deref());
356    delegate!(DagAlgorithm, &(dyn DagAlgorithm + Send + Sync) => self.deref());
357}