@@ -450,15 +450,18 @@ impl<N: Clone, E: Clone> Clone for StackGraph<N, E> {
450450 }
451451}
452452
453+ fn remove_from_other_refs ( borrow : & mut RefMut < ' _ , BTreeMap < GraphAge , usize > > , age : GraphAge ) {
454+ if let std:: collections:: btree_map:: Entry :: Occupied ( mut val) = borrow. entry ( age) {
455+ * val. get_mut ( ) -= 1 ;
456+ if val. get ( ) == & 0 {
457+ val. remove ( ) ;
458+ }
459+ }
460+ }
461+
453462impl < N : Clone , E : Clone > Drop for StackGraph < N , E > {
454463 fn drop ( & mut self ) {
455- let mut borrow = RefCell :: borrow_mut ( & self . other_refs ) ;
456- if let std:: collections:: btree_map:: Entry :: Occupied ( mut val) = borrow. entry ( self . age ) {
457- * val. get_mut ( ) -= 1 ;
458- if val. get ( ) == & 0 {
459- val. remove ( ) ;
460- }
461- }
464+ remove_from_other_refs ( & mut RefCell :: borrow_mut ( & self . other_refs ) , self . age ) ;
462465 }
463466}
464467
@@ -488,20 +491,14 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
488491 /// Gets mutable access to the inner `Graph`. Uses `other_refs` to determine if a deep clone is
489492 /// needed, and runs `reset_to` to get the `Graph` into the state it was in.
490493 ///
491- /// It is the responsibility of the caller to re-add this clone to `other_refs ` after the new age is determined.
494+ /// It is the responsibility of the caller to set `self.age` and call `add_to_other_refs ` after the new age is determined.
492495 fn activate ( & mut self ) -> RefMut < ' _ , Graph < N , E > > {
493496 let inner = if {
494497 let mut borrow = RefCell :: borrow_mut ( & self . other_refs ) ;
495- if let std:: collections:: btree_map:: Entry :: Occupied ( mut val) = borrow. entry ( self . age ) {
496- * val. get_mut ( ) -= 1 ;
497- if val. get ( ) == & 0 {
498- val. remove ( ) ;
499- }
500- }
498+ remove_from_other_refs ( & mut borrow, self . age ) ;
501499 borrow
502500 . keys ( )
503- . rev ( )
504- . next ( )
501+ . next_back ( )
505502 . map ( |a| a <= & self . age )
506503 . unwrap_or ( true )
507504 } {
@@ -523,15 +520,23 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
523520 inner
524521 }
525522
523+ fn add_to_other_refs ( & mut self ) {
524+ * RefCell :: borrow_mut ( & self . other_refs )
525+ . entry ( self . age )
526+ . or_insert ( 0 ) += 1 ;
527+ }
528+
529+ pub fn clone_into_graph ( & self ) -> Graph < N , E > {
530+ self . clone ( ) . activate ( ) . clone ( )
531+ }
532+
526533 pub fn add ( & mut self , node : N ) {
527534 self . age = {
528535 let mut g = self . activate ( ) ;
529536 g. add ( node) ;
530537 g. len ( )
531538 } ;
532- * RefCell :: borrow_mut ( & self . other_refs )
533- . entry ( self . age )
534- . or_insert ( 0 ) += 1 ;
539+ self . add_to_other_refs ( ) ;
535540 }
536541
537542 /// connect `node`to `child` with out associating any data.
@@ -541,9 +546,7 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
541546 g. link ( node, child) ;
542547 g. len ( )
543548 } ;
544- * RefCell :: borrow_mut ( & self . other_refs )
545- . entry ( self . age )
546- . or_insert ( 0 ) += 1 ;
549+ self . add_to_other_refs ( ) ;
547550 }
548551
549552 /// connect `node`to `child` associating it with `edge`.
@@ -553,9 +556,7 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
553556 g. add_edge ( node, child, edge) ;
554557 g. len ( )
555558 } ;
556- * RefCell :: borrow_mut ( & self . other_refs )
557- . entry ( self . age )
558- . or_insert ( 0 ) += 1 ;
559+ self . add_to_other_refs ( ) ;
559560 }
560561}
561562
@@ -717,28 +718,4 @@ impl<'a, N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraphView<'a, N, E> {
717718 } )
718719 } )
719720 }
720-
721- /// Resolves one of the paths from the given dependent package down to
722- /// a leaf.
723- pub fn path_to_bottom < ' s > ( & ' s self , mut pkg : & ' s N ) -> Vec < & ' s N > {
724- let mut result = vec ! [ pkg] ;
725- while let Some ( p) = self
726- . inner
727- . nodes
728- . get_full ( pkg)
729- . filter ( |( i, _, _) | * i < self . age . len_nodes )
730- . and_then ( |( _, _, p) | {
731- p. iter ( )
732- . filter ( |( _, idx) | * * idx < self . age . len_edges )
733- // Note that we can have "cycles" introduced through dev-dependency
734- // edges, so make sure we don't loop infinitely.
735- . find ( |& ( node, _) | !result. contains ( & node) )
736- . map ( |( p, _) | p)
737- } )
738- {
739- result. push ( p) ;
740- pkg = p;
741- }
742- result
743- }
744721}
0 commit comments