1- use super :: tree:: { AccessRelatedness , LocationTree , Node } ;
1+ use std:: marker:: PhantomData ;
2+
3+ use super :: tree:: { AccessRelatedness , Node } ;
24use super :: unimap:: { UniIndex , UniValMap } ;
35
46/// Data given to the transition function
5- pub struct NodeAppArgs < ' visit > {
7+ pub struct NodeAppArgs < ' visit , T > {
68 /// The index of the current node.
79 pub idx : UniIndex ,
810 /// Relative position of the access.
911 pub rel_pos : AccessRelatedness ,
1012 /// The node map of this tree.
1113 pub nodes : & ' visit mut UniValMap < Node > ,
12- /// The permissions map of this tree .
13- pub loc : & ' visit mut LocationTree ,
14+ /// Additional data we want to be able to modify in f_propagate and read in f_continue .
15+ pub data : & ' visit mut T ,
1416}
1517/// Internal contents of `Tree` with the minimum of mutable access for
1618/// For soundness do not modify the children or parent indexes of nodes
1719/// during traversal.
18- pub struct TreeVisitor < ' tree > {
20+ pub struct TreeVisitor < ' tree , T > {
1921 pub nodes : & ' tree mut UniValMap < Node > ,
20- pub loc : & ' tree mut LocationTree ,
22+ pub data : & ' tree mut T ,
2123}
2224
2325/// Whether to continue exploring the children recursively or not.
@@ -41,7 +43,7 @@ enum RecursionState {
4143/// Stack of nodes left to explore in a tree traversal.
4244/// See the docs of `traverse_this_parents_children_other` for details on the
4345/// traversal order.
44- struct TreeVisitorStack < NodeContinue , NodeApp > {
46+ struct TreeVisitorStack < NodeContinue , NodeApp , T > {
4547 /// Function describing whether to continue at a tag.
4648 /// This is only invoked for foreign accesses.
4749 f_continue : NodeContinue ,
@@ -56,36 +58,37 @@ struct TreeVisitorStack<NodeContinue, NodeApp> {
5658 /// This is just an artifact of how you hand-roll recursion,
5759 /// it does not have a deeper meaning otherwise.
5860 stack : Vec < ( UniIndex , AccessRelatedness , RecursionState ) > ,
61+ phantom : PhantomData < T > ,
5962}
6063
61- impl < NodeContinue , NodeApp , Err > TreeVisitorStack < NodeContinue , NodeApp >
64+ impl < NodeContinue , NodeApp , T , Err > TreeVisitorStack < NodeContinue , NodeApp , T >
6265where
63- NodeContinue : Fn ( & NodeAppArgs < ' _ > ) -> ContinueTraversal ,
64- NodeApp : FnMut ( NodeAppArgs < ' _ > ) -> Result < ( ) , Err > ,
66+ NodeContinue : Fn ( & NodeAppArgs < ' _ , T > ) -> ContinueTraversal ,
67+ NodeApp : FnMut ( NodeAppArgs < ' _ , T > ) -> Result < ( ) , Err > ,
6568{
6669 fn should_continue_at (
6770 & self ,
68- this : & mut TreeVisitor < ' _ > ,
71+ this : & mut TreeVisitor < ' _ , T > ,
6972 idx : UniIndex ,
7073 rel_pos : AccessRelatedness ,
7174 ) -> ContinueTraversal {
72- let args = NodeAppArgs { idx, rel_pos, nodes : this. nodes , loc : this. loc } ;
75+ let args = NodeAppArgs { idx, rel_pos, nodes : this. nodes , data : this. data } ;
7376 ( self . f_continue ) ( & args)
7477 }
7578
7679 fn propagate_at (
7780 & mut self ,
78- this : & mut TreeVisitor < ' _ > ,
81+ this : & mut TreeVisitor < ' _ , T > ,
7982 idx : UniIndex ,
8083 rel_pos : AccessRelatedness ,
8184 ) -> Result < ( ) , Err > {
82- ( self . f_propagate ) ( NodeAppArgs { idx, rel_pos, nodes : this. nodes , loc : this. loc } )
85+ ( self . f_propagate ) ( NodeAppArgs { idx, rel_pos, nodes : this. nodes , data : this. data } )
8386 }
8487
8588 /// Returns the root of this tree.
8689 fn go_upwards_from_accessed (
8790 & mut self ,
88- this : & mut TreeVisitor < ' _ > ,
91+ this : & mut TreeVisitor < ' _ , T > ,
8992 accessed_node : UniIndex ,
9093 visit_children : ChildrenVisitMode ,
9194 ) -> Result < UniIndex , Err > {
@@ -136,7 +139,7 @@ where
136139 Ok ( last_node)
137140 }
138141
139- fn finish_foreign_accesses ( & mut self , this : & mut TreeVisitor < ' _ > ) -> Result < ( ) , Err > {
142+ fn finish_foreign_accesses ( & mut self , this : & mut TreeVisitor < ' _ , T > ) -> Result < ( ) , Err > {
140143 while let Some ( ( idx, rel_pos, step) ) = self . stack . last_mut ( ) {
141144 let idx = * idx;
142145 let rel_pos = * rel_pos;
@@ -173,11 +176,11 @@ where
173176 }
174177
175178 fn new ( f_continue : NodeContinue , f_propagate : NodeApp ) -> Self {
176- Self { f_continue, f_propagate, stack : Vec :: new ( ) }
179+ Self { f_continue, f_propagate, stack : Vec :: new ( ) , phantom : PhantomData }
177180 }
178181}
179182
180- impl < ' tree > TreeVisitor < ' tree > {
183+ impl < ' tree , T > TreeVisitor < ' tree , T > {
181184 /// Applies `f_propagate` to every vertex of the tree in a piecewise bottom-up way: First, visit
182185 /// all ancestors of `start_idx` (starting with `start_idx` itself), then children of `start_idx`, then the rest,
183186 /// going bottom-up in each of these two "pieces" / sections.
@@ -219,8 +222,8 @@ impl<'tree> TreeVisitor<'tree> {
219222 pub fn traverse_this_parents_children_other < Err > (
220223 mut self ,
221224 start_idx : UniIndex ,
222- f_continue : impl Fn ( & NodeAppArgs < ' _ > ) -> ContinueTraversal ,
223- f_propagate : impl FnMut ( NodeAppArgs < ' _ > ) -> Result < ( ) , Err > ,
225+ f_continue : impl Fn ( & NodeAppArgs < ' _ , T > ) -> ContinueTraversal ,
226+ f_propagate : impl FnMut ( NodeAppArgs < ' _ , T > ) -> Result < ( ) , Err > ,
224227 ) -> Result < UniIndex , Err > {
225228 let mut stack = TreeVisitorStack :: new ( f_continue, f_propagate) ;
226229 // Visits the accessed node itself, and all its parents, i.e. all nodes
@@ -245,8 +248,8 @@ impl<'tree> TreeVisitor<'tree> {
245248 pub fn traverse_nonchildren < Err > (
246249 mut self ,
247250 start_idx : UniIndex ,
248- f_continue : impl Fn ( & NodeAppArgs < ' _ > ) -> ContinueTraversal ,
249- f_propagate : impl FnMut ( NodeAppArgs < ' _ > ) -> Result < ( ) , Err > ,
251+ f_continue : impl Fn ( & NodeAppArgs < ' _ , T > ) -> ContinueTraversal ,
252+ f_propagate : impl FnMut ( NodeAppArgs < ' _ , T > ) -> Result < ( ) , Err > ,
250253 ) -> Result < UniIndex , Err > {
251254 let mut stack = TreeVisitorStack :: new ( f_continue, f_propagate) ;
252255 // Visits the accessed node itself, and all its parents, i.e. all nodes
@@ -271,8 +274,8 @@ impl<'tree> TreeVisitor<'tree> {
271274 pub fn traverse_children_this < Err > (
272275 mut self ,
273276 start_idx : UniIndex ,
274- f_continue : impl Fn ( & NodeAppArgs < ' _ > ) -> ContinueTraversal ,
275- f_propagate : impl FnMut ( NodeAppArgs < ' _ > ) -> Result < ( ) , Err > ,
277+ f_continue : impl Fn ( & NodeAppArgs < ' _ , T > ) -> ContinueTraversal ,
278+ f_propagate : impl FnMut ( NodeAppArgs < ' _ , T > ) -> Result < ( ) , Err > ,
276279 ) -> Result < ( ) , Err > {
277280 let mut stack = TreeVisitorStack :: new ( f_continue, f_propagate) ;
278281
0 commit comments