zenoh_keyexpr/keyexpr_tree/traits/
default_impls.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14
15use alloc::{boxed::Box, sync::Arc};
16
17use token_cell::prelude::{TokenCell, TokenCellTrait, TokenTrait};
18
19use super::*;
20
21impl<T: HasChunk> HasChunk for &T {
22    fn chunk(&self) -> &keyexpr {
23        T::chunk(self)
24    }
25}
26impl<T: HasChunk> HasChunk for &mut T {
27    fn chunk(&self) -> &keyexpr {
28        T::chunk(self)
29    }
30}
31impl<T: HasChunk> HasChunk for Box<T> {
32    fn chunk(&self) -> &keyexpr {
33        T::chunk(self)
34    }
35}
36impl<T: HasChunk> HasChunk for Arc<T> {
37    fn chunk(&self) -> &keyexpr {
38        T::chunk(self)
39    }
40}
41impl<T: HasChunk, Token: TokenTrait> HasChunk for TokenCell<T, Token> {
42    fn chunk(&self) -> &keyexpr {
43        T::chunk(unsafe { &*self.get() })
44    }
45}
46impl<T> AsNode<T> for T {
47    fn as_node(&self) -> &T {
48        self
49    }
50}
51impl<T> AsNode<T> for &T {
52    fn as_node(&self) -> &T {
53        self
54    }
55}
56impl<T> AsNode<T> for &mut T {
57    fn as_node(&self) -> &T {
58        self
59    }
60}
61impl<T> AsNodeMut<T> for T {
62    fn as_node_mut(&mut self) -> &mut T {
63        self
64    }
65}
66impl<T> AsNodeMut<T> for &mut T {
67    fn as_node_mut(&mut self) -> &mut T {
68        self
69    }
70}
71impl<T: IKeyExprTreeNode<Weight>, Weight> IKeyExprTreeNode<Weight> for &T {}
72impl<T: IKeyExprTreeNode<Weight>, Weight> UIKeyExprTreeNode<Weight> for &T {
73    type Parent = T::Parent;
74    unsafe fn __parent(&self) -> Option<&Self::Parent> {
75        T::__parent(self)
76    }
77    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
78        T::__keyexpr(self)
79    }
80    unsafe fn __weight(&self) -> Option<&Weight> {
81        T::__weight(self)
82    }
83
84    type Child = T::Child;
85    type Children = T::Children;
86
87    unsafe fn __children(&self) -> &Self::Children {
88        T::__children(self)
89    }
90}
91impl<T: IKeyExprTreeNode<Weight>, Weight> IKeyExprTreeNode<Weight> for &mut T {}
92impl<T: IKeyExprTreeNode<Weight>, Weight> UIKeyExprTreeNode<Weight> for &mut T {
93    type Parent = T::Parent;
94    unsafe fn __parent(&self) -> Option<&Self::Parent> {
95        T::__parent(self)
96    }
97    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
98        T::__keyexpr(self)
99    }
100    unsafe fn __weight(&self) -> Option<&Weight> {
101        T::__weight(self)
102    }
103
104    type Child = T::Child;
105    type Children = T::Children;
106
107    unsafe fn __children(&self) -> &Self::Children {
108        T::__children(self)
109    }
110}
111impl<T: IKeyExprTreeNode<Weight>, Weight> IKeyExprTreeNode<Weight> for Box<T> {}
112impl<T: IKeyExprTreeNode<Weight>, Weight> UIKeyExprTreeNode<Weight> for Box<T> {
113    type Parent = T::Parent;
114    unsafe fn __parent(&self) -> Option<&Self::Parent> {
115        T::__parent(self)
116    }
117    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
118        T::__keyexpr(self)
119    }
120    unsafe fn __weight(&self) -> Option<&Weight> {
121        T::__weight(self)
122    }
123
124    type Child = T::Child;
125    type Children = T::Children;
126
127    unsafe fn __children(&self) -> &Self::Children {
128        T::__children(self)
129    }
130}
131
132impl<T: IKeyExprTreeNodeMut<Weight>, Weight> IKeyExprTreeNodeMut<Weight> for &mut T {
133    fn parent_mut(&mut self) -> Option<&mut Self::Parent> {
134        T::parent_mut(self)
135    }
136    fn weight_mut(&mut self) -> Option<&mut Weight> {
137        T::weight_mut(self)
138    }
139    fn take_weight(&mut self) -> Option<Weight> {
140        T::take_weight(self)
141    }
142    fn insert_weight(&mut self, weight: Weight) -> Option<Weight> {
143        T::insert_weight(self, weight)
144    }
145
146    fn children_mut(&mut self) -> &mut Self::Children {
147        T::children_mut(self)
148    }
149}
150
151impl<T: IKeyExprTreeNodeMut<Weight>, Weight> IKeyExprTreeNodeMut<Weight> for Box<T> {
152    fn parent_mut(&mut self) -> Option<&mut Self::Parent> {
153        T::parent_mut(self)
154    }
155    fn weight_mut(&mut self) -> Option<&mut Weight> {
156        T::weight_mut(self)
157    }
158    fn take_weight(&mut self) -> Option<Weight> {
159        T::take_weight(self)
160    }
161    fn insert_weight(&mut self, weight: Weight) -> Option<Weight> {
162        T::insert_weight(self, weight)
163    }
164
165    fn children_mut(&mut self) -> &mut Self::Children {
166        T::children_mut(self)
167    }
168}
169
170impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
171    for (&TokenCell<T, Token>, &Token)
172{
173}
174impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
175    for (&TokenCell<T, Token>, &Token)
176{
177    type Parent = T::Parent;
178    unsafe fn __parent(&self) -> Option<&Self::Parent> {
179        self.0
180            .try_borrow(self.1)
181            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
182            .__parent()
183    }
184    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
185        self.0
186            .try_borrow(self.1)
187            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
188            .__keyexpr()
189    }
190    unsafe fn __weight(&self) -> Option<&Weight> {
191        self.0
192            .try_borrow(self.1)
193            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
194            .__weight()
195    }
196
197    type Child = T::Child;
198    type Children = T::Children;
199
200    unsafe fn __children(&self) -> &Self::Children {
201        self.0
202            .try_borrow(self.1)
203            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
204            .__children()
205    }
206}
207
208impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
209    for (&TokenCell<T, Token>, &mut Token)
210{
211}
212impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
213    for (&TokenCell<T, Token>, &mut Token)
214{
215    type Parent = T::Parent;
216    unsafe fn __parent(&self) -> Option<&Self::Parent> {
217        self.0
218            .try_borrow(self.1)
219            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
220            .__parent()
221    }
222    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
223        self.0
224            .try_borrow(self.1)
225            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
226            .__keyexpr()
227    }
228    unsafe fn __weight(&self) -> Option<&Weight> {
229        self.0
230            .try_borrow(self.1)
231            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
232            .__weight()
233    }
234
235    type Child = T::Child;
236    type Children = T::Children;
237
238    unsafe fn __children(&self) -> &Self::Children {
239        self.0
240            .try_borrow(self.1)
241            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
242            .__children()
243    }
244}
245
246impl<T: IKeyExprTreeNodeMut<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNodeMut<Weight>
247    for (&TokenCell<T, Token>, &mut Token)
248{
249    fn parent_mut(&mut self) -> Option<&mut Self::Parent> {
250        self.0
251            .try_borrow_mut(self.1)
252            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
253            .parent_mut()
254    }
255    fn weight_mut(&mut self) -> Option<&mut Weight> {
256        self.0
257            .try_borrow_mut(self.1)
258            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
259            .weight_mut()
260    }
261    fn take_weight(&mut self) -> Option<Weight> {
262        self.0
263            .try_borrow_mut(self.1)
264            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
265            .take_weight()
266    }
267    fn insert_weight(&mut self, weight: Weight) -> Option<Weight> {
268        self.0
269            .try_borrow_mut(self.1)
270            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
271            .insert_weight(weight)
272    }
273
274    fn children_mut(&mut self) -> &mut Self::Children {
275        self.0
276            .try_borrow_mut(self.1)
277            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
278            .children_mut()
279    }
280}
281
282impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
283    for (&Arc<TokenCell<T, Token>>, &Token)
284{
285}
286impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
287    for (&Arc<TokenCell<T, Token>>, &Token)
288{
289    type Parent = T::Parent;
290    unsafe fn __parent(&self) -> Option<&Self::Parent> {
291        self.0
292            .try_borrow(self.1)
293            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
294            .__parent()
295    }
296    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
297        self.0
298            .try_borrow(self.1)
299            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
300            .__keyexpr()
301    }
302    unsafe fn __weight(&self) -> Option<&Weight> {
303        self.0
304            .try_borrow(self.1)
305            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
306            .__weight()
307    }
308
309    type Child = T::Child;
310    type Children = T::Children;
311
312    unsafe fn __children(&self) -> &Self::Children {
313        self.0
314            .try_borrow(self.1)
315            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
316            .__children()
317    }
318}
319
320impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
321    for (&Arc<TokenCell<T, Token>>, &mut Token)
322{
323}
324impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
325    for (&Arc<TokenCell<T, Token>>, &mut Token)
326{
327    type Parent = T::Parent;
328    unsafe fn __parent(&self) -> Option<&Self::Parent> {
329        self.0
330            .try_borrow(self.1)
331            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
332            .__parent()
333    }
334    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
335        self.0
336            .try_borrow(self.1)
337            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
338            .__keyexpr()
339    }
340    unsafe fn __weight(&self) -> Option<&Weight> {
341        self.0
342            .try_borrow(self.1)
343            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
344            .__weight()
345    }
346
347    type Child = T::Child;
348    type Children = T::Children;
349
350    unsafe fn __children(&self) -> &Self::Children {
351        self.0
352            .try_borrow(self.1)
353            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
354            .__children()
355    }
356}
357
358impl<T: IKeyExprTreeNodeMut<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNodeMut<Weight>
359    for (&Arc<TokenCell<T, Token>>, &mut Token)
360{
361    fn parent_mut(&mut self) -> Option<&mut Self::Parent> {
362        self.0
363            .try_borrow_mut(self.1)
364            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
365            .parent_mut()
366    }
367    fn weight_mut(&mut self) -> Option<&mut Weight> {
368        self.0
369            .try_borrow_mut(self.1)
370            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
371            .weight_mut()
372    }
373    fn take_weight(&mut self) -> Option<Weight> {
374        self.0
375            .try_borrow_mut(self.1)
376            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
377            .take_weight()
378    }
379    fn insert_weight(&mut self, weight: Weight) -> Option<Weight> {
380        self.0
381            .try_borrow_mut(self.1)
382            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
383            .insert_weight(weight)
384    }
385
386    fn children_mut(&mut self) -> &mut Self::Children {
387        self.0
388            .try_borrow_mut(self.1)
389            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
390            .children_mut()
391    }
392}
393
394use crate::keyexpr_tree::arc_tree::Tokenized;
395impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
396    for Tokenized<&TokenCell<T, Token>, &Token>
397{
398}
399impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
400    for Tokenized<&TokenCell<T, Token>, &Token>
401{
402    type Parent = T::Parent;
403    unsafe fn __parent(&self) -> Option<&Self::Parent> {
404        self.0
405            .try_borrow(self.1)
406            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
407            .__parent()
408    }
409    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
410        self.0
411            .try_borrow(self.1)
412            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
413            .__keyexpr()
414    }
415    unsafe fn __weight(&self) -> Option<&Weight> {
416        self.0
417            .try_borrow(self.1)
418            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
419            .__weight()
420    }
421
422    type Child = T::Child;
423    type Children = T::Children;
424
425    unsafe fn __children(&self) -> &Self::Children {
426        self.0
427            .try_borrow(self.1)
428            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
429            .__children()
430    }
431}
432
433impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
434    for Tokenized<&TokenCell<T, Token>, &mut Token>
435{
436}
437impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
438    for Tokenized<&TokenCell<T, Token>, &mut Token>
439{
440    type Parent = T::Parent;
441    unsafe fn __parent(&self) -> Option<&Self::Parent> {
442        self.0
443            .try_borrow(self.1)
444            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
445            .__parent()
446    }
447    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
448        self.0
449            .try_borrow(self.1)
450            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
451            .__keyexpr()
452    }
453    unsafe fn __weight(&self) -> Option<&Weight> {
454        self.0
455            .try_borrow(self.1)
456            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
457            .__weight()
458    }
459
460    type Child = T::Child;
461    type Children = T::Children;
462
463    unsafe fn __children(&self) -> &Self::Children {
464        self.0
465            .try_borrow(self.1)
466            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
467            .__children()
468    }
469}
470
471impl<T: IKeyExprTreeNodeMut<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNodeMut<Weight>
472    for Tokenized<&TokenCell<T, Token>, &mut Token>
473{
474    fn parent_mut(&mut self) -> Option<&mut Self::Parent> {
475        self.0
476            .try_borrow_mut(self.1)
477            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
478            .parent_mut()
479    }
480    fn weight_mut(&mut self) -> Option<&mut Weight> {
481        self.0
482            .try_borrow_mut(self.1)
483            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
484            .weight_mut()
485    }
486    fn take_weight(&mut self) -> Option<Weight> {
487        self.0
488            .try_borrow_mut(self.1)
489            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
490            .take_weight()
491    }
492    fn insert_weight(&mut self, weight: Weight) -> Option<Weight> {
493        self.0
494            .try_borrow_mut(self.1)
495            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
496            .insert_weight(weight)
497    }
498
499    fn children_mut(&mut self) -> &mut Self::Children {
500        self.0
501            .try_borrow_mut(self.1)
502            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
503            .children_mut()
504    }
505}
506
507impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
508    for Tokenized<&Arc<TokenCell<T, Token>>, &Token>
509{
510}
511impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
512    for Tokenized<&Arc<TokenCell<T, Token>>, &Token>
513{
514    type Parent = T::Parent;
515    unsafe fn __parent(&self) -> Option<&Self::Parent> {
516        self.0
517            .try_borrow(self.1)
518            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
519            .__parent()
520    }
521    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
522        self.0
523            .try_borrow(self.1)
524            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
525            .__keyexpr()
526    }
527    unsafe fn __weight(&self) -> Option<&Weight> {
528        self.0
529            .try_borrow(self.1)
530            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
531            .__weight()
532    }
533
534    type Child = T::Child;
535    type Children = T::Children;
536
537    unsafe fn __children(&self) -> &Self::Children {
538        self.0
539            .try_borrow(self.1)
540            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
541            .__children()
542    }
543}
544
545impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNode<Weight>
546    for Tokenized<&Arc<TokenCell<T, Token>>, &mut Token>
547{
548}
549impl<T: IKeyExprTreeNode<Weight>, Weight, Token: TokenTrait> UIKeyExprTreeNode<Weight>
550    for Tokenized<&Arc<TokenCell<T, Token>>, &mut Token>
551{
552    type Parent = T::Parent;
553    unsafe fn __parent(&self) -> Option<&Self::Parent> {
554        self.0
555            .try_borrow(self.1)
556            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
557            .__parent()
558    }
559    unsafe fn __keyexpr(&self) -> OwnedKeyExpr {
560        self.0
561            .try_borrow(self.1)
562            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
563            .__keyexpr()
564    }
565    unsafe fn __weight(&self) -> Option<&Weight> {
566        self.0
567            .try_borrow(self.1)
568            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
569            .__weight()
570    }
571
572    type Child = T::Child;
573    type Children = T::Children;
574
575    unsafe fn __children(&self) -> &Self::Children {
576        self.0
577            .try_borrow(self.1)
578            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
579            .__children()
580    }
581}
582
583impl<T: IKeyExprTreeNodeMut<Weight>, Weight, Token: TokenTrait> IKeyExprTreeNodeMut<Weight>
584    for Tokenized<&Arc<TokenCell<T, Token>>, &mut Token>
585{
586    fn parent_mut(&mut self) -> Option<&mut Self::Parent> {
587        self.0
588            .try_borrow_mut(self.1)
589            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
590            .parent_mut()
591    }
592    fn weight_mut(&mut self) -> Option<&mut Weight> {
593        self.0
594            .try_borrow_mut(self.1)
595            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
596            .weight_mut()
597    }
598    fn take_weight(&mut self) -> Option<Weight> {
599        self.0
600            .try_borrow_mut(self.1)
601            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
602            .take_weight()
603    }
604    fn insert_weight(&mut self, weight: Weight) -> Option<Weight> {
605        self.0
606            .try_borrow_mut(self.1)
607            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
608            .insert_weight(weight)
609    }
610
611    fn children_mut(&mut self) -> &mut Self::Children {
612        self.0
613            .try_borrow_mut(self.1)
614            .unwrap_or_else(|_| panic!("Used wrong token to access TokenCell"))
615            .children_mut()
616    }
617}