Skip to content

Commit

Permalink
add a PollExt so we can use expect instead of match
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Feb 3, 2021
1 parent 33190fa commit 1d9e675
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 59 deletions.
6 changes: 2 additions & 4 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::core::{Dependency, PackageId, Source, SourceId, SourceMap, Summary};
use crate::sources::config::SourceConfigMap;
use crate::util::errors::{CargoResult, CargoResultExt};
use crate::util::interning::InternedString;
use crate::util::network::PollExt;
use crate::util::{profile, CanonicalUrl, Config};
use anyhow::bail;
use log::{debug, trace};
Expand Down Expand Up @@ -334,10 +335,7 @@ impl<'cfg> PackageRegistry<'cfg> {
if try_summaries.iter().all(|p| p.is_ready()) {
break try_summaries
.into_iter()
.map(|p| match p {
Poll::Ready(s) => s,
Poll::Pending => unreachable!("we just check for this!"),
})
.map(|p| p.expect("we just checked for this!"))
.collect::<Vec<_>>();
} else {
// TODO: dont hot loop for it to be Ready
Expand Down
52 changes: 24 additions & 28 deletions src/cargo/core/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use crate::core::PackageIdSpec;
use crate::core::{Dependency, PackageId, Registry, Summary};
use crate::util::config::Config;
use crate::util::errors::CargoResult;
use crate::util::network::PollExt;
use crate::util::profile;

use self::context::Context;
Expand All @@ -73,8 +74,6 @@ pub use self::features::{ForceAllTargets, HasDevUnits};
pub use self::resolve::{Resolve, ResolveVersion};
pub use self::types::{ResolveBehavior, ResolveOpts};

use std::task::Poll;

mod conflict_cache;
mod context;
mod dep_cache;
Expand Down Expand Up @@ -861,34 +860,31 @@ fn generalize_conflicting(
// A dep is equivalent to one of the things it can resolve to.
// Thus, if all the things it can resolve to have already ben determined
// to be conflicting, then we can just say that we conflict with the parent.
if let Some(others) = match registry
if let Some(others) = registry
.query(critical_parents_dep)
.expect("an already used dep now error!?")
{
Poll::Ready(q) => q,
Poll::Pending => unreachable!("an already used dep now pending!?"),
}
.iter()
.rev() // the last one to be tried is the least likely to be in the cache, so start with that.
.map(|other| {
past_conflicting_activations
.find(
dep,
&|id| {
if id == other.package_id() {
// we are imagining that we used other instead
Some(backtrack_critical_age)
} else {
cx.is_active(id)
}
},
Some(other.package_id()),
// we only care about things that are newer then critical_age
backtrack_critical_age,
)
.map(|con| (other.package_id(), con))
})
.collect::<Option<Vec<(PackageId, &ConflictMap)>>>()
.expect("an already used dep now pending!?")
.iter()
.rev() // the last one to be tried is the least likely to be in the cache, so start with that.
.map(|other| {
past_conflicting_activations
.find(
dep,
&|id| {
if id == other.package_id() {
// we are imagining that we used other instead
Some(backtrack_critical_age)
} else {
cx.is_active(id)
}
},
Some(other.package_id()),
// we only care about things that are newer then critical_age
backtrack_critical_age,
)
.map(|con| (other.package_id(), con))
})
.collect::<Option<Vec<(PackageId, &ConflictMap)>>>()
{
let mut con = conflicting_activations.clone();
// It is always valid to combine previously inserted conflicts.
Expand Down
46 changes: 19 additions & 27 deletions src/cargo/sources/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ use crate::util::errors::CargoResultExt;
use crate::util::hex;
use crate::util::interning::InternedString;
use crate::util::into_url::IntoUrl;
use crate::util::network::PollExt;
use crate::util::{restricted_names, CargoResult, Config, Filesystem};

const PACKAGE_SOURCE_LOCK: &str = ".cargo-ok";
Expand Down Expand Up @@ -643,13 +644,10 @@ impl<'cfg> RegistrySource<'cfg> {
// After we've loaded the package configure its summary's `checksum`
// field with the checksum we know for this `PackageId`.
let req = VersionReq::exact(package.version());
let summary_with_cksum =
match self.index.summaries(package.name(), &req, &mut *self.ops)? {
Poll::Ready(summaries) => summaries,
Poll::Pending => {
unreachable!("a downloaded dep now pending!?")
}
}
let summary_with_cksum = self
.index
.summaries(package.name(), &req, &mut *self.ops)?
.expect("a downloaded dep now pending!?")
.map(|s| s.summary.clone())
.next()
.expect("summary not found");
Expand Down Expand Up @@ -737,31 +735,25 @@ impl<'cfg> Source for RegistrySource<'cfg> {
}

fn download(&mut self, package: PackageId) -> CargoResult<MaybePackage> {
match self.index.hash(package, &mut *self.ops)? {
Poll::Ready(hash) => {
return match self.ops.download(package, hash)? {
MaybeLock::Ready(file) => self.get_pkg(package, &file).map(MaybePackage::Ready),
MaybeLock::Download { url, descriptor } => {
Ok(MaybePackage::Download { url, descriptor })
}
}
}
Poll::Pending => {
unreachable!("we got to downloading a dep while pending!?")
let hash = self
.index
.hash(package, &mut *self.ops)?
.expect("we got to downloading a dep while pending!?");
match self.ops.download(package, hash)? {
MaybeLock::Ready(file) => self.get_pkg(package, &file).map(MaybePackage::Ready),
MaybeLock::Download { url, descriptor } => {
Ok(MaybePackage::Download { url, descriptor })
}
}
}

fn finish_download(&mut self, package: PackageId, data: Vec<u8>) -> CargoResult<Package> {
match self.index.hash(package, &mut *self.ops)? {
Poll::Ready(hash) => {
let file = self.ops.finish_download(package, hash, &data)?;
return self.get_pkg(package, &file);
}
Poll::Pending => {
unreachable!("we got to downloading a dep while pending!?")
}
}
let hash = self
.index
.hash(package, &mut *self.ops)?
.expect("we got to downloading a dep while pending!?");
let file = self.ops.finish_download(package, hash, &data)?;
self.get_pkg(package, &file)
}

fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
Expand Down
15 changes: 15 additions & 0 deletions src/cargo/util/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ use anyhow::Error;

use crate::util::errors::{CargoResult, HttpNot200};
use crate::util::Config;
use std::task::Poll;

pub trait PollExt<T> {
fn expect(self, msg: &str) -> T;
}

impl<T> PollExt<T> for Poll<T> {
#[track_caller]
fn expect(self, msg: &str) -> T {
match self {
Poll::Ready(val) => val,
Poll::Pending => panic!("{}", msg),
}
}
}

pub struct Retry<'a> {
config: &'a Config,
Expand Down

0 comments on commit 1d9e675

Please sign in to comment.