@@ -925,31 +925,9 @@ impl<T> TransformedResult<T> for Result<Transformed<T>> {
925925 }
926926}
927927
928- /// A node trait which makes all tree traversal iterative and not recursive.
929- pub trait RecursiveNode : Sized {
930- /// Read-only access to children
931- fn children ( & self ) -> Vec < & Self > ;
932-
933- /// Detaches children from the parent node (if possible).
934- /// Unlike [`ConcreteTreeNode`] it doesn't possible that the value will actually be removed from the parent
935- fn take_children ( self ) -> ( Self , Vec < Self > ) ;
936-
937- /// Replaces children with the given one
938- fn with_new_children ( self , children : Vec < Self > ) -> Result < Self > ;
939- }
940-
941- impl < T : RecursiveNode > Transformed < T > {
942- fn children ( mut self ) -> ( Self , Vec < T > ) {
943- let ( data, children) = self . data . take_children ( ) ;
944- self . data = data;
945-
946- ( self , children)
947- }
948- }
949-
950928/// Helper trait for implementing [`TreeNode`] that have children stored as
951929/// `Arc`s. If some trait object, such as `dyn T`, implements this trait,
952- /// its related `Arc<dyn T>` will automatically implement [`TreeNode`].
930+ /// its related `Arc<dyn T>` will automatically implement [`TreeNode`] and [`ConcreteTreeNode`] .
953931pub trait DynTreeNode {
954932 /// Returns all children of the specified `TreeNode`.
955933 fn arc_children ( & self ) -> Vec < & Arc < Self > > ;
@@ -961,6 +939,23 @@ pub trait DynTreeNode {
961939 ) -> Result < Arc < Self > > ;
962940}
963941
942+ /// Note that this implementation doesn't actually take children from DynTreeNode, since they are stored
943+ /// inside shared references, but instead produces new links to them
944+ impl < T : DynTreeNode + ?Sized > ConcreteTreeNode for Arc < T > {
945+ fn children ( & self ) -> Vec < & Self > {
946+ self . arc_children ( )
947+ }
948+
949+ fn take_children ( self ) -> ( Self , Vec < Self > ) {
950+ let children = self . children ( ) . into_iter ( ) . cloned ( ) . collect ( ) ;
951+ ( self , children)
952+ }
953+
954+ fn with_new_children ( self , children : Vec < Self > ) -> Result < Self > {
955+ self . with_new_arc_children ( children)
956+ }
957+ }
958+
964959/// Adapter from the old function-based rewriter to the new Transformer one
965960struct FuncRewriter <
966961 FD : FnMut ( Node ) -> Result < Transformed < Node > > ,
@@ -1003,23 +998,16 @@ impl<
1003998 }
1004999}
10051000
1006- /// Note that this implementation won't actually take children, but instead clone a reference
1007- impl < T : DynTreeNode + ?Sized > RecursiveNode for Arc < T > {
1008- fn children ( & self ) -> Vec < & Self > {
1009- self . arc_children ( )
1010- }
1001+ impl < T : ConcreteTreeNode > Transformed < T > {
1002+ fn children ( mut self ) -> ( Self , Vec < T > ) {
1003+ let ( data, children) = self . data . take_children ( ) ;
1004+ self . data = data;
10111005
1012- fn take_children ( self ) -> ( Self , Vec < Self > ) {
1013- let children = self . arc_children ( ) . into_iter ( ) . cloned ( ) . collect ( ) ;
10141006 ( self , children)
10151007 }
1016-
1017- fn with_new_children ( self , children : Vec < Self > ) -> Result < Self > {
1018- self . with_new_arc_children ( children)
1019- }
10201008}
10211009
1022- impl < T : RecursiveNode > TreeNode for T {
1010+ impl < T : ConcreteTreeNode > TreeNode for T {
10231011 fn apply_children < ' n , F : FnMut ( & ' n Self ) -> Result < TreeNodeRecursion > > (
10241012 & ' n self ,
10251013 f : F ,
@@ -1259,7 +1247,7 @@ enum VisitingState<'a, T> {
12591247/// involving payloads, by enforcing rules for detaching and reattaching child nodes.
12601248pub trait ConcreteTreeNode : Sized {
12611249 /// Provides read-only access to child nodes.
1262- fn children ( & self ) -> & [ Self ] ;
1250+ fn children ( & self ) -> Vec < & Self > ;
12631251
12641252 /// Detaches the node from its children, returning the node itself and its detached children.
12651253 fn take_children ( self ) -> ( Self , Vec < Self > ) ;
@@ -1268,30 +1256,15 @@ pub trait ConcreteTreeNode: Sized {
12681256 fn with_new_children ( self , children : Vec < Self > ) -> Result < Self > ;
12691257}
12701258
1271- impl < T : ConcreteTreeNode > RecursiveNode for T {
1272- fn children ( & self ) -> Vec < & Self > {
1273- self . children ( ) . iter ( ) . collect ( )
1274- }
1275-
1276- fn take_children ( self ) -> ( Self , Vec < Self > ) {
1277- self . take_children ( )
1278- }
1279-
1280- fn with_new_children ( self , children : Vec < Self > ) -> Result < Self > {
1281- self . with_new_children ( children)
1282- }
1283- }
1284-
12851259#[ cfg( test) ]
12861260pub ( crate ) mod tests {
12871261 use crate :: tree_node:: {
1288- DynTreeNode , Transformed , TreeNode , TreeNodeIterator , TreeNodeRecursion ,
1289- TreeNodeRewriter , TreeNodeVisitor ,
1262+ Transformed , TreeNode , TreeNodeIterator , TreeNodeRecursion , TreeNodeRewriter ,
1263+ TreeNodeVisitor ,
12901264 } ;
12911265 use crate :: Result ;
12921266 use std:: collections:: HashMap ;
12931267 use std:: fmt:: Display ;
1294- use std:: sync:: Arc ;
12951268
12961269 macro_rules! visit_test {
12971270 ( $NAME: ident, $F_DOWN: expr, $F_UP: expr, $EXPECTED_VISITS: expr) => {
@@ -2523,81 +2496,6 @@ pub(crate) mod tests {
25232496 node_tests ! ( TestTreeNode ) ;
25242497 }
25252498
2526- mod test_dyn_tree_node {
2527- use super :: * ;
2528-
2529- #[ derive( Debug , Eq , Hash , PartialEq , Clone ) ]
2530- pub struct DynTestNode < T > {
2531- pub ( crate ) data : Arc < T > ,
2532- pub ( crate ) children : Vec < Arc < DynTestNode < T > > > ,
2533- }
2534-
2535- impl < T : PartialEq > TestTree < T > for Arc < DynTestNode < T > > {
2536- fn new_with_children ( children : Vec < Self > , data : T ) -> Self
2537- where
2538- Self : Sized ,
2539- {
2540- Arc :: new ( DynTestNode {
2541- data : Arc :: new ( data) ,
2542- children,
2543- } )
2544- }
2545-
2546- fn new_leaf ( data : T ) -> Self {
2547- Arc :: new ( DynTestNode {
2548- data : Arc :: new ( data) ,
2549- children : vec ! [ ] ,
2550- } )
2551- }
2552-
2553- fn is_leaf ( & self ) -> bool {
2554- self . children . is_empty ( )
2555- }
2556-
2557- fn eq_data ( & self , other : & T ) -> bool {
2558- self . data . as_ref ( ) . eq ( other)
2559- }
2560-
2561- fn with_children_from ( data : T , other : Self ) -> Self {
2562- Self :: new_with_children ( other. children . clone ( ) , data)
2563- }
2564- }
2565-
2566- impl < T > DynTreeNode for DynTestNode < T > {
2567- fn arc_children ( & self ) -> Vec < & Arc < Self > > {
2568- self . children . iter ( ) . collect ( )
2569- }
2570-
2571- fn with_new_arc_children (
2572- self : Arc < Self > ,
2573- new_children : Vec < Arc < Self > > ,
2574- ) -> Result < Arc < Self > > {
2575- Ok ( Arc :: new ( Self {
2576- children : new_children,
2577- data : Arc :: clone ( & self . data ) ,
2578- } ) )
2579- }
2580- }
2581-
2582- type ArcTestNode < T > = Arc < DynTestNode < T > > ;
2583-
2584- node_tests ! ( ArcTestNode ) ;
2585-
2586- #[ test]
2587- fn test_large_visit ( ) {
2588- let mut item = ArcTestNode :: new_leaf ( "initial" . to_string ( ) ) ;
2589- for i in 0 ..3000 {
2590- item =
2591- ArcTestNode :: new_with_children ( vec ! [ item] , format ! ( "parent-{}" , i) ) ;
2592- }
2593-
2594- let mut visitor =
2595- TestVisitor :: new ( Box :: new ( visit_continue) , Box :: new ( visit_continue) ) ;
2596-
2597- item. visit ( & mut visitor) . unwrap ( ) ;
2598- }
2599- }
2600-
26012499 mod test_concrete_tree_node {
26022500 use super :: * ;
26032501 use crate :: tree_node:: ConcreteTreeNode ;
@@ -2606,8 +2504,8 @@ pub(crate) mod tests {
26062504 test_node ! ( ConcreteTestTreeNode ) ;
26072505
26082506 impl < T : PartialEq > ConcreteTreeNode for ConcreteTestTreeNode < T > {
2609- fn children ( & self ) -> & [ Self ] {
2610- self . children . as_slice ( )
2507+ fn children ( & self ) -> Vec < & Self > {
2508+ self . children . iter ( ) . collect ( )
26112509 }
26122510
26132511 fn take_children ( mut self ) -> ( Self , Vec < Self > ) {
0 commit comments