Skip to content

Commit 8a25085

Browse files
committed
refactor(update): Pull out package_id search
1 parent 94f67d0 commit 8a25085

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -255,37 +255,35 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
255255
(dep.name().as_str(), dep.source_id())
256256
}
257257

258-
// Removes all package IDs in `b` from `a`. Note that this is somewhat
259-
// more complicated because the equality for source IDs does not take
260-
// precise versions into account (e.g., git shas), but we want to take
261-
// that into account here.
262258
fn vec_subtract(a: &[PackageId], b: &[PackageId]) -> Vec<PackageId> {
263-
a.iter()
264-
.filter(|a| {
265-
// If this package ID is not found in `b`, then it's definitely
266-
// in the subtracted set.
267-
let Ok(i) = b.binary_search(a) else {
268-
return true;
269-
};
259+
a.iter().filter(|a| !contains_id(b, a)).cloned().collect()
260+
}
270261

271-
// If we've found `a` in `b`, then we iterate over all instances
272-
// (we know `b` is sorted) and see if they all have different
273-
// precise versions. If so, then `a` isn't actually in `b` so
274-
// we'll let it through.
275-
//
276-
// Note that we only check this for non-registry sources,
277-
// however, as registries contain enough version information in
278-
// the package ID to disambiguate.
279-
if a.source_id().is_registry() {
280-
return false;
281-
}
282-
b[i..]
283-
.iter()
284-
.take_while(|b| a == b)
285-
.all(|b| !a.source_id().has_same_precise_as(b.source_id()))
286-
})
287-
.cloned()
288-
.collect()
262+
// Check if a PackageId is present `b` from `a`.
263+
//
264+
// Note that this is somewhat more complicated because the equality for source IDs does not
265+
// take precise versions into account (e.g., git shas), but we want to take that into
266+
// account here.
267+
fn contains_id(haystack: &[PackageId], needle: &PackageId) -> bool {
268+
let Ok(i) = haystack.binary_search(needle) else {
269+
return false;
270+
};
271+
272+
// If we've found `a` in `b`, then we iterate over all instances
273+
// (we know `b` is sorted) and see if they all have different
274+
// precise versions. If so, then `a` isn't actually in `b` so
275+
// we'll let it through.
276+
//
277+
// Note that we only check this for non-registry sources,
278+
// however, as registries contain enough version information in
279+
// the package ID to disambiguate.
280+
if needle.source_id().is_registry() {
281+
return true;
282+
}
283+
haystack[i..]
284+
.iter()
285+
.take_while(|b| &needle == b)
286+
.any(|b| needle.source_id().has_same_precise_as(b.source_id()))
289287
}
290288

291289
// Map `(package name, package source)` to `(removed versions, added versions)`.

0 commit comments

Comments
 (0)