From ee60a4f6ec174ff1f31b94b6d331e9d77bd12210 Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Sun, 18 May 2025 21:54:27 +0200 Subject: [PATCH 1/3] refactor: replace InternedString with Cow in IndexPackage Signed-off-by: 0xPoe --- src/cargo/sources/registry/index/mod.rs | 57 ++++++++++++++++--------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/cargo/sources/registry/index/mod.rs b/src/cargo/sources/registry/index/mod.rs index eb745142680..c47e393eaf2 100644 --- a/src/cargo/sources/registry/index/mod.rs +++ b/src/cargo/sources/registry/index/mod.rs @@ -198,7 +198,8 @@ impl IndexSummary { #[derive(Deserialize, Serialize)] pub struct IndexPackage<'a> { /// Name of the package. - pub name: InternedString, + #[serde(borrow)] + pub name: Cow<'a, str>, /// The version of this dependency. pub vers: Version, /// All kinds of direct dependencies of the package, including dev and @@ -207,14 +208,14 @@ pub struct IndexPackage<'a> { pub deps: Vec>, /// Set of features defined for the package, i.e., `[features]` table. #[serde(default)] - pub features: BTreeMap>, + pub features: BTreeMap, Vec>>, /// This field contains features with new, extended syntax. Specifically, /// namespaced features (`dep:`) and weak dependencies (`pkg?/feat`). /// /// This is separated from `features` because versions older than 1.19 /// will fail to load due to not being able to parse the new syntax, even /// with a `Cargo.lock` file. - pub features2: Option>>, + pub features2: Option, Vec>>>, /// Checksum for verifying the integrity of the corresponding downloaded package. pub cksum: String, /// If `true`, Cargo will skip this version when resolving. @@ -226,7 +227,7 @@ pub struct IndexPackage<'a> { /// /// Added early 2018 (see ), /// can be `None` if published before then. - pub links: Option, + pub links: Option>, /// Required version of rust /// /// Corresponds to `package.rust-version`. @@ -263,7 +264,11 @@ 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(self.name.into(), self.vers.clone(), source_id); + let pkgid = PackageId::new( + InternedString::new(&self.name), + self.vers.clone(), + source_id, + ); let deps = self .deps .iter() @@ -272,24 +277,31 @@ impl IndexPackage<'_> { let mut features = self.features.clone(); if let Some(features2) = &self.features2 { for (name, values) in features2 { - features.entry(*name).or_default().extend(values); + features + .entry(name.clone()) + .or_default() + .extend(values.iter().cloned()); } } - let mut summary = Summary::new( - pkgid, - deps, - &features, - self.links, - self.rust_version.clone(), - )?; + let features = features + .into_iter() + .map(|(name, values)| { + ( + InternedString::new(&name), + values.iter().map(|v| InternedString::new(&v)).collect(), + ) + }) + .collect::>(); + let links = self.links.as_ref().map(|l| InternedString::new(&l)); + let mut summary = Summary::new(pkgid, deps, &features, links, self.rust_version.clone())?; summary.set_checksum(self.cksum.clone()); Ok(summary) } } #[derive(Deserialize, Serialize)] -struct IndexPackageMinimum { - name: InternedString, +struct IndexPackageMinimum<'a> { + name: Cow<'a, str>, vers: Version, } @@ -308,13 +320,14 @@ struct IndexPackageV { pub struct RegistryDependency<'a> { /// Name of the dependency. If the dependency is renamed, the original /// would be stored in [`RegistryDependency::package`]. - pub name: InternedString, + #[serde(borrow)] + pub name: Cow<'a, str>, /// The SemVer requirement for this dependency. #[serde(borrow)] pub req: Cow<'a, str>, /// Set of features enabled for this dependency. #[serde(default)] - pub features: Vec, + pub features: Vec>, /// Whether or not this is an optional dependency. #[serde(default)] pub optional: bool, @@ -329,7 +342,7 @@ pub struct RegistryDependency<'a> { // `None` if it is from the same index. pub registry: Option>, /// The original name if the dependency is renamed. - pub package: Option, + pub package: Option>, /// Whether or not this is a public dependency. Unstable. See [RFC 1977]. /// /// [RFC 1977]: https://rust-lang.github.io/rfcs/1977-public-private-dependencies.html @@ -759,7 +772,7 @@ impl IndexSummary { Ok((index, summary)) => (index, summary, true), Err(err) => { let Ok(IndexPackageMinimum { name, vers }) = - serde_json::from_slice::(line) + serde_json::from_slice::>(line) else { // If we can't recover, prefer the original error return Err(err); @@ -833,9 +846,10 @@ impl<'a> RegistryDependency<'a> { default }; - let mut dep = Dependency::parse(package.unwrap_or(name), Some(&req), id)?; + 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(name); + dep.set_explicit_name_in_toml(InternedString::new(&name)); } let kind = match kind.as_deref().unwrap_or("") { "dev" => DepKind::Development, @@ -869,6 +883,7 @@ 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) From 42f593fa72c6eb5e4d1b660be1cca0d1858ab126 Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Mon, 26 May 2025 16:17:50 +0200 Subject: [PATCH 2/3] 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 af7ddf2d2a9..fce486601ef 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 c47e393eaf2..14fb276fcd6 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 61ecae54ea7..37a47d422e0 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 } From 5f900981de60ce4df51f3161d5505fedbf179d1d Mon Sep 17 00:00:00 2001 From: 0xPoe Date: Sat, 14 Jun 2025 22:56:17 +0200 Subject: [PATCH 3/3] refactor: replace InternedString with into() conversions across the codebase Signed-off-by: 0xPoe --- .../benchsuite/src/bin/capture-last-use.rs | 7 ++- src/bin/cargo/commands/add.rs | 3 +- src/bin/cargo/commands/rustc.rs | 3 +- src/cargo/core/compiler/build_config.rs | 2 +- src/cargo/core/dependency.rs | 2 +- src/cargo/core/package.rs | 11 +--- src/cargo/core/package_id.rs | 2 +- src/cargo/core/profiles.rs | 52 +++++++++---------- src/cargo/core/resolver/features.rs | 2 +- src/cargo/core/summary.rs | 6 +-- src/cargo/core/workspace.rs | 2 +- src/cargo/ops/cargo_output_metadata.rs | 4 +- src/cargo/ops/registry/info/view.rs | 2 +- src/cargo/sources/registry/index/mod.rs | 2 +- src/cargo/sources/registry/mod.rs | 2 +- src/cargo/sources/registry/remote.rs | 2 +- src/cargo/util/command_prelude.rs | 6 +-- src/cargo/util/interning.rs | 2 +- src/cargo/util/rustc.rs | 2 +- src/cargo/util/sqlite.rs | 2 +- src/cargo/util/toml/mod.rs | 4 +- src/cargo/util/toml_mut/manifest.rs | 7 ++- tests/testsuite/global_cache_tracker.rs | 3 +- tests/testsuite/profile_config.rs | 3 +- 24 files changed, 58 insertions(+), 75 deletions(-) diff --git a/benches/benchsuite/src/bin/capture-last-use.rs b/benches/benchsuite/src/bin/capture-last-use.rs index eb869f2ee7f..d2079cd0989 100644 --- a/benches/benchsuite/src/bin/capture-last-use.rs +++ b/benches/benchsuite/src/bin/capture-last-use.rs @@ -14,7 +14,6 @@ use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCacheTracker}; use cargo::util::cache_lock::CacheLockMode; -use cargo::util::interning::InternedString; use cargo::GlobalContext; use rand::prelude::*; use std::collections::HashMap; @@ -57,7 +56,7 @@ fn main() { let cache_dir = real_home.join("registry/cache"); for dir_ent in fs::read_dir(cache_dir).unwrap() { let registry = dir_ent.unwrap(); - let encoded_registry_name = InternedString::new(®istry.file_name().to_string_lossy()); + let encoded_registry_name = registry.file_name().to_string_lossy().into(); for krate in fs::read_dir(registry.path()).unwrap() { let krate = krate.unwrap(); let meta = krate.metadata().unwrap(); @@ -77,7 +76,7 @@ fn main() { let cache_dir = real_home.join("registry/src"); for dir_ent in fs::read_dir(cache_dir).unwrap() { let registry = dir_ent.unwrap(); - let encoded_registry_name = InternedString::new(®istry.file_name().to_string_lossy()); + let encoded_registry_name = registry.file_name().to_string_lossy().into(); for krate in fs::read_dir(registry.path()).unwrap() { let krate = krate.unwrap(); let meta = krate.metadata().unwrap(); @@ -95,7 +94,7 @@ fn main() { let git_co_dir = real_home.join("git/checkouts"); for dir_ent in fs::read_dir(git_co_dir).unwrap() { let git_source = dir_ent.unwrap(); - let encoded_git_name = InternedString::new(&git_source.file_name().to_string_lossy()); + let encoded_git_name = git_source.file_name().to_string_lossy().into(); for co in fs::read_dir(git_source.path()).unwrap() { let co = co.unwrap(); let meta = co.metadata().unwrap(); diff --git a/src/bin/cargo/commands/add.rs b/src/bin/cargo/commands/add.rs index de9f382ea01..5ccd7b15250 100644 --- a/src/bin/cargo/commands/add.rs +++ b/src/bin/cargo/commands/add.rs @@ -10,7 +10,6 @@ use cargo::ops::cargo_add::AddOptions; use cargo::ops::cargo_add::DepOp; use cargo::ops::resolve_ws; use cargo::util::command_prelude::*; -use cargo::util::interning::InternedString; use cargo::util::toml_mut::manifest::DepTable; use cargo::CargoResult; @@ -287,7 +286,7 @@ fn parse_dependencies(gctx: &GlobalContext, matches: &ArgMatches) -> CargoResult .map(String::as_str) .flat_map(parse_feature) { - let parsed_value = FeatureValue::new(InternedString::new(feature)); + let parsed_value = FeatureValue::new(feature.into()); match parsed_value { FeatureValue::Feature(_) => { if 1 < crates.len() { diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 301ad7349c5..cee871db025 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -1,6 +1,5 @@ use crate::command_prelude::*; use cargo::ops; -use cargo::util::interning::InternedString; const PRINT_ARG_NAME: &str = "print"; const CRATE_TYPE_ARG_NAME: &str = "crate-type"; @@ -77,7 +76,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { ProfileChecking::LegacyRustc, )?; if compile_opts.build_config.requested_profile == "check" { - compile_opts.build_config.requested_profile = InternedString::new("dev"); + compile_opts.build_config.requested_profile = "dev".into(); } let target_args = values(args, "args"); compile_opts.target_rustc_args = if target_args.is_empty() { diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index bec2c914bd4..387098a2e3a 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -116,7 +116,7 @@ impl BuildConfig { requested_kinds, jobs, keep_going, - requested_profile: InternedString::new("dev"), + requested_profile: "dev".into(), intent, message_format: MessageFormat::Human, force_rebuild: false, diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index 2ec2aa1a328..f3d526f1b44 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -632,7 +632,7 @@ impl ArtifactKind { _ => { return kind .strip_prefix("bin:") - .map(|bin_name| ArtifactKind::SelectedBinary(InternedString::new(bin_name))) + .map(|bin_name| ArtifactKind::SelectedBinary(bin_name.into())) .ok_or_else(|| anyhow::anyhow!("'{}' is not a valid artifact specifier", kind)) } }) diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 6a6b0c647a9..ebb10aca108 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -208,14 +208,7 @@ impl Package { let crate_features = summary .features() .iter() - .map(|(k, v)| { - ( - *k, - v.iter() - .map(|fv| InternedString::new(&fv.to_string())) - .collect(), - ) - }) + .map(|(k, v)| (*k, v.iter().map(|fv| fv.to_string().into()).collect())) .collect(); SerializedPackage { @@ -443,7 +436,7 @@ impl<'gctx> PackageSet<'gctx> { ))), downloads_finished: 0, downloaded_bytes: 0, - largest: (0, InternedString::new("")), + largest: (0, "".into()), success: false, updated_at: Cell::new(Instant::now()), timeout, diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index 37b367218f8..329442974e6 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -78,7 +78,7 @@ impl<'de> de::Deserialize<'de> for PackageId { let (field, rest) = string .split_once(' ') .ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?; - let name = InternedString::new(field); + let name = field.into(); let (field, rest) = rest .split_once(' ') diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 86d64d668d8..2a4cc47b190 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -93,7 +93,7 @@ impl Profiles { // Merge with predefined profiles. use std::collections::btree_map::Entry; for (predef_name, mut predef_prof) in Self::predefined_profiles().into_iter() { - match profiles.entry(InternedString::new(predef_name)) { + match profiles.entry(predef_name.into()) { Entry::Vacant(vac) => { vac.insert(predef_prof); } @@ -119,9 +119,9 @@ impl Profiles { /// Returns the hard-coded directory names for built-in profiles. fn predefined_dir_names() -> HashMap { [ - (InternedString::new("dev"), InternedString::new("debug")), - (InternedString::new("test"), InternedString::new("debug")), - (InternedString::new("bench"), InternedString::new("release")), + ("dev".into(), "debug".into()), + ("test".into(), "debug".into()), + ("bench".into(), "release".into()), ] .into() } @@ -134,12 +134,12 @@ impl Profiles { trim_paths_enabled: bool, ) { profile_makers.by_name.insert( - InternedString::new("dev"), + "dev".into(), ProfileMaker::new(Profile::default_dev(), profiles.get("dev").cloned()), ); profile_makers.by_name.insert( - InternedString::new("release"), + "release".into(), ProfileMaker::new( Profile::default_release(trim_paths_enabled), profiles.get("release").cloned(), @@ -185,7 +185,7 @@ impl Profiles { match &profile.dir_name { None => {} Some(dir_name) => { - self.dir_names.insert(name, InternedString::new(dir_name)); + self.dir_names.insert(name, dir_name.into()); } } @@ -230,7 +230,7 @@ impl Profiles { self.get_profile_maker(&inherits_name).unwrap().clone() } Some(inherits_name) => { - let inherits_name = InternedString::new(&inherits_name); + let inherits_name = inherits_name.into(); if !set.insert(inherits_name) { bail!( "profile inheritance loop detected with profile `{}` inheriting `{}`", @@ -297,7 +297,7 @@ impl Profiles { CompileKind::Target(target) => target.short_name(), }; if target.contains("-apple-") { - profile.split_debuginfo = Some(InternedString::new("unpacked")); + profile.split_debuginfo = Some("unpacked".into()); } } @@ -455,7 +455,7 @@ impl ProfileMaker { // basically turning down the optimization level and avoid limiting // codegen units. This ensures that we spend little time optimizing as // well as enabling parallelism by not constraining codegen units. - profile.opt_level = InternedString::new("0"); + profile.opt_level = "0".into(); profile.codegen_units = None; // For build dependencies, we usually don't need debuginfo, and @@ -531,12 +531,12 @@ fn merge_toml_overrides( /// Does not merge overrides (see `merge_toml_overrides`). fn merge_profile(profile: &mut Profile, toml: &TomlProfile) { if let Some(ref opt_level) = toml.opt_level { - profile.opt_level = InternedString::new(&opt_level.0); + profile.opt_level = opt_level.0.as_str().into(); } match toml.lto { Some(StringOrBool::Bool(b)) => profile.lto = Lto::Bool(b), Some(StringOrBool::String(ref n)) if is_off(n.as_str()) => profile.lto = Lto::Off, - Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(InternedString::new(n)), + Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(n.into()), None => {} } if toml.codegen_backend.is_some() { @@ -552,7 +552,7 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) { profile.debug_assertions = debug_assertions; } if let Some(split_debuginfo) = &toml.split_debuginfo { - profile.split_debuginfo = Some(InternedString::new(split_debuginfo)); + profile.split_debuginfo = Some(split_debuginfo.into()); } if let Some(rpath) = toml.rpath { profile.rpath = rpath; @@ -578,16 +578,12 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) { profile.trim_paths = Some(trim_paths.clone()); } profile.strip = match toml.strip { - Some(StringOrBool::Bool(true)) => { - Strip::Resolved(StripInner::Named(InternedString::new("symbols"))) - } + Some(StringOrBool::Bool(true)) => Strip::Resolved(StripInner::Named("symbols".into())), Some(StringOrBool::Bool(false)) => Strip::Resolved(StripInner::None), Some(StringOrBool::String(ref n)) if n.as_str() == "none" => { Strip::Resolved(StripInner::None) } - Some(StringOrBool::String(ref n)) => { - Strip::Resolved(StripInner::Named(InternedString::new(n))) - } + Some(StringOrBool::String(ref n)) => Strip::Resolved(StripInner::Named(n.into())), None => Strip::Deferred(StripInner::None), }; } @@ -635,8 +631,8 @@ pub struct Profile { impl Default for Profile { fn default() -> Profile { Profile { - name: InternedString::new(""), - opt_level: InternedString::new("0"), + name: "".into(), + opt_level: "0".into(), root: ProfileRoot::Debug, lto: Lto::Bool(false), codegen_backend: None, @@ -710,7 +706,7 @@ impl Profile { /// Returns a built-in `dev` profile. fn default_dev() -> Profile { Profile { - name: InternedString::new("dev"), + name: "dev".into(), root: ProfileRoot::Debug, debuginfo: DebugInfo::Resolved(TomlDebugInfo::Full), debug_assertions: true, @@ -724,9 +720,9 @@ impl Profile { fn default_release(trim_paths_enabled: bool) -> Profile { let trim_paths = trim_paths_enabled.then(|| TomlTrimPathsValue::Object.into()); Profile { - name: InternedString::new("release"), + name: "release".into(), root: ProfileRoot::Release, - opt_level: InternedString::new("3"), + opt_level: "3".into(), trim_paths, ..Profile::default() } @@ -1274,13 +1270,13 @@ fn merge_config_profiles( profile.merge(&config_profile); } if let Some(inherits) = &profile.inherits { - check_to_add.insert(InternedString::new(inherits)); + check_to_add.insert(inherits.into()); } } // Add the built-in profiles. This is important for things like `cargo // test` which implicitly use the "dev" profile for dependencies. - for name in &["dev", "release", "test", "bench"] { - check_to_add.insert(InternedString::new(name)); + for name in ["dev", "release", "test", "bench"] { + check_to_add.insert(name.into()); } // Add config-only profiles. // Need to iterate repeatedly to get all the inherits values. @@ -1291,7 +1287,7 @@ fn merge_config_profiles( if !profiles.contains_key(name.as_str()) { if let Some(config_profile) = get_config_profile(ws, &name)? { if let Some(inherits) = &config_profile.inherits { - check_to_add.insert(InternedString::new(inherits)); + check_to_add.insert(inherits.into()); } profiles.insert(name, config_profile); } diff --git a/src/cargo/core/resolver/features.rs b/src/cargo/core/resolver/features.rs index 33e24db3a99..828138a9ad7 100644 --- a/src/cargo/core/resolver/features.rs +++ b/src/cargo/core/resolver/features.rs @@ -306,7 +306,7 @@ impl CliFeatures { .flat_map(|s| s.split_whitespace()) .flat_map(|s| s.split(',')) .filter(|s| !s.is_empty()) - .map(InternedString::new) + .map(|s| s.into()) .map(FeatureValue::new) .collect() } diff --git a/src/cargo/core/summary.rs b/src/cargo/core/summary.rs index d12a6f49bbb..590da98ed3c 100644 --- a/src/cargo/core/summary.rs +++ b/src/cargo/core/summary.rs @@ -377,15 +377,15 @@ impl FeatureValue { Some((dep, dep_feat)) => { let dep_name = dep.strip_suffix('?'); FeatureValue::DepFeature { - dep_name: InternedString::new(dep_name.unwrap_or(dep)), - dep_feature: InternedString::new(dep_feat), + dep_name: dep_name.unwrap_or(dep).into(), + dep_feature: dep_feat.into(), weak: dep_name.is_some(), } } None => { if let Some(dep_name) = feature.strip_prefix("dep:") { FeatureValue::Dep { - dep_name: InternedString::new(dep_name), + dep_name: dep_name.into(), } } else { FeatureValue::Feature(feature) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 57e50ebefee..cfb93a00a4d 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1549,7 +1549,7 @@ impl<'gctx> Workspace<'gctx> { .flatten() .unique() .filter(|element| { - let feature = FeatureValue::new(InternedString::new(element)); + let feature = FeatureValue::new(element.into()); !cli_features.features.contains(&feature) && !found_features.contains(&feature) }) .sorted() diff --git a/src/cargo/ops/cargo_output_metadata.rs b/src/cargo/ops/cargo_output_metadata.rs index 43ee9a8adc9..9f4ae4a6327 100644 --- a/src/cargo/ops/cargo_output_metadata.rs +++ b/src/cargo/ops/cargo_output_metadata.rs @@ -302,7 +302,7 @@ fn build_resolve_graph_r( // Given that Cargo doesn't know which target it should resolve to, // when an artifact dep is specified with { target = "target" }, // keep it with a special "" string, - .or_else(|| Some(InternedString::new(""))), + .or_else(|| Some("".into())), None => None, }; @@ -334,7 +334,7 @@ fn build_resolve_graph_r( }, // No lib target exists but contains artifact deps. (None, 1..) => Dep { - name: InternedString::new(""), + name: "".into(), pkg: pkg_id.to_spec(), pkg_id, dep_kinds, diff --git a/src/cargo/ops/registry/info/view.rs b/src/cargo/ops/registry/info/view.rs index e7fd4aeeb43..c847e86c4f0 100644 --- a/src/cargo/ops/registry/info/view.rs +++ b/src/cargo/ops/registry/info/view.rs @@ -135,7 +135,7 @@ pub(super) fn pretty_view( )?; } - let activated = &[InternedString::new("default")]; + let activated = &["default".into()]; let resolved_features = resolve_features(activated, summary.features()); pretty_features( resolved_features.clone(), diff --git a/src/cargo/sources/registry/index/mod.rs b/src/cargo/sources/registry/index/mod.rs index 14fb276fcd6..bde1c8eb1db 100644 --- a/src/cargo/sources/registry/index/mod.rs +++ b/src/cargo/sources/registry/index/mod.rs @@ -683,7 +683,7 @@ impl Summaries { /// represents information previously cached by Cargo. pub fn parse_cache(contents: Vec) -> CargoResult<(Summaries, InternedString)> { let cache = SummariesCache::parse(&contents)?; - let index_version = InternedString::new(cache.index_version); + let index_version = cache.index_version.into(); let mut ret = Summaries::default(); for (version, summary) in cache.versions { let (start, end) = subslice_bounds(&contents, summary); diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index e68a460c388..f622ab462fd 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -844,7 +844,7 @@ impl<'gctx> Source for RegistrySource<'gctx> { dep.package_name().replace('-', "_"), dep.package_name().replace('_', "-"), ] { - let name_permutation = InternedString::new(&name_permutation); + let name_permutation = name_permutation.into(); if name_permutation == dep.package_name() { continue; } diff --git a/src/cargo/sources/registry/remote.rs b/src/cargo/sources/registry/remote.rs index 52563a2efa1..6398689e435 100644 --- a/src/cargo/sources/registry/remote.rs +++ b/src/cargo/sources/registry/remote.rs @@ -198,7 +198,7 @@ impl<'gctx> RemoteRegistry<'gctx> { if let Some(sha) = self.current_sha.get() { return Some(sha); } - let sha = InternedString::new(&self.head().ok()?.to_string()); + let sha = self.head().ok()?.to_string().into(); self.current_sha.set(Some(sha)); Some(sha) } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 8e375f7e1ea..2350c978eab 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -681,7 +681,7 @@ Run `{cmd}` to see possible targets." (Some(name @ ("dev" | "test" | "bench" | "check")), ProfileChecking::LegacyRustc) // `cargo fix` and `cargo check` has legacy handling of this profile name | (Some(name @ "test"), ProfileChecking::LegacyTestOnly) => { - return Ok(InternedString::new(name)); + return Ok(name.into()); } _ => {} } @@ -708,7 +708,7 @@ Run `{cmd}` to see possible targets." } }; - Ok(InternedString::new(name)) + Ok(name.into()) } fn packages_from_flags(&self) -> CargoResult { @@ -1133,7 +1133,7 @@ fn get_profile_candidates() -> Vec { fn get_workspace_profile_candidates() -> CargoResult> { let gctx = new_gctx_for_completions()?; let ws = Workspace::new(&find_root_manifest_for_wd(gctx.cwd())?, &gctx)?; - let profiles = Profiles::new(&ws, InternedString::new("dev"))?; + let profiles = Profiles::new(&ws, "dev".into())?; let mut candidates = Vec::new(); for name in profiles.profile_names() { diff --git a/src/cargo/util/interning.rs b/src/cargo/util/interning.rs index 37a47d422e0..a08738439ef 100644 --- a/src/cargo/util/interning.rs +++ b/src/cargo/util/interning.rs @@ -176,7 +176,7 @@ impl<'de> serde::Deserialize<'de> for InternedString { { UntaggedEnumVisitor::new() .expecting("an String like thing") - .string(|value| Ok(InternedString::new(value))) + .string(|value| Ok(value.into())) .deserialize(deserializer) } } diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs index 22dcc5b060d..003318aa885 100644 --- a/src/cargo/util/rustc.rs +++ b/src/cargo/util/rustc.rs @@ -79,7 +79,7 @@ impl Rustc { }) }; - let host = InternedString::new(extract("host: ")?); + let host = extract("host: ")?.into(); let version = semver::Version::parse(extract("release: ")?).with_context(|| { format!( "rustc version does not appear to be a valid semver version, from:\n{}", diff --git a/src/cargo/util/sqlite.rs b/src/cargo/util/sqlite.rs index b391cc6dbb2..8e8e302a260 100644 --- a/src/cargo/util/sqlite.rs +++ b/src/cargo/util/sqlite.rs @@ -7,7 +7,7 @@ use rusqlite::{Connection, TransactionBehavior}; impl FromSql for InternedString { fn column_result(value: rusqlite::types::ValueRef<'_>) -> Result { - value.as_str().map(InternedString::new) + value.as_str().map(InternedString::from) } } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 88ddb4adebd..83d10678368 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1697,7 +1697,7 @@ pub fn to_real_manifest( .iter() .map(|(k, v)| { ( - InternedString::new(k), + k.to_string().into(), v.iter().map(InternedString::from).collect(), ) }) @@ -3090,7 +3090,7 @@ fn prepare_toml_for_publish( features.values_mut().for_each(|feature_deps| { feature_deps.retain(|feature_dep| { - let feature_value = FeatureValue::new(InternedString::new(feature_dep)); + let feature_value = FeatureValue::new(feature_dep.into()); match feature_value { FeatureValue::Dep { dep_name } | FeatureValue::DepFeature { dep_name, .. } => { let k = &manifest::PackageName::new(dep_name.to_string()).unwrap(); diff --git a/src/cargo/util/toml_mut/manifest.rs b/src/cargo/util/toml_mut/manifest.rs index 9f7a26d0732..eda0b419d89 100644 --- a/src/cargo/util/toml_mut/manifest.rs +++ b/src/cargo/util/toml_mut/manifest.rs @@ -10,7 +10,6 @@ use super::dependency::Dependency; use crate::core::dependency::DepKind; use crate::core::{FeatureValue, Features, Workspace}; use crate::util::closest; -use crate::util::interning::InternedString; use crate::util::toml::{is_embedded, ScriptSource}; use crate::{CargoResult, GlobalContext}; @@ -533,7 +532,7 @@ impl LocalManifest { .filter_map(|v| v.as_array()) { for value in values.iter().filter_map(|v| v.as_str()) { - let value = FeatureValue::new(InternedString::new(value)); + let value = FeatureValue::new(value.into()); if let FeatureValue::Dep { dep_name } = &value { if dep_name.as_str() == dep_key { return true; @@ -637,7 +636,7 @@ fn fix_feature_activations( .enumerate() .filter_map(|(idx, value)| value.as_str().map(|s| (idx, s))) .filter_map(|(idx, value)| { - let parsed_value = FeatureValue::new(InternedString::new(value)); + let parsed_value = FeatureValue::new(value.into()); match status { DependencyStatus::None => match (parsed_value, explicit_dep_activation) { (FeatureValue::Feature(dep_name), false) @@ -666,7 +665,7 @@ fn fix_feature_activations( if status == DependencyStatus::Required { for value in feature_values.iter_mut() { let parsed_value = if let Some(value) = value.as_str() { - FeatureValue::new(InternedString::new(value)) + FeatureValue::new(value.into()) } else { continue; }; diff --git a/tests/testsuite/global_cache_tracker.rs b/tests/testsuite/global_cache_tracker.rs index 1cc8d263c77..66ebdcaa2ab 100644 --- a/tests/testsuite/global_cache_tracker.rs +++ b/tests/testsuite/global_cache_tracker.rs @@ -16,7 +16,6 @@ use std::time::{Duration, SystemTime}; use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCacheTracker}; use cargo::util::cache_lock::CacheLockMode; -use cargo::util::interning::InternedString; use cargo::GlobalContext; use cargo_test_support::compare::assert_e2e; use cargo_test_support::paths; @@ -137,7 +136,7 @@ fn populate_cache( .join(".cargo/registry/index/example.com-a6c4a5adcb232b9a") .mkdir_p(); let mut create = |name: &str, age, crate_size: u64, src_size: u64| { - let crate_filename = InternedString::new(&format!("{name}.crate")); + let crate_filename = format!("{name}.crate").into(); deferred.mark_registry_crate_used_stamp( global_cache_tracker::RegistryCrate { encoded_registry_name: "example.com-a6c4a5adcb232b9a".into(), diff --git a/tests/testsuite/profile_config.rs b/tests/testsuite/profile_config.rs index e3afa09f017..d51c0ce6995 100644 --- a/tests/testsuite/profile_config.rs +++ b/tests/testsuite/profile_config.rs @@ -375,7 +375,6 @@ fn named_config_profile() { use cargo::core::compiler::CompileKind; use cargo::core::profiles::{Profiles, UnitFor}; use cargo::core::{PackageId, Workspace}; - use cargo::util::interning::InternedString; use std::fs; paths::root().join(".cargo").mkdir_p(); fs::write( @@ -421,7 +420,7 @@ fn named_config_profile() { ) .unwrap(); let gctx = GlobalContextBuilder::new().build(); - let profile_name = InternedString::new("foo"); + let profile_name = "foo".into(); let ws = Workspace::new(&paths::root().join("Cargo.toml"), &gctx).unwrap(); let profiles = Profiles::new(&ws, profile_name).unwrap();