@@ -450,15 +450,18 @@ impl<N: Clone, E: Clone> Clone for StackGraph<N, E> {
450
450
}
451
451
}
452
452
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
+
453
462
impl < N : Clone , E : Clone > Drop for StackGraph < N , E > {
454
463
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 ) ;
462
465
}
463
466
}
464
467
@@ -488,20 +491,14 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
488
491
/// Gets mutable access to the inner `Graph`. Uses `other_refs` to determine if a deep clone is
489
492
/// needed, and runs `reset_to` to get the `Graph` into the state it was in.
490
493
///
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.
492
495
fn activate ( & mut self ) -> RefMut < ' _ , Graph < N , E > > {
493
496
let inner = if {
494
497
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 ) ;
501
499
borrow
502
500
. keys ( )
503
- . rev ( )
504
- . next ( )
501
+ . next_back ( )
505
502
. map ( |a| a <= & self . age )
506
503
. unwrap_or ( true )
507
504
} {
@@ -523,15 +520,23 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
523
520
inner
524
521
}
525
522
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
+
526
533
pub fn add ( & mut self , node : N ) {
527
534
self . age = {
528
535
let mut g = self . activate ( ) ;
529
536
g. add ( node) ;
530
537
g. len ( )
531
538
} ;
532
- * RefCell :: borrow_mut ( & self . other_refs )
533
- . entry ( self . age )
534
- . or_insert ( 0 ) += 1 ;
539
+ self . add_to_other_refs ( ) ;
535
540
}
536
541
537
542
/// connect `node`to `child` with out associating any data.
@@ -541,9 +546,7 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
541
546
g. link ( node, child) ;
542
547
g. len ( )
543
548
} ;
544
- * RefCell :: borrow_mut ( & self . other_refs )
545
- . entry ( self . age )
546
- . or_insert ( 0 ) += 1 ;
549
+ self . add_to_other_refs ( ) ;
547
550
}
548
551
549
552
/// connect `node`to `child` associating it with `edge`.
@@ -553,9 +556,7 @@ impl<N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraph<N, E> {
553
556
g. add_edge ( node, child, edge) ;
554
557
g. len ( )
555
558
} ;
556
- * RefCell :: borrow_mut ( & self . other_refs )
557
- . entry ( self . age )
558
- . or_insert ( 0 ) += 1 ;
559
+ self . add_to_other_refs ( ) ;
559
560
}
560
561
}
561
562
@@ -717,28 +718,4 @@ impl<'a, N: Eq + Hash + Clone, E: Clone + PartialEq> StackGraphView<'a, N, E> {
717
718
} )
718
719
} )
719
720
}
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
- }
744
721
}
0 commit comments