Skip to content

Commit

Permalink
switch to std::task::ready!() where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Sep 22, 2022
1 parent 247ca7f commit 0b70fd4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 59 deletions.
59 changes: 22 additions & 37 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::task::Poll;
use std::task::{ready, Poll};

use crate::core::PackageSet;
use crate::core::{Dependency, PackageId, QueryKind, Source, SourceId, SourceMap, Summary};
Expand Down Expand Up @@ -482,10 +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, QueryKind::Exact) {
Poll::Ready(results) => results?,
Poll::Pending => return Poll::Pending,
};
let mut results = ready!(src.query_vec(&dep, QueryKind::Exact))?;
if !results.is_empty() {
return Poll::Ready(Ok(Some(results.remove(0))));
}
Expand Down Expand Up @@ -580,10 +577,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
assert!(self.patches_locked);
let (override_summary, n, to_warn) = {
// Look for an override and get ready to query the real source.
let override_summary = match self.query_overrides(dep) {
Poll::Ready(override_summary) => override_summary?,
Poll::Pending => return Poll::Pending,
};
let override_summary = ready!(self.query_overrides(dep))?;

// Next up on our list of candidates is to check the `[patch]`
// section of the manifest. Here we look through all patches
Expand Down Expand Up @@ -880,23 +874,17 @@ 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, QueryKind::Exact) {
Poll::Pending => return Poll::Pending,
Poll::Ready(deps) => deps,
}
.unwrap_or_else(|e| {
log::warn!(
"could not determine unlocked summaries for dep {:?}: {:?}",
orig_patch,
e
);
Vec::new()
});
let orig_matches =
ready!(source.query_vec(orig_patch, QueryKind::Exact)).unwrap_or_else(|e| {
log::warn!(
"could not determine unlocked summaries for dep {:?}: {:?}",
orig_patch,
e
);
Vec::new()
});

let summary = match summary_for_patch(orig_patch, &None, orig_matches, source) {
Poll::Pending => return Poll::Pending,
Poll::Ready(summary) => summary?,
};
let summary = ready!(summary_for_patch(orig_patch, &None, orig_matches, source))?;

// The unlocked version found a match. This returns a value to
// indicate that this entry should be unlocked.
Expand All @@ -905,18 +893,15 @@ 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, QueryKind::Exact) {
Poll::Pending => return Poll::Pending,
Poll::Ready(deps) => deps,
}
.unwrap_or_else(|e| {
log::warn!(
"failed to do name-only summary query for {:?}: {:?}",
name_only_dep,
e
);
Vec::new()
});
let name_summaries =
ready!(source.query_vec(&name_only_dep, QueryKind::Exact)).unwrap_or_else(|e| {
log::warn!(
"failed to do name-only summary query for {:?}: {:?}",
name_only_dep,
e
);
Vec::new()
});
let mut vers = name_summaries
.iter()
.map(|summary| summary.version())
Expand Down
7 changes: 2 additions & 5 deletions src/cargo/sources/registry/http_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::collections::{HashMap, HashSet};
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use std::str;
use std::task::Poll;
use std::task::{Poll, ready};
use std::time::Duration;
use url::Url;

Expand Down Expand Up @@ -383,10 +383,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {

// Load the registry config.
if self.registry_config.is_none() && path != Path::new("config.json") {
match self.config()? {
Poll::Ready(_) => {}
Poll::Pending => return Poll::Pending,
}
ready!(self.config()?);
}

let mut handle = ops::http_handle(self.config)?;
Expand Down
22 changes: 5 additions & 17 deletions src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ use std::fs;
use std::io::ErrorKind;
use std::path::Path;
use std::str;
use std::task::Poll;
use std::task::{Poll, ready};

/// Crates.io treats hyphen and underscores as interchangeable, but the index and old Cargo do not.
/// Therefore, the index must store uncanonicalized version of the name so old Cargo's can find it.
Expand Down Expand Up @@ -268,10 +268,7 @@ impl<'cfg> RegistryIndex<'cfg> {
pub fn hash(&mut self, pkg: PackageId, load: &mut dyn RegistryData) -> Poll<CargoResult<&str>> {
let req = OptVersionReq::exact(pkg.version());
let summary = self.summaries(pkg.name(), &req, load)?;
let summary = match summary {
Poll::Ready(mut summary) => summary.next(),
Poll::Pending => return Poll::Pending,
};
let summary = ready!(summary).next();
Poll::Ready(Ok(summary
.ok_or_else(|| internal(format!("no hash listed for {}", pkg)))?
.summary
Expand Down Expand Up @@ -302,10 +299,7 @@ impl<'cfg> RegistryIndex<'cfg> {
// has run previously this will parse a Cargo-specific cache file rather
// than the registry itself. In effect this is intended to be a quite
// cheap operation.
let summaries = match self.load_summaries(name, load)? {
Poll::Ready(summaries) => summaries,
Poll::Pending => return Poll::Pending,
};
let summaries = ready!(self.load_summaries(name, load)?);

// Iterate over our summaries, extract all relevant ones which match our
// version requirement, and then parse all corresponding rows in the
Expand Down Expand Up @@ -450,10 +444,7 @@ impl<'cfg> RegistryIndex<'cfg> {
) -> Poll<CargoResult<usize>> {
let source_id = self.source_id;

let summaries = match self.summaries(dep.package_name(), dep.version_req(), load)? {
Poll::Ready(summaries) => summaries,
Poll::Pending => return Poll::Pending,
};
let summaries = ready!(self.summaries(dep.package_name(), dep.version_req(), load))?;

let summaries = summaries
// First filter summaries for `--offline`. If we're online then
Expand Down Expand Up @@ -582,10 +573,7 @@ impl Summaries {
Err(e) => log::debug!("cache missing for {:?} error: {}", relative, e),
}

let response = match load.load(root, relative, index_version.as_deref())? {
Poll::Pending => return Poll::Pending,
Poll::Ready(response) => response,
};
let response = ready!(load.load(root, relative, index_version.as_deref())?);

match response {
LoadResponse::CacheValid => {
Expand Down

0 comments on commit 0b70fd4

Please sign in to comment.