refactor: rename and type-alias to clarify patch arguments

This commit is contained in:
Weihang Lo 2024-05-17 00:03:47 -04:00
parent 4e393497fa
commit 8e051a692b
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7

View File

@ -157,6 +157,15 @@ enum Kind {
Normal, Normal,
} }
/// This tuple is an argument to [`PackageRegistry::patch`].
///
/// * The first element is the patch definition straight from the manifest.
/// * The second element is an optional variant where the patch has been locked.
/// It is the patch locked to a specific version found in Cargo.lock.
/// This will be `None` if `Cargo.lock` doesn't exist,
/// or the patch did not match any existing entries in `Cargo.lock`.
pub type PatchDependency<'a> = (&'a Dependency, Option<LockedPatchDependency>);
/// Argument to [`PackageRegistry::patch`] which is information about a `[patch]` /// Argument to [`PackageRegistry::patch`] which is information about a `[patch]`
/// directive that we found in a lockfile, if present. /// directive that we found in a lockfile, if present.
pub struct LockedPatchDependency { pub struct LockedPatchDependency {
@ -303,14 +312,8 @@ impl<'gctx> PackageRegistry<'gctx> {
/// The `deps` is an array of all the entries in the `[patch]` section of /// The `deps` is an array of all the entries in the `[patch]` section of
/// the manifest. /// the manifest.
/// ///
/// Here the `deps` will be resolved to a precise version and stored /// Here the `patch_deps` will be resolved to a precise version and stored
/// internally for future calls to `query` below. `deps` should be a tuple /// internally for future calls to `query` below.
/// where the first element is the patch definition straight from the
/// manifest, and the second element is an optional variant where the
/// patch has been locked. This locked patch is the patch locked to
/// a specific version found in Cargo.lock. This will be `None` if
/// `Cargo.lock` doesn't exist, or the patch did not match any existing
/// entries in `Cargo.lock`.
/// ///
/// Note that the patch list specified here *will not* be available to /// Note that the patch list specified here *will not* be available to
/// [`Registry::query`] until [`PackageRegistry::lock_patches`] is called /// [`Registry::query`] until [`PackageRegistry::lock_patches`] is called
@ -322,7 +325,7 @@ impl<'gctx> PackageRegistry<'gctx> {
pub fn patch( pub fn patch(
&mut self, &mut self,
url: &Url, url: &Url,
deps: &[(&Dependency, Option<LockedPatchDependency>)], patch_deps: &[PatchDependency<'_>],
) -> CargoResult<Vec<(Dependency, PackageId)>> { ) -> CargoResult<Vec<(Dependency, PackageId)>> {
// NOTE: None of this code is aware of required features. If a patch // NOTE: None of this code is aware of required features. If a patch
// is missing a required feature, you end up with an "unused patch" // is missing a required feature, you end up with an "unused patch"
@ -333,9 +336,9 @@ impl<'gctx> PackageRegistry<'gctx> {
// Return value of patches that shouldn't be locked. // Return value of patches that shouldn't be locked.
let mut unlock_patches = Vec::new(); let mut unlock_patches = Vec::new();
// First up we need to actually resolve each `deps` specification to // First up we need to actually resolve each `patch_deps` specification
// precisely one summary. We're not using the `query` method below as it // to precisely one summary. We're not using the `query` method below
// internally uses maps we're building up as part of this method // as it internally uses maps we're building up as part of this method
// (`patches_available` and `patches`). Instead we're going straight to // (`patches_available` and `patches`). Instead we're going straight to
// the source to load information from it. // the source to load information from it.
// //
@ -343,12 +346,12 @@ impl<'gctx> PackageRegistry<'gctx> {
// precisely one package, so that's why we're just creating a flat list // precisely one package, so that's why we're just creating a flat list
// of summaries which should be the same length as `deps` above. // of summaries which should be the same length as `deps` above.
let mut deps_remaining: Vec<_> = deps.iter().collect(); let mut patch_deps_remaining: Vec<_> = patch_deps.iter().collect();
let mut unlocked_summaries = Vec::new(); let mut unlocked_summaries = Vec::new();
while !deps_remaining.is_empty() { while !patch_deps_remaining.is_empty() {
let mut deps_pending = Vec::new(); let mut patch_deps_pending = Vec::new();
for dep_remaining in deps_remaining { for patch_dep_remaining in patch_deps_remaining {
let (orig_patch, locked) = dep_remaining; let (orig_patch, locked) = patch_dep_remaining;
// Use the locked patch if it exists, otherwise use the original. // Use the locked patch if it exists, otherwise use the original.
let dep = match locked { let dep = match locked {
@ -388,7 +391,7 @@ impl<'gctx> PackageRegistry<'gctx> {
let summaries = match source.query_vec(dep, QueryKind::Exact)? { let summaries = match source.query_vec(dep, QueryKind::Exact)? {
Poll::Ready(deps) => deps, Poll::Ready(deps) => deps,
Poll::Pending => { Poll::Pending => {
deps_pending.push(dep_remaining); patch_deps_pending.push(patch_dep_remaining);
continue; continue;
} }
}; };
@ -399,7 +402,7 @@ impl<'gctx> PackageRegistry<'gctx> {
match summary_for_patch(orig_patch, &locked, summaries, source) { match summary_for_patch(orig_patch, &locked, summaries, source) {
Poll::Ready(x) => x, Poll::Ready(x) => x,
Poll::Pending => { Poll::Pending => {
deps_pending.push(dep_remaining); patch_deps_pending.push(patch_dep_remaining);
continue; continue;
} }
} }
@ -432,7 +435,7 @@ impl<'gctx> PackageRegistry<'gctx> {
unlocked_summaries.push(summary); unlocked_summaries.push(summary);
} }
deps_remaining = deps_pending; patch_deps_remaining = patch_deps_pending;
self.block_until_ready()?; self.block_until_ready()?;
} }
@ -467,7 +470,7 @@ impl<'gctx> PackageRegistry<'gctx> {
// in the future, but for now it hopefully doesn't cause too much // in the future, but for now it hopefully doesn't cause too much
// harm... // harm...
let mut ids = Vec::new(); let mut ids = Vec::new();
for (summary, (_, lock)) in unlocked_summaries.iter().zip(deps) { for (summary, (_, lock)) in unlocked_summaries.iter().zip(patch_deps) {
ids.push(summary.package_id()); ids.push(summary.package_id());
if let Some(lock) = lock { if let Some(lock) = lock {
ids.extend(lock.alt_package_id); ids.extend(lock.alt_package_id);