Skip to main content

dynamic_graph/
lib.rs

1#![allow(unused_unsafe)]
2
3pub mod graph_ptr;
4pub use crate::graph_ptr::*;
5
6mod graph_raw;
7use crate::graph_raw::*;
8
9pub mod edge;
10pub use crate::edge::*;
11
12pub mod nodes;
13pub use crate::nodes::*;
14
15use core::hash::{Hash, Hasher};
16use core::mem::transmute;
17use core::ops::{Index, IndexMut, Deref, DerefMut};
18use core::ptr::NonNull;
19
20pub struct GenericGraph<Root, NodeType>
21where Root : RootCollection<'static, NodeType>,
22      NodeType : GraphNode,
23{
24    internal : GraphRaw<NodeType>,
25    root : Root
26}
27
28pub trait GraphImpl {
29    /// Traverses the graph and drops any inaccessible node. Disregards any heuristic designed to improve
30    /// cleanup performance.
31    fn cleanup_precise(&mut self);
32    /// Traverses the graph and drops inaccessible nodes. This method will miss some of the leaked items which
33    /// might result in spikes in memory usage. !! Currently, none of the possible heuristics are implemented.
34    fn cleanup(&mut self) {
35        self.cleanup_precise();
36    }
37}
38
39impl <Root, NodeType> Default for GenericGraph<Root, NodeType>
40where Root : RootCollection<'static, NodeType>,
41      NodeType : GraphNode
42{
43    fn default() -> Self
44    {
45        GenericGraph::new()
46    }
47}
48
49impl <Root, NodeType> GenericGraph<Root, NodeType>
50where Root : RootCollection<'static, NodeType>,
51      NodeType : GraphNode
52{
53    pub fn new() -> Self
54    {
55        GenericGraph { root : Root::default(), internal : GraphRaw::new() }
56    }
57}
58
59impl <Root, NodeType> GenericGraph<Root, NodeType>
60where Root : RootCollection<'static, NodeType>,
61      NodeType : GraphNode
62{
63    /// Creates an AnchorMut from a generativity brand using selected cleanup strategy.
64    /// Prefer `anchor_mut!` macro in application code.
65    /// # Safety
66    /// Caller must use a unique `guard` from generativity::Guard.
67    pub unsafe fn anchor_mut<'id>(&mut self, guard : Id<'id>, strategy : CleanupStrategy)
68                                  -> AnchorMut<'_, 'id, GenericGraph<Root, NodeType>>
69    {
70        AnchorMut { parent : self, _guard : guard, strategy }
71    }
72
73    /// Creates an Anchor from a generativity brand.
74    /// Prefer `anchor!` macro in application code.
75    /// # Safety
76    /// Caller must use a unique `guard` from generativity::Guard.
77    pub unsafe fn anchor<'id>(&self, guard : Id<'id>) -> Anchor<'_, 'id, GenericGraph<Root, NodeType>>
78    {
79        Anchor { parent : self, _guard : guard }
80    }
81}
82
83pub type VecGraph<T> = GenericGraph<RootVec<'static, T>, T>;
84pub type NamedGraph<T> = GenericGraph<RootNamedSet<'static, T>, T>;
85pub type OptionGraph<T> = GenericGraph<RootOption<'static, T>, T>;
86
87/// A strategy AnchorMut employs to perform cleanup after drop.
88pub enum CleanupStrategy {
89    /// AnchorMut never cleans up.
90    Never,
91    /// AnchorMut always performs cleanup when dropped
92    Always,
93}
94
95pub struct AnchorMut<'this, 'id, T : 'this>
96where T : GraphImpl
97{
98    parent: &'this mut T,
99    strategy : CleanupStrategy,
100    _guard : Id<'id>,
101}
102
103pub struct Anchor<'this, 'id, T : 'this>
104where T : GraphImpl
105{
106    parent: &'this T,
107    _guard : Id<'id>,
108}
109
110impl <Root, NodeType> GraphImpl
111for GenericGraph<Root, NodeType>
112where Root : RootCollection<'static, NodeType>,
113      NodeType : GraphNode
114{
115    fn cleanup_precise(&mut self) {
116        self.internal.cleanup_precise(&self.root);
117    }
118}
119
120impl <'this, 'id, T : 'this> Drop for AnchorMut<'this, 'id, T>
121where T : GraphImpl
122{
123    fn drop(&mut self) {
124        match &self.strategy {
125            CleanupStrategy::Always => self.parent.cleanup(),
126            _ => ()
127        }
128    }
129}
130
131macro_rules! impl_anchor_index {
132    ($NodeType:ident) => {
133        impl <'this, 'id, N : 'this, E : 'this, Root : 'this>
134        Index<GraphPtr<'id, $NodeType<N, E>>>
135        for Anchor<'this, 'id, GenericGraph<Root, $NodeType<N, E>>>
136        where Root : RootCollection<'static, $NodeType<N, E>>
137        {
138            type Output = node_views::$NodeType<'id, N, E>;
139            fn index(&self, dst : GraphPtr<'id, $NodeType<N, E>>) -> &Self::Output
140            {
141                self.internal().get_view(dst)
142            }
143        }
144
145        impl <'this, 'id, N : 'this, E : 'this, Root : 'this>
146        Anchor<'this, 'id, GenericGraph<Root, $NodeType<N, E>>>
147        where Root : RootCollection<'static, $NodeType<N, E>>
148        {
149            /// Returns an iterator over edges attached to `src` node.
150            pub fn edges(&self, src : GraphPtr<'id, $NodeType<N, E>>) ->
151                impl Iterator<Item = GraphItem<Edge<&'_ N, &'_ E>, GraphPtr<'id, $NodeType<N, E>>>>
152            {
153                self.internal().iter(src)
154            }
155        }
156    }
157}
158
159
160impl_anchor_index!{NamedNode}
161impl_anchor_index!{OptionNode}
162impl_anchor_index!{VecNode}
163
164impl <'this, 'id, K : 'this, N : 'this, E : 'this, Root : 'this>
165Index<GraphPtr<'id, TreeNode<K, N, E>>>
166for Anchor<'this, 'id, GenericGraph<Root, TreeNode<K, N, E>>>
167where Root : RootCollection<'static, TreeNode<K, N, E>>, K : Ord
168{
169    type Output = node_views::TreeNode<'id, K, N, E>;
170    fn index(&self, dst : GraphPtr<'id, TreeNode<K, N, E>>) -> &Self::Output
171    {
172        self.internal().get_view(dst)
173    }
174}
175
176impl <'this, 'id, K : 'this, N : 'this, E : 'this, Root : 'this>
177Anchor<'this, 'id, GenericGraph<Root, TreeNode<K, N, E>>>
178where Root : RootCollection<'static, TreeNode<K, N, E>>, K : Ord
179{
180    /// Returns an iterator over edges attached to `src` node.
181    pub fn edges(&self, src : GraphPtr<'id, TreeNode<K, N, E>>) ->
182        impl Iterator<Item = GraphItem<Edge<&'_ N, &'_ E>, GraphPtr<'id, TreeNode<K, N, E>>>>
183    {
184        self.internal().iter(src)
185    }
186}
187
188
189macro_rules! impl_anchor_mut_index {
190    ($NodeType:ident) => {
191        impl <'this, 'id, N : 'this, E : 'this, Root : 'this>
192        Index<GraphPtr<'id, $NodeType<N, E>>>
193        for AnchorMut<'this, 'id, GenericGraph<Root, $NodeType<N, E>>>
194        where Root : RootCollection<'static, $NodeType<N, E>>
195        {
196            type Output = node_views::$NodeType<'id, N, E>;
197            fn index(&self, dst : GraphPtr<'id, $NodeType<N, E>>) -> &Self::Output
198            {
199                self.internal().get_view(dst)
200            }
201        }
202
203        impl <'this, 'id, N : 'this, E : 'this, Root : 'this>
204        AnchorMut<'this, 'id, GenericGraph<Root, $NodeType<N, E>>>
205        where Root : RootCollection<'static, $NodeType<N, E>>
206        {
207            /// Returns an iterator over edges attached to `src` node.
208            pub fn edges(&self, src : GraphPtr<'id, $NodeType<N, E>>) ->
209                impl Iterator<Item = GraphItem<Edge<&'_ N, &'_ E>, GraphPtr<'id, $NodeType<N, E>>>>
210            {
211                self.internal().iter(src)
212            }
213        }
214
215        impl <'this, 'id, N : 'this, E : 'this, Root : 'this>
216        IndexMut<GraphPtr<'id, $NodeType<N, E>>>
217        for AnchorMut<'this, 'id, GenericGraph<Root, $NodeType<N, E>>>
218        where Root : RootCollection<'static, $NodeType<N, E>>
219        {
220            fn index_mut(&mut self, dst : GraphPtr<'id, $NodeType<N, E>>) -> &mut Self::Output {
221                self.internal_mut().get_view_mut(dst)
222            }
223        }
224        
225        impl <'this, 'id, N : 'this, E : 'this, Root : 'this>
226        AnchorMut<'this, 'id, GenericGraph<Root, $NodeType<N, E>>>
227        where Root : RootCollection<'static, $NodeType<N,E>>
228        {
229            /// Returns a mutable iterator over edges attached to `src` node.
230            pub fn edges_mut(&mut self, src : GraphPtr<'id, $NodeType<N, E>>) ->
231                impl Iterator<Item = GraphItem<Edge<&'_ mut N, &'_ mut E>, GraphPtr<'id, $NodeType<N, E>>>>
232            {
233                self.internal_mut().iter_mut(src)
234            }
235        
236            /// Provides direct mutable direct access to two different nodes `src` and `dst`. Returns or None if `src` is the same as `dst`.
237            pub fn bridge(&mut self, src : GraphPtr<'id, $NodeType<N, E>>,
238                                     dst : GraphPtr<'id, $NodeType<N, E>>) ->
239                Option<(&'_ mut node_views::$NodeType<'id, N, E>, &'_ mut node_views::$NodeType<'id, N, E>)>
240            {
241                self.internal_mut().bridge(src, dst)
242            }
243        }
244    }
245}
246
247impl_anchor_mut_index!{NamedNode}
248impl_anchor_mut_index!{OptionNode}
249impl_anchor_mut_index!{VecNode}
250
251impl <'this, 'id, K : 'this, N : 'this, E : 'this, Root : 'this>
252Index<GraphPtr<'id, TreeNode<K, N, E>>>
253for AnchorMut<'this, 'id, GenericGraph<Root, TreeNode<K, N, E>>>
254where Root : RootCollection<'static, TreeNode<K, N, E>>, K : Ord
255{
256    type Output = node_views::TreeNode<'id, K, N, E>;
257    fn index(&self, dst : GraphPtr<'id, TreeNode<K, N, E>>) -> &Self::Output
258    {
259        self.internal().get_view(dst)
260    }
261}
262
263impl <'this, 'id, K : 'this, N : 'this, E : 'this, Root : 'this>
264AnchorMut<'this, 'id, GenericGraph<Root, TreeNode<K, N, E>>>
265where Root : RootCollection<'static, TreeNode<K, N, E>>, K : Ord
266{
267    /// Returns an iterator over edges attached to `src` node.
268    pub fn edges(&self, src : GraphPtr<'id, TreeNode<K, N, E>>) ->
269        impl Iterator<Item = GraphItem<Edge<&'_ N, &'_ E>, GraphPtr<'id, TreeNode<K, N, E>>>>
270    {
271        self.internal().iter(src)
272    }
273}
274
275impl <'this, 'id, K : 'this, N : 'this, E : 'this, Root : 'this>
276IndexMut<GraphPtr<'id, TreeNode<K, N, E>>>
277for AnchorMut<'this, 'id, GenericGraph<Root, TreeNode<K, N, E>>>
278where Root : RootCollection<'static, TreeNode<K, N, E>>, K : Ord
279{
280    fn index_mut(&mut self, dst : GraphPtr<'id,  TreeNode<K, N, E>>) -> &mut Self::Output {
281        self.internal_mut().get_view_mut(dst)
282    }
283}
284
285impl <'this, 'id, K : 'this, N : 'this, E : 'this, Root : 'this>
286AnchorMut<'this, 'id, GenericGraph<Root, TreeNode<K, N, E>>>
287where Root : RootCollection<'static, TreeNode<K, N, E>>, K : Ord
288{
289    /// Returns a mutable iterator over edges attached to `src` node.
290    pub fn edges_mut(&mut self, src : GraphPtr<'id, TreeNode<K, N, E>>) ->
291        impl Iterator<Item = GraphItem<Edge<&'_ mut N, &'_ mut E>, GraphPtr<'id, TreeNode<K, N, E>>>>
292    {
293        self.internal_mut().iter_mut(src)
294    }
295
296    /// Provides direct mutable direct access to two different nodes `src` and `dst`. Returns or None if `src` is the same as `dst`.
297    pub fn bridge(&mut self, src : GraphPtr<'id, TreeNode<K, N, E>>,
298                             dst : GraphPtr<'id, TreeNode<K, N, E>>) ->
299        Option<(&'_ mut node_views::TreeNode<'id, K, N, E>, &'_ mut node_views::TreeNode<'id, K, N, E>)>
300    {
301        self.internal_mut().bridge(src, dst)
302    }
303}
304
305impl <'this, 'id, N : 'this, NodeType : 'this, Root : 'this>
306AnchorMut<'this, 'id, GenericGraph<Root, NodeType>>
307where NodeType : GraphNode<Node = N>,
308      Root : RootCollection<'static, NodeType>
309{
310    fn internal(&self) -> &GraphRaw<NodeType> {
311        &self.parent.internal
312    }
313
314    /// Creates a checked pointer from a raw pointer.
315    /// # Safety
316    /// Caller must guarantee `raw` points to a node which was not cleaned up and belongs to the parent graph. 
317    pub unsafe fn from_raw(&self, raw : *const NodeType) -> GraphPtr<'id, NodeType>
318    {
319        GraphPtr::from_ptr(raw, self._guard)
320    }
321
322    /// Creates an immutable cursor pointing to `dst`
323    pub fn cursor(&self, dst : GraphPtr<'id, NodeType>) -> Cursor<'_, 'id, NodeType>
324    {
325        Cursor { parent : self.internal(), current : dst }
326    }
327}
328
329impl <'this, 'id, N : 'this, NodeType : 'this, Root : 'this>
330Anchor<'this, 'id, GenericGraph<Root, NodeType>>
331where NodeType : GraphNode<Node = N>,
332      Root : RootCollection<'static, NodeType>
333{
334    fn internal(&self) -> &GraphRaw<NodeType> {
335        &self.parent.internal
336    }
337
338    /// Creates a checked pointer from a raw pointer.
339    /// # Safety
340    /// Caller must guarantee `raw` points to a node which was not cleaned up and belongs to the parent graph. 
341    pub unsafe fn from_raw(&self, raw : *const NodeType) -> GraphPtr<'id, NodeType>
342    {
343        GraphPtr::from_ptr(raw, self._guard)
344    }
345
346    /// Creates an immutable cursor pointing to `dst`
347    pub fn cursor(&self, dst : GraphPtr<'id, NodeType>) -> Cursor<'_, 'id, NodeType>
348    {
349        Cursor { parent : self.internal(), current : dst }
350    }
351}
352
353impl <'this, 'id, N : 'this, NodeType : 'this, Root : 'this>
354AnchorMut<'this, 'id, GenericGraph<Root, NodeType>>
355where NodeType : GraphNode<Node = N>,
356      Root : RootCollection<'static, NodeType>
357{
358    fn internal_mut(&mut self) -> &mut GraphRaw<NodeType>
359    {
360        &mut self.parent.internal
361    }
362
363    /// Allocates a new node and returns the pointer. This node will become inaccessible when parent anchor
364    /// is dropped and will be disposed of upon next cleanup unless you attach it to the root or another node accessible
365    /// from the root.
366    pub fn spawn(&mut self, data : N) -> GraphPtr<'id, NodeType>
367    {
368        let ptr = self.internal_mut().spawn_detached(data);
369        unsafe {
370            //allocation never fails
371            GraphPtr::from_ptr(ptr, self._guard )
372        }
373    }
374
375    /// Immediately drops `dst` node and frees allocated memory.
376    /// # Safety
377    /// Caller must ensure killed node will never be accessed. `dst` must become inaccesible from root before
378    /// anchor is dropped. Any copies of `dst` in external collections should be disposed of as well.
379    pub unsafe fn kill(&mut self, dst : GraphPtr<'id, NodeType>) {
380        self.internal_mut().kill(dst.as_mut());
381    }
382
383    /// Creates a mutable cursor pointing to `dst`.
384    pub fn cursor_mut(&mut self, dst : GraphPtr<'id, NodeType>)
385           -> CursorMut<'_, 'id, NodeType>
386    {
387        CursorMut { parent : self.internal_mut(), current : dst }
388    }
389}
390
391macro_rules! impl_root_mut_iter {
392    ($root_type:ident) => {
393        impl <'this, 'id, N : 'this, NodeType : 'this>
394        AnchorMut<'this, 'id, $root_type<NodeType>>
395        where NodeType : GraphNode<Node = N>
396        {
397            /// Returns an iterator over data and pointers to nodes attached to the root.
398            pub fn iter(&self) -> impl Iterator<Item = GraphItem<&'_ N, GraphPtr<'id, NodeType>>>
399            {
400                self.root().iter().map(move |x| {
401                    let p = x.as_ptr();
402                    let values = unsafe { (*p).get() };
403                    GraphItem { values, ptr : *x }
404                })
405            }
406
407            /// Returns a mutable iterator over data and pointers to nodes attached to the root.
408            pub fn iter_mut(&mut self) -> impl Iterator<Item = GraphItem<&'_ mut N, GraphPtr<'id, NodeType>>>
409            {
410                self.root_mut().iter().map(move |x| {
411                    let p = x.as_mut();
412                    let values = unsafe { (*p).get_mut() };
413                    GraphItem { values, ptr : *x }
414                })
415            }
416        }
417    }
418}
419
420impl_root_mut_iter!{VecGraph}
421impl_root_mut_iter!{NamedGraph}
422impl_root_mut_iter!{OptionGraph}
423
424/// A wrapper over a GraphPtr which provides simplified access to AnchorMut API.
425pub struct CursorMut<'this, 'id, T : 'this> {
426    parent : &'this mut GraphRaw<T>,
427    current : GraphPtr<'id, T>
428}
429
430/// A wrapper over a GraphPtr which provides simplified access to Anchor API.
431pub struct Cursor<'this, 'id, T : 'this> {
432    parent : &'this GraphRaw<T>,
433    current : GraphPtr<'id, T>
434}
435
436macro_rules! impl_cursor_immutable {
437    ($cursor_type:ident) => {
438        impl <'this, 'id, N : 'this, NodeType : 'this>
439        $cursor_type<'this, 'id, NodeType>
440        where NodeType : GraphNode<Node = N>
441        {
442            /// Returns a pointer to the current node the cursor points to.
443            pub fn at(&self) -> GraphPtr<'id, NodeType>
444            {
445                self.current
446            }
447        
448            /// Returns true if the cursor points to `dst`.
449            pub fn is_at(&self, dst : GraphPtr<'id, NodeType>) -> bool
450            {
451                dst == self.at()
452            }
453        
454            /// Moves the cursor to `dst`.
455            pub fn jump(&mut self, dst : GraphPtr<'id, NodeType>)
456            {
457                self.current = dst;
458            }
459        }
460        
461        impl <'this, 'id, N : 'this, E : 'this>
462        $cursor_type<'this, 'id, NamedNode<N, E>>
463        {    
464            /// Returns Some if `dst` is attached to the current node and None otherwise.
465            pub fn get_edge(&self, dst : GraphPtr<'id, NamedNode<N, E>>) -> Option<Edge<&'_ N, &'_ E>>
466            {
467                self.parent.get_edge(self.at(), dst)
468            }
469        }
470
471        impl <'this, 'id, N : 'this, E : 'this>
472        $cursor_type<'this, 'id, VecNode<N, E>>
473        {    
474            /// Returns Some if `dst` is attached to the current node and None otherwise.
475            pub fn get_edge(&self, dst : usize) -> Option<Edge<&'_ N, &'_ E>>
476            {
477                self.parent.get_edge(self.at(), dst)
478            }
479        }
480
481        impl <'this, 'id, N : 'this, E : 'this>
482        $cursor_type<'this, 'id, OptionNode<N, E>>
483        {    
484            /// Returns Some if a node is attached to the current node and None otherwise.
485            pub fn get_edge(&self, _dst : ()) -> Option<Edge<&'_ N, &'_ E>>
486            {
487                self.parent.get_edge(self.at())
488            }
489        }
490
491        impl <'this, 'id, K : 'this, N : 'this, E : 'this>
492        $cursor_type<'this, 'id, TreeNode<K, N, E>> where K : Ord
493        {    
494            /// Returns Some if a node is attached to the current node and None otherwise.
495            pub fn get_edge(&self, dst : &K) -> Option<Edge<&'_ N, &'_ E>>
496            {
497                self.parent.get_edge(self.at(), dst)
498            }
499        }
500
501
502        impl <'this, 'id, K : 'this, N : 'this, E : 'this>
503        $cursor_type<'this, 'id, TreeNode<K, N, E>> where K : Ord
504        {
505            /// Returns an iterator over edges and node pointers attached to the current node.
506            pub fn edges(&self) ->
507                impl Iterator<Item = GraphItem<Edge<&'_ N, &'_ E>, GraphPtr<'id, TreeNode<K, N, E>>>>
508            {
509                self.parent.iter(self.at())
510            }
511        }
512        
513        impl <'this, 'id, K : 'this, N : 'this, E : 'this> Deref for $cursor_type<'this, 'id, TreeNode<K, N, E>> where K : Ord
514        {
515            type Target = node_views::TreeNode<'id, K, N, E>;
516            fn deref(&self) -> &Self::Target
517            {
518                self.parent.get_view(self.at())
519            }
520        }
521
522
523    };
524    ($cursor_type:ident, $node_type:ident) => {
525        impl <'this, 'id, N : 'this, E : 'this>
526        $cursor_type<'this, 'id, $node_type<N, E>>
527        {
528            /// Returns an iterator over edges and node pointers attached to the current node.
529            pub fn edges(&self) ->
530                impl Iterator<Item = GraphItem<Edge<&'_ N, &'_ E>, GraphPtr<'id, $node_type<N, E>>>>
531            {
532                self.parent.iter(self.at())
533            }
534        }
535        
536        impl <'this, 'id, N : 'this, E : 'this> Deref for $cursor_type<'this, 'id, $node_type<N, E>>
537        {
538            type Target = node_views::$node_type<'id, N, E>;
539            fn deref(&self) -> &Self::Target
540            {
541                self.parent.get_view(self.at())
542            }
543        }
544    };
545}
546
547impl_cursor_immutable!{CursorMut}
548impl_cursor_immutable!{Cursor}
549
550impl_cursor_immutable!{CursorMut, NamedNode}
551impl_cursor_immutable!{Cursor, NamedNode}
552impl_cursor_immutable!{CursorMut, VecNode}
553impl_cursor_immutable!{Cursor, VecNode}
554impl_cursor_immutable!{CursorMut, OptionNode}
555impl_cursor_immutable!{Cursor, OptionNode}
556
557impl <'this, 'id, N : 'this, E : 'this>
558CursorMut<'this, 'id, NamedNode<N, E>>
559{    
560    /// Returns Some if `dst` is attached to the current node and None otherwise.
561    pub fn get_edge_mut(&mut self, dst : GraphPtr<'id, NamedNode<N, E>>) -> Option<Edge<&'_ mut N, &'_ mut E>>
562    {
563        self.parent.get_edge_mut(self.at(), dst)
564    }
565}
566
567impl <'this, 'id, N : 'this, E : 'this>
568CursorMut<'this, 'id, VecNode<N, E>>
569{    
570    /// Returns Some if `dst` is attached to the current node and None otherwise.
571    pub fn get_edge_mut(&mut self, dst : usize) -> Option<Edge<&'_ mut N, &'_ mut E>>
572    {
573        self.parent.get_edge_mut(self.at(), dst)
574    }
575}
576
577impl <'this, 'id, N : 'this, E : 'this>
578CursorMut<'this, 'id, OptionNode<N, E>>
579{    
580    /// Returns Some if a node is attached to the current node and None otherwise.
581    pub fn get_edge_mut(&mut self, _key : ()) -> Option<Edge<&'_ mut N, &'_ mut E>>
582    {
583        self.parent.get_edge_mut(self.at())
584    }
585}
586
587macro_rules! impl_cursor_mut {
588    ($node_type:ident) => {
589        impl <'this, 'id, N : 'this, E : 'this>
590        CursorMut<'this, 'id, $node_type<N, E>>
591        {
592            /// Returns a mutable iterator over edges and node pointers attached to the current node.
593            pub fn edges_mut(&mut self) ->
594                impl Iterator<Item = GraphItem<Edge<&'_ mut  N, &'_ mut E>, GraphPtr<'id, $node_type<N, E>>>>
595            {
596                self.parent.iter_mut(self.at())
597            }
598
599            /// Provides direct mutable access to current and `dst` nodes or or None if current is the same as `dst`.
600            /// Returns mutable views into the current and `dst` nodes or None if current is the same as `dst`.
601            pub fn bridge(&mut self, dst : GraphPtr<'id, $node_type<N, E>>) ->
602                Option<(&'_ mut node_views::$node_type<'id, N, E>, &'_ mut node_views::$node_type<'id, N, E>)>
603            {
604                self.parent.bridge(self.at(), dst)
605            }
606        }
607
608        impl <'this, 'id, N : 'this, E : 'this> DerefMut for CursorMut<'this, 'id, $node_type<N, E>>
609        {
610            fn deref_mut(&mut self) -> &mut Self::Target {
611                self.parent.get_view_mut(self.at())
612            }
613        }
614    }
615}
616
617impl_cursor_mut!{NamedNode}
618impl_cursor_mut!{VecNode}
619impl_cursor_mut!{OptionNode}
620
621impl <'this, 'id, K : 'this, N : 'this, E : 'this>
622CursorMut<'this, 'id, TreeNode<K, N, E>> where K : Ord
623{
624    /// Returns a mutable iterator over edges and node pointers attached to the current node.
625    pub fn edges_mut(&mut self) ->
626        impl Iterator<Item = GraphItem<Edge<&'_ mut  N, &'_ mut E>, GraphPtr<'id, TreeNode<K, N, E>>>>
627    {
628        self.parent.iter_mut(self.at())
629    }
630
631    /// Provides direct mutable access to current and `dst` nodes or or None if current is the same as `dst`.
632    /// Returns mutable views into the current and `dst` nodes or None if current is the same as `dst`.
633    pub fn bridge(&mut self, dst : GraphPtr<'id, TreeNode<K, N, E>>) ->
634        Option<(&'_ mut node_views::TreeNode<'id, K, N, E>, &'_ mut node_views::TreeNode<'id, K, N, E>)>
635    {
636        self.parent.bridge(self.at(), dst)
637    }
638}
639
640impl <'this, 'id, K : 'this, N : 'this, E : 'this>
641DerefMut for CursorMut<'this, 'id, TreeNode<K, N, E>> where K : Ord
642{
643    fn deref_mut(&mut self) -> &mut Self::Target {
644        self.parent.get_view_mut(self.at())
645    }
646}
647
648macro_rules! impl_generic_graph_root {
649    ($collection:ident, $graph:ident) => {
650        impl <'this, 'id, N : 'this, NodeType : 'this>
651        AnchorMut<'this, 'id, $graph<NodeType>>
652        where NodeType : GraphNode<Node = N>
653        {
654            /// Provides direct access to the collection of the root.
655            pub fn root(&self) -> &$collection<'id, NodeType>
656            {
657                //this transmute only affects lifetime parameter
658                unsafe {
659                    transmute(&self.parent.root)
660                }
661            }
662
663            /// Provides direct mutable access to the collection of the root.
664            pub fn root_mut(&mut self) -> &mut $collection<'id, NodeType>
665            {
666                //this transmute only affects lifetime parameter
667                unsafe {
668                    transmute(&mut self.parent.root)
669                }
670            }
671        }
672
673        impl <'this, 'id, N : 'this, NodeType : 'this>
674        Anchor<'this, 'id, $graph<NodeType>>
675        where NodeType : GraphNode<Node = N>
676        {
677            /// Provides direct access to the collection of the root.
678            pub fn root(&self) -> &$collection<'id, NodeType>
679            {
680                //this transmute only affects lifetime parameter
681                unsafe {
682                    transmute(&self.parent.root)
683                }
684            }
685        }
686    }
687}
688
689impl_generic_graph_root!{RootVec, VecGraph}
690impl_generic_graph_root!{RootNamedSet, NamedGraph}
691impl_generic_graph_root!{RootOption, OptionGraph}
692
693#[macro_export]
694/// Creates an AnchorMut using selected cleanup strategy.
695macro_rules! anchor_mut
696{
697    ($name:ident, $strategy:tt) => {
698        make_guard!(g);
699        let mut $name = unsafe { $name.anchor_mut(Id::from(g), $strategy)   };
700    };
701    ($name:ident, $parent:tt, $strategy:tt) => {
702        make_guard!(g);
703        let mut $name = unsafe { $parent.anchor_mut(Id::from(g), $strategy) };
704    };
705}
706
707#[macro_export]
708/// Creates an Anchor.
709macro_rules! anchor
710{
711    ($name:ident) => {
712        make_guard!(g);
713        let mut $name = unsafe { $name.anchor(Id::from(g))   };
714    };
715    ($name:ident, $parent:tt) => {
716        make_guard!(g);
717        let mut $name = unsafe { $parent.anchor(Id::from(g)) };
718    };
719}