Show registry name for SourceId from lockfile

Since current lockfile does not serialize any registry names. We here
try best effort to restore registry name from either `[registries]`
table or `[source]` replacement table. This is done by manually
implementing `Hash` and `PartialEq` for `SourceIdInner`, of which two
traits previously are simply `derive`d.

To make `SourceIdInner` generate the same hash whether contains `name`
field or not, here we remove `name` field from hashing and only concern
about `kind`, `precise` and `canonical_url`.
This commit is contained in:
Weihang Lo 2021-06-28 00:28:39 +08:00
parent b8ddbd2387
commit 7720662630
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7

View File

@ -24,7 +24,7 @@ pub struct SourceId {
inner: &'static SourceIdInner,
}
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[derive(Eq, Clone, Debug)]
struct SourceIdInner {
/// The source URL.
url: Url,
@ -237,6 +237,10 @@ impl SourceId {
CRATES_IO_DOMAIN.to_string()
} else if let Some(name) = &self.inner.name {
name.clone()
} else if self.precise().is_some() {
// We remove `precise` here to retrieve an permissive version of
// `SourceIdInner`, which may contain the registry name.
self.with_precise(None).display_registry_name()
} else {
url_display(self.url())
}
@ -493,6 +497,29 @@ impl Hash for SourceId {
}
}
impl Hash for SourceIdInner {
/// The hash of `SourceIdInner` is used to retrieve its interned value. We
/// only care about fields that make `SourceIdInner` unique, which are:
///
/// - `kind`
/// - `precise`
/// - `canonical_url`
fn hash<S: hash::Hasher>(&self, into: &mut S) {
self.kind.hash(into);
self.precise.hash(into);
self.canonical_url.hash(into);
}
}
impl PartialEq for SourceIdInner {
/// This implementation must be synced with [`SourceIdInner::hash`].
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
&& self.precise == other.precise
&& self.canonical_url == other.canonical_url
}
}
// forward to `Ord`
impl PartialOrd for SourceKind {
fn partial_cmp(&self, other: &SourceKind) -> Option<Ordering> {