From 42f593fa72c6eb5e4d1b660be1cca0d1858ab126 Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Mon, 26 May 2025 16:17:50 +0200 Subject: [PATCH] refactor: implement From Cow trait for the InternedString Signed-off-by: 0xPoe --- src/cargo/core/compiler/fingerprint/mod.rs | 2 +- src/cargo/sources/registry/index/mod.rs | 25 ++++++---------------- src/cargo/util/interning.rs | 19 ++++++++-------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/cargo/core/compiler/fingerprint/mod.rs b/src/cargo/core/compiler/fingerprint/mod.rs index af7ddf2d2..fce486601 100644 --- a/src/cargo/core/compiler/fingerprint/mod.rs +++ b/src/cargo/core/compiler/fingerprint/mod.rs @@ -694,7 +694,7 @@ impl<'de> Deserialize<'de> for DepFingerprint { let (pkg_id, name, public, hash) = <(u64, String, bool, u64)>::deserialize(d)?; Ok(DepFingerprint { pkg_id, - name: InternedString::new(&name), + name: name.into(), public, fingerprint: Arc::new(Fingerprint { memoized_hash: Mutex::new(Some(hash)), diff --git a/src/cargo/sources/registry/index/mod.rs b/src/cargo/sources/registry/index/mod.rs index c47e393ea..14fb276fc 100644 --- a/src/cargo/sources/registry/index/mod.rs +++ b/src/cargo/sources/registry/index/mod.rs @@ -264,35 +264,23 @@ impl IndexPackage<'_> { fn to_summary(&self, source_id: SourceId) -> CargoResult { // ****CAUTION**** Please be extremely careful with returning errors, see // `IndexSummary::parse` for details - let pkgid = PackageId::new( - InternedString::new(&self.name), - self.vers.clone(), - source_id, - ); + let pkgid = PackageId::new(self.name.as_ref().into(), self.vers.clone(), source_id); let deps = self .deps .iter() .map(|dep| dep.clone().into_dep(source_id)) .collect::>>()?; let mut features = self.features.clone(); - if let Some(features2) = &self.features2 { + if let Some(features2) = self.features2.clone() { for (name, values) in features2 { - features - .entry(name.clone()) - .or_default() - .extend(values.iter().cloned()); + features.entry(name).or_default().extend(values); } } let features = features .into_iter() - .map(|(name, values)| { - ( - InternedString::new(&name), - values.iter().map(|v| InternedString::new(&v)).collect(), - ) - }) + .map(|(name, values)| (name.into(), values.into_iter().map(|v| v.into()).collect())) .collect::>(); - let links = self.links.as_ref().map(|l| InternedString::new(&l)); + let links: Option = self.links.as_ref().map(|l| l.as_ref().into()); let mut summary = Summary::new(pkgid, deps, &features, links, self.rust_version.clone())?; summary.set_checksum(self.cksum.clone()); Ok(summary) @@ -849,7 +837,7 @@ impl<'a> RegistryDependency<'a> { let interned_name = InternedString::new(package.as_ref().unwrap_or(&name)); let mut dep = Dependency::parse(interned_name, Some(&req), id)?; if package.is_some() { - dep.set_explicit_name_in_toml(InternedString::new(&name)); + dep.set_explicit_name_in_toml(name); } let kind = match kind.as_deref().unwrap_or("") { "dev" => DepKind::Development, @@ -883,7 +871,6 @@ impl<'a> RegistryDependency<'a> { dep.set_artifact(artifact); } - let features = features.iter().map(|f| InternedString::new(&f)); dep.set_optional(optional) .set_default_features(default_features) .set_features(features) diff --git a/src/cargo/util/interning.rs b/src/cargo/util/interning.rs index 61ecae54e..37a47d422 100644 --- a/src/cargo/util/interning.rs +++ b/src/cargo/util/interning.rs @@ -47,7 +47,7 @@ impl<'a> From<&'a String> for InternedString { impl From for InternedString { fn from(item: String) -> Self { - InternedString::from_cow(item.into()) + InternedString::from(Cow::Owned(item)) } } @@ -69,14 +69,8 @@ impl<'a> PartialEq<&'a str> for InternedString { } } -impl Eq for InternedString {} - -impl InternedString { - pub fn new(s: &str) -> InternedString { - InternedString::from_cow(s.into()) - } - - fn from_cow<'a>(cs: Cow<'a, str>) -> InternedString { +impl<'a> From> for InternedString { + fn from(cs: Cow<'a, str>) -> Self { let mut cache = interned_storage(); let s = cache.get(cs.as_ref()).copied().unwrap_or_else(|| { let s = cs.into_owned().leak(); @@ -86,7 +80,14 @@ impl InternedString { InternedString { inner: s } } +} +impl Eq for InternedString {} + +impl InternedString { + pub fn new(s: &str) -> InternedString { + InternedString::from(Cow::Borrowed(s)) + } pub fn as_str(&self) -> &'static str { self.inner }