Skip to content

Commit 7720662

Browse files
committed
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`.
1 parent b8ddbd2 commit 7720662

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct SourceId {
2424
inner: &'static SourceIdInner,
2525
}
2626

27-
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
27+
#[derive(Eq, Clone, Debug)]
2828
struct SourceIdInner {
2929
/// The source URL.
3030
url: Url,
@@ -237,6 +237,10 @@ impl SourceId {
237237
CRATES_IO_DOMAIN.to_string()
238238
} else if let Some(name) = &self.inner.name {
239239
name.clone()
240+
} else if self.precise().is_some() {
241+
// We remove `precise` here to retrieve an permissive version of
242+
// `SourceIdInner`, which may contain the registry name.
243+
self.with_precise(None).display_registry_name()
240244
} else {
241245
url_display(self.url())
242246
}
@@ -493,6 +497,29 @@ impl Hash for SourceId {
493497
}
494498
}
495499

500+
impl Hash for SourceIdInner {
501+
/// The hash of `SourceIdInner` is used to retrieve its interned value. We
502+
/// only care about fields that make `SourceIdInner` unique, which are:
503+
///
504+
/// - `kind`
505+
/// - `precise`
506+
/// - `canonical_url`
507+
fn hash<S: hash::Hasher>(&self, into: &mut S) {
508+
self.kind.hash(into);
509+
self.precise.hash(into);
510+
self.canonical_url.hash(into);
511+
}
512+
}
513+
514+
impl PartialEq for SourceIdInner {
515+
/// This implementation must be synced with [`SourceIdInner::hash`].
516+
fn eq(&self, other: &Self) -> bool {
517+
self.kind == other.kind
518+
&& self.precise == other.precise
519+
&& self.canonical_url == other.canonical_url
520+
}
521+
}
522+
496523
// forward to `Ord`
497524
impl PartialOrd for SourceKind {
498525
fn partial_cmp(&self, other: &SourceKind) -> Option<Ordering> {

0 commit comments

Comments
 (0)