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