Skip to content

Commit 451f76d

Browse files
committed
remove some duplication
1 parent 2340384 commit 451f76d

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

src/cargo/core/resolver/errors.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ pub(super) fn activation_error(
7777
candidates: &[Summary],
7878
config: Option<&Config>,
7979
) -> ResolveError {
80+
let parents = cx.parents.clone_into_graph();
8081
let to_resolve_err = |err| {
8182
ResolveError::new(
8283
err,
83-
cx.parents
84-
.borrow()
84+
parents
8585
.path_to_bottom(&parent.package_id())
8686
.into_iter()
8787
.cloned()
@@ -93,7 +93,7 @@ pub(super) fn activation_error(
9393
let mut msg = format!("failed to select a version for `{}`.", dep.package_name());
9494
msg.push_str("\n ... required by ");
9595
msg.push_str(&describe_path(
96-
&cx.parents.borrow().path_to_bottom(&parent.package_id()),
96+
&parents.path_to_bottom(&parent.package_id()),
9797
));
9898

9999
msg.push_str("\nversions that meet the requirements `");
@@ -125,7 +125,7 @@ pub(super) fn activation_error(
125125
msg.push_str(link);
126126
msg.push_str("` as well:\n");
127127
}
128-
msg.push_str(&describe_path(&cx.parents.borrow().path_to_bottom(p)));
128+
msg.push_str(&describe_path(&parents.path_to_bottom(p)));
129129
}
130130

131131
let (features_errors, mut other_errors): (Vec<_>, Vec<_>) = other_errors
@@ -179,7 +179,7 @@ pub(super) fn activation_error(
179179

180180
for &(p, _) in other_errors.iter() {
181181
msg.push_str("\n\n previously selected ");
182-
msg.push_str(&describe_path(&cx.parents.borrow().path_to_bottom(p)));
182+
msg.push_str(&describe_path(&parents.path_to_bottom(p)));
183183
}
184184

185185
msg.push_str("\n\nfailed to select a version for `");
@@ -231,7 +231,7 @@ pub(super) fn activation_error(
231231
);
232232
msg.push_str("required by ");
233233
msg.push_str(&describe_path(
234-
&cx.parents.borrow().path_to_bottom(&parent.package_id()),
234+
&parents.path_to_bottom(&parent.package_id()),
235235
));
236236

237237
// If we have a path dependency with a locked version, then this may
@@ -308,7 +308,7 @@ pub(super) fn activation_error(
308308
}
309309
msg.push_str("required by ");
310310
msg.push_str(&describe_path(
311-
&cx.parents.borrow().path_to_bottom(&parent.package_id()),
311+
&parents.path_to_bottom(&parent.package_id()),
312312
));
313313

314314
msg

src/cargo/util/graph.rs

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
453462
impl<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

Comments
 (0)