Call cmp_precedence where applicable

This commit is contained in:
David Tolnay 2023-10-10 20:45:25 -07:00
parent c0ed70ef5e
commit f1d2237b72
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 28 additions and 23 deletions

View file

@ -29,8 +29,7 @@ struct PackageIdInner {
source_id: SourceId,
}
// Custom equality that uses full equality of SourceId, rather than its custom equality,
// and Version, which usually ignores `build` metadata.
// Custom equality that uses full equality of SourceId, rather than its custom equality.
//
// The `build` part of the version is usually ignored (like a "comment").
// However, there are some cases where it is important. The download path from
@ -40,11 +39,7 @@ struct PackageIdInner {
impl PartialEq for PackageIdInner {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.version.major == other.version.major
&& self.version.minor == other.version.minor
&& self.version.patch == other.version.patch
&& self.version.pre == other.version.pre
&& self.version.build == other.version.build
&& self.version == other.version
&& self.source_id.full_eq(other.source_id)
}
}
@ -53,11 +48,7 @@ impl PartialEq for PackageIdInner {
impl Hash for PackageIdInner {
fn hash<S: hash::Hasher>(&self, into: &mut S) {
self.name.hash(into);
self.version.major.hash(into);
self.version.minor.hash(into);
self.version.patch.hash(into);
self.version.pre.hash(into);
self.version.build.hash(into);
self.version.hash(into);
self.source_id.full_hash(into);
}
}
@ -233,6 +224,16 @@ impl fmt::Debug for PackageId {
}
}
impl fmt::Debug for PackageIdInner {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("PackageIdInner")
.field("name", &self.name)
.field("version", &self.version.to_string())
.field("source", &self.source_id.to_string())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::PackageId;
@ -257,4 +258,14 @@ mod tests {
let pkg_id = PackageId::new("foo", "1.0.0", SourceId::for_registry(&loc).unwrap()).unwrap();
assert_eq!("foo v1.0.0", pkg_id.to_string());
}
#[test]
fn unequal_build_metadata() {
let loc = CRATES_IO_INDEX.into_url().unwrap();
let repo = SourceId::for_registry(&loc).unwrap();
let first = PackageId::new("foo", "0.0.1+first", repo).unwrap();
let second = PackageId::new("foo", "0.0.1+second", repo).unwrap();
assert_ne!(first, second);
assert_ne!(first.inner, second.inner);
}
}

View file

@ -98,6 +98,7 @@ use cargo_util::{paths, registry::make_dep_path};
use semver::Version;
use serde::Deserialize;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::{HashMap, HashSet};
use std::fs;
@ -674,11 +675,8 @@ impl<'cfg> RegistryIndex<'cfg> {
(true, true) => s_vers == requested,
(true, false) => false,
(false, true) => {
// Strip out the metadata.
s_vers.major == requested.major
&& s_vers.minor == requested.minor
&& s_vers.patch == requested.patch
&& s_vers.pre == requested.pre
// Compare disregarding the metadata.
s_vers.cmp_precedence(requested) == Ordering::Equal
}
(false, false) => s_vers == requested,
}

View file

@ -1,5 +1,6 @@
use semver::{Comparator, Op, Version, VersionReq};
use serde_untagged::UntaggedEnumVisitor;
use std::cmp::Ordering;
use std::fmt::{self, Display};
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
@ -83,12 +84,7 @@ impl OptVersionReq {
match self {
OptVersionReq::Any => true,
OptVersionReq::Req(req) => req.matches(version),
OptVersionReq::Locked(v, _) => {
v.major == version.major
&& v.minor == version.minor
&& v.patch == version.patch
&& v.pre == version.pre
}
OptVersionReq::Locked(v, _) => v.cmp_precedence(version) == Ordering::Equal,
}
}
}