Skip to content

Commit

Permalink
refactor(source): Replace bool with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 20, 2022
1 parent 617ab8d commit 5265cb7
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 85 deletions.
10 changes: 7 additions & 3 deletions crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::time::Instant;

use cargo::core::dependency::DepKind;
use cargo::core::resolver::{self, ResolveOpts, VersionPreferences};
use cargo::core::source::{GitReference, SourceId};
use cargo::core::source::{GitReference, QueryKind, SourceId};
use cargo::core::Resolve;
use cargo::core::{Dependency, PackageId, Registry, Summary};
use cargo::util::{CargoResult, Config, Graph, IntoUrl};
Expand Down Expand Up @@ -128,11 +128,15 @@ pub fn resolve_with_config_raw(
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
for summary in self.list.iter() {
if fuzzy || dep.matches(summary) {
let matched = match kind {
QueryKind::Exact => dep.matches(summary),
QueryKind::Fuzzy => true,
};
if matched {
self.used.insert(summary.package_id());
f(summary.clone());
}
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Support for future-incompatible warning reporting.
use crate::core::compiler::BuildContext;
use crate::core::{Dependency, PackageId, Workspace};
use crate::core::{Dependency, PackageId, QueryKind, Workspace};
use crate::sources::SourceConfigMap;
use crate::util::{iter_join, CargoResult, Config};
use anyhow::{bail, format_err, Context};
Expand Down Expand Up @@ -293,7 +293,7 @@ fn get_updates(ws: &Workspace<'_>, package_ids: &BTreeSet<PackageId>) -> Option<
Ok(dep) => dep,
Err(_) => return false,
};
match source.query_vec(&dep, false) {
match source.query_vec(&dep, QueryKind::Exact) {
Poll::Ready(Ok(sum)) => {
summaries.push((pkg_id, sum));
false
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use self::package_id_spec::PackageIdSpec;
pub use self::registry::Registry;
pub use self::resolver::{Resolve, ResolveVersion};
pub use self::shell::{Shell, Verbosity};
pub use self::source::{GitReference, Source, SourceId, SourceMap};
pub use self::source::{GitReference, QueryKind, Source, SourceId, SourceMap};
pub use self::summary::{FeatureMap, FeatureValue, Summary};
pub use self::workspace::{
find_workspace_root, resolve_relative_path, MaybePackage, Workspace, WorkspaceConfig,
Expand Down
23 changes: 11 additions & 12 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::task::Poll;

use crate::core::PackageSet;
use crate::core::{Dependency, PackageId, Source, SourceId, SourceMap, Summary};
use crate::core::{Dependency, PackageId, QueryKind, Source, SourceId, SourceMap, Summary};
use crate::sources::config::SourceConfigMap;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
Expand All @@ -19,14 +19,13 @@ pub trait Registry {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>>;

fn query_vec(&mut self, dep: &Dependency, fuzzy: bool) -> Poll<CargoResult<Vec<Summary>>> {
fn query_vec(&mut self, dep: &Dependency, kind: QueryKind) -> Poll<CargoResult<Vec<Summary>>> {
let mut ret = Vec::new();
self.query(dep, fuzzy, &mut |s| ret.push(s))
.map_ok(|()| ret)
self.query(dep, kind, &mut |s| ret.push(s)).map_ok(|()| ret)
}

fn describe_source(&self, source: SourceId) -> String;
Expand Down Expand Up @@ -327,7 +326,7 @@ impl<'cfg> PackageRegistry<'cfg> {
.get_mut(dep.source_id())
.expect("loaded source not present");

let summaries = match source.query_vec(dep, false)? {
let summaries = match source.query_vec(dep, QueryKind::Exact)? {
Poll::Ready(deps) => deps,
Poll::Pending => {
deps_pending.push(dep_remaining);
Expand Down Expand Up @@ -483,7 +482,7 @@ impl<'cfg> PackageRegistry<'cfg> {
for &s in self.overrides.iter() {
let src = self.sources.get_mut(s).unwrap();
let dep = Dependency::new_override(dep.package_name(), s);
let mut results = match src.query_vec(&dep, false) {
let mut results = match src.query_vec(&dep, QueryKind::Exact) {
Poll::Ready(results) => results?,
Poll::Pending => return Poll::Pending,
};
Expand Down Expand Up @@ -575,7 +574,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
assert!(self.patches_locked);
Expand Down Expand Up @@ -671,7 +670,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
}
f(lock(locked, all_patches, summary))
};
return source.query(dep, fuzzy, callback);
return source.query(dep, kind, callback);
}

// If we have an override summary then we query the source
Expand All @@ -690,7 +689,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
n += 1;
to_warn = Some(summary);
};
let pend = source.query(dep, fuzzy, callback);
let pend = source.query(dep, kind, callback);
if pend.is_pending() {
return Poll::Pending;
}
Expand Down Expand Up @@ -881,7 +880,7 @@ fn summary_for_patch(
// No summaries found, try to help the user figure out what is wrong.
if let Some(locked) = locked {
// Since the locked patch did not match anything, try the unlocked one.
let orig_matches = match source.query_vec(orig_patch, false) {
let orig_matches = match source.query_vec(orig_patch, QueryKind::Exact) {
Poll::Pending => return Poll::Pending,
Poll::Ready(deps) => deps,
}
Expand All @@ -906,7 +905,7 @@ fn summary_for_patch(
// Try checking if there are *any* packages that match this by name.
let name_only_dep = Dependency::new_override(orig_patch.package_name(), orig_patch.source_id());

let name_summaries = match source.query_vec(&name_only_dep, false) {
let name_summaries = match source.query_vec(&name_only_dep, QueryKind::Exact) {
Poll::Pending => return Poll::Pending,
Poll::Ready(deps) => deps,
}
Expand Down
8 changes: 5 additions & 3 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use crate::core::resolver::{
ActivateError, ActivateResult, CliFeatures, RequestedFeatures, ResolveOpts, VersionOrdering,
VersionPreferences,
};
use crate::core::{Dependency, FeatureValue, PackageId, PackageIdSpec, Registry, Summary};
use crate::core::{
Dependency, FeatureValue, PackageId, PackageIdSpec, QueryKind, Registry, Summary,
};
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;

Expand Down Expand Up @@ -100,7 +102,7 @@ impl<'a> RegistryQueryer<'a> {
}

let mut ret = Vec::new();
let ready = self.registry.query(dep, false, &mut |s| {
let ready = self.registry.query(dep, QueryKind::Exact, &mut |s| {
ret.push(s);
})?;
if ready.is_pending() {
Expand All @@ -123,7 +125,7 @@ impl<'a> RegistryQueryer<'a> {
dep.version_req()
);

let mut summaries = match self.registry.query_vec(dep, false)? {
let mut summaries = match self.registry.query_vec(dep, QueryKind::Exact)? {
Poll::Ready(s) => s.into_iter(),
Poll::Pending => {
self.registry_cache.insert(dep.clone(), Poll::Pending);
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::task::Poll;

use crate::core::{Dependency, PackageId, Registry, Summary};
use crate::core::{Dependency, PackageId, QueryKind, Registry, Summary};
use crate::util::lev_distance::lev_distance;
use crate::util::{Config, VersionExt};
use anyhow::Error;
Expand Down Expand Up @@ -228,7 +228,7 @@ pub(super) fn activation_error(
new_dep.set_version_req(all_req);

let mut candidates = loop {
match registry.query_vec(&new_dep, false) {
match registry.query_vec(&new_dep, QueryKind::Exact) {
Poll::Ready(Ok(candidates)) => break candidates,
Poll::Ready(Err(e)) => return to_resolve_err(e),
Poll::Pending => match registry.block_until_ready() {
Expand Down Expand Up @@ -294,7 +294,7 @@ pub(super) fn activation_error(
// Maybe the user mistyped the name? Like `dep-thing` when `Dep_Thing`
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
let mut candidates = loop {
match registry.query_vec(&new_dep, true) {
match registry.query_vec(&new_dep, QueryKind::Fuzzy) {
Poll::Ready(Ok(candidates)) => break candidates,
Poll::Ready(Err(e)) => return to_resolve_err(e),
Poll::Pending => match registry.block_until_ready() {
Expand Down
26 changes: 15 additions & 11 deletions src/cargo/core/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ pub trait Source {
fn requires_precise(&self) -> bool;

/// Attempts to find the packages that match a dependency request.
///
/// When fuzzy, each source gets to define what `close` means for it.
/// Path/Git sources may return all dependencies that are at that URI,
/// whereas an `Index` source may return dependencies that have the same canonicalization.
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>>;

fn query_vec(&mut self, dep: &Dependency, fuzzy: bool) -> Poll<CargoResult<Vec<Summary>>> {
fn query_vec(&mut self, dep: &Dependency, kind: QueryKind) -> Poll<CargoResult<Vec<Summary>>> {
let mut ret = Vec::new();
self.query(dep, fuzzy, &mut |s| ret.push(s)).map_ok(|_| ret)
self.query(dep, kind, &mut |s| ret.push(s)).map_ok(|_| ret)
}

/// Ensure that the source is fully up-to-date for the current session on the next query.
Expand Down Expand Up @@ -114,6 +110,14 @@ pub trait Source {
fn block_until_ready(&mut self) -> CargoResult<()>;
}

pub enum QueryKind {
Exact,
/// Each source gets to define what `close` means for it.
/// Path/Git sources may return all dependencies that are at that URI,
/// whereas an `Index` source may return dependencies that have the same canonicalization.
Fuzzy,
}

pub enum MaybePackage {
Ready(Package),
Download { url: String, descriptor: String },
Expand Down Expand Up @@ -144,10 +148,10 @@ impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
(**self).query(dep, fuzzy, f)
(**self).query(dep, kind, f)
}

fn invalidate_cache(&mut self) {
Expand Down Expand Up @@ -214,10 +218,10 @@ impl<'a, T: Source + ?Sized + 'a> Source for &'a mut T {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
(**self).query(dep, fuzzy, f)
(**self).query(dep, kind, f)
}

fn invalidate_cache(&mut self) {
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use toml_edit::Item as TomlItem;
use crate::core::dependency::DepKind;
use crate::core::registry::PackageRegistry;
use crate::core::Package;
use crate::core::QueryKind;
use crate::core::Registry;
use crate::core::Shell;
use crate::core::Workspace;
Expand Down Expand Up @@ -443,8 +444,7 @@ fn get_latest_dependency(
}
MaybeWorkspace::Other(query) => {
let possibilities = loop {
let fuzzy = true;
match registry.query_vec(&query, fuzzy) {
match registry.query_vec(&query, QueryKind::Fuzzy) {
std::task::Poll::Ready(res) => {
break res?;
}
Expand Down Expand Up @@ -485,8 +485,8 @@ fn select_package(
}
MaybeWorkspace::Other(query) => {
let possibilities = loop {
let fuzzy = false; // Returns all for path/git
match registry.query_vec(&query, fuzzy) {
// Exact to avoid returning all for path/git
match registry.query_vec(&query, QueryKind::Exact) {
std::task::Poll::Ready(res) => {
break res?;
}
Expand Down Expand Up @@ -600,7 +600,7 @@ fn populate_available_features(
MaybeWorkspace::Other(query) => query,
};
let possibilities = loop {
match registry.query_vec(&query, true) {
match registry.query_vec(&query, QueryKind::Fuzzy) {
std::task::Poll::Ready(res) => {
break res?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/common_for_install_and_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
use toml_edit::easy as toml;

use crate::core::compiler::Freshness;
use crate::core::{Dependency, FeatureValue, Package, PackageId, Source, SourceId};
use crate::core::{Dependency, FeatureValue, Package, PackageId, QueryKind, Source, SourceId};
use crate::ops::{self, CompileFilter, CompileOptions};
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
Expand Down Expand Up @@ -540,7 +540,7 @@ where
}

let deps = loop {
match source.query_vec(&dep, false)? {
match source.query_vec(&dep, QueryKind::Exact)? {
Poll::Ready(deps) => break deps,
Poll::Pending => source.block_until_ready()?,
}
Expand Down
9 changes: 6 additions & 3 deletions src/cargo/sources/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use std::task::Poll;

use crate::core::source::MaybePackage;
use crate::core::{Dependency, Package, PackageId, Source, SourceId, Summary};
use crate::core::{Dependency, Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::sources::PathSource;
use crate::util::errors::CargoResult;
use crate::util::Config;
Expand Down Expand Up @@ -49,14 +49,17 @@ impl<'cfg> Source for DirectorySource<'cfg> {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
if !self.updated {
return Poll::Pending;
}
let packages = self.packages.values().map(|p| &p.0);
let matches = packages.filter(|pkg| fuzzy || dep.matches(pkg.summary()));
let matches = packages.filter(|pkg| match kind {
QueryKind::Exact => dep.matches(pkg.summary()),
QueryKind::Fuzzy => true,
});
for summary in matches.map(|pkg| pkg.summary().clone()) {
f(summary);
}
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::source::{MaybePackage, Source, SourceId};
use crate::core::source::{MaybePackage, QueryKind, Source, SourceId};
use crate::core::GitReference;
use crate::core::{Dependency, Package, PackageId, Summary};
use crate::sources::git::utils::GitRemote;
Expand Down Expand Up @@ -89,11 +89,11 @@ impl<'cfg> Source for GitSource<'cfg> {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
if let Some(src) = self.path_source.as_mut() {
src.query(dep, fuzzy, f)
src.query(dep, kind, f)
} else {
Poll::Pending
}
Expand Down
10 changes: 7 additions & 3 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
use std::task::Poll;

use crate::core::source::MaybePackage;
use crate::core::{Dependency, Package, PackageId, Source, SourceId, Summary};
use crate::core::{Dependency, Package, PackageId, QueryKind, Source, SourceId, Summary};
use crate::ops;
use crate::util::{internal, CargoResult, Config};
use anyhow::Context as _;
Expand Down Expand Up @@ -500,12 +500,16 @@ impl<'cfg> Source for PathSource<'cfg> {
fn query(
&mut self,
dep: &Dependency,
fuzzy: bool,
kind: QueryKind,
f: &mut dyn FnMut(Summary),
) -> Poll<CargoResult<()>> {
self.update()?;
for s in self.packages.iter().map(|p| p.summary()) {
if fuzzy || dep.matches(s) {
let matched = match kind {
QueryKind::Exact => dep.matches(s),
QueryKind::Fuzzy => true,
};
if matched {
f(s.clone())
}
}
Expand Down
Loading

0 comments on commit 5265cb7

Please sign in to comment.