Skip to content

Commit c5b799e

Browse files
committed
refactor: replace InternedString with Cow in IndexPackage
Signed-off-by: 0xPoe <techregister@pm.me>
1 parent c2c636a commit c5b799e

File tree

1 file changed

+42
-21
lines changed
  • src/cargo/sources/registry/index

1 file changed

+42
-21
lines changed

src/cargo/sources/registry/index/mod.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ impl IndexSummary {
198198
#[derive(Deserialize, Serialize)]
199199
pub struct IndexPackage<'a> {
200200
/// Name of the package.
201-
pub name: InternedString,
201+
#[serde(borrow)]
202+
pub name: Cow<'a, str>,
202203
/// The version of this dependency.
203204
pub vers: Version,
204205
/// All kinds of direct dependencies of the package, including dev and
@@ -207,14 +208,14 @@ pub struct IndexPackage<'a> {
207208
pub deps: Vec<RegistryDependency<'a>>,
208209
/// Set of features defined for the package, i.e., `[features]` table.
209210
#[serde(default)]
210-
pub features: BTreeMap<InternedString, Vec<InternedString>>,
211+
pub features: BTreeMap<Cow<'a, str>, Vec<Cow<'a, str>>>,
211212
/// This field contains features with new, extended syntax. Specifically,
212213
/// namespaced features (`dep:`) and weak dependencies (`pkg?/feat`).
213214
///
214215
/// This is separated from `features` because versions older than 1.19
215216
/// will fail to load due to not being able to parse the new syntax, even
216217
/// with a `Cargo.lock` file.
217-
pub features2: Option<BTreeMap<InternedString, Vec<InternedString>>>,
218+
pub features2: Option<BTreeMap<Cow<'a, str>, Vec<Cow<'a, str>>>>,
218219
/// Checksum for verifying the integrity of the corresponding downloaded package.
219220
pub cksum: String,
220221
/// If `true`, Cargo will skip this version when resolving.
@@ -226,7 +227,7 @@ pub struct IndexPackage<'a> {
226227
///
227228
/// Added early 2018 (see <https://github.com/rust-lang/cargo/pull/4978>),
228229
/// can be `None` if published before then.
229-
pub links: Option<InternedString>,
230+
pub links: Option<Cow<'a, str>>,
230231
/// Required version of rust
231232
///
232233
/// Corresponds to `package.rust-version`.
@@ -263,7 +264,11 @@ impl IndexPackage<'_> {
263264
fn to_summary(&self, source_id: SourceId) -> CargoResult<Summary> {
264265
// ****CAUTION**** Please be extremely careful with returning errors, see
265266
// `IndexSummary::parse` for details
266-
let pkgid = PackageId::new(self.name.into(), self.vers.clone(), source_id);
267+
let pkgid = PackageId::new(
268+
InternedString::new(&self.name),
269+
self.vers.clone(),
270+
source_id,
271+
);
267272
let deps = self
268273
.deps
269274
.iter()
@@ -272,24 +277,31 @@ impl IndexPackage<'_> {
272277
let mut features = self.features.clone();
273278
if let Some(features2) = &self.features2 {
274279
for (name, values) in features2 {
275-
features.entry(*name).or_default().extend(values);
280+
features
281+
.entry(name.clone())
282+
.or_default()
283+
.extend(values.iter().cloned());
276284
}
277285
}
278-
let mut summary = Summary::new(
279-
pkgid,
280-
deps,
281-
&features,
282-
self.links,
283-
self.rust_version.clone(),
284-
)?;
286+
let features = features
287+
.into_iter()
288+
.map(|(name, values)| {
289+
(
290+
InternedString::new(&name),
291+
values.iter().map(|v| InternedString::new(&v)).collect(),
292+
)
293+
})
294+
.collect::<BTreeMap<_, _>>();
295+
let links = self.links.as_ref().map(|l| InternedString::new(&l));
296+
let mut summary = Summary::new(pkgid, deps, &features, links, self.rust_version.clone())?;
285297
summary.set_checksum(self.cksum.clone());
286298
Ok(summary)
287299
}
288300
}
289301

290302
#[derive(Deserialize, Serialize)]
291-
struct IndexPackageMinimum {
292-
name: InternedString,
303+
struct IndexPackageMinimum<'a> {
304+
name: Cow<'a, str>,
293305
vers: Version,
294306
}
295307

@@ -308,13 +320,14 @@ struct IndexPackageV {
308320
pub struct RegistryDependency<'a> {
309321
/// Name of the dependency. If the dependency is renamed, the original
310322
/// would be stored in [`RegistryDependency::package`].
311-
pub name: InternedString,
323+
#[serde(borrow)]
324+
pub name: Cow<'a, str>,
312325
/// The SemVer requirement for this dependency.
313326
#[serde(borrow)]
314327
pub req: Cow<'a, str>,
315328
/// Set of features enabled for this dependency.
316329
#[serde(default)]
317-
pub features: Vec<InternedString>,
330+
pub features: Vec<Cow<'a, str>>,
318331
/// Whether or not this is an optional dependency.
319332
#[serde(default)]
320333
pub optional: bool,
@@ -329,7 +342,7 @@ pub struct RegistryDependency<'a> {
329342
// `None` if it is from the same index.
330343
pub registry: Option<Cow<'a, str>>,
331344
/// The original name if the dependency is renamed.
332-
pub package: Option<InternedString>,
345+
pub package: Option<Cow<'a, str>>,
333346
/// Whether or not this is a public dependency. Unstable. See [RFC 1977].
334347
///
335348
/// [RFC 1977]: https://rust-lang.github.io/rfcs/1977-public-private-dependencies.html
@@ -759,7 +772,7 @@ impl IndexSummary {
759772
Ok((index, summary)) => (index, summary, true),
760773
Err(err) => {
761774
let Ok(IndexPackageMinimum { name, vers }) =
762-
serde_json::from_slice::<IndexPackageMinimum>(line)
775+
serde_json::from_slice::<IndexPackageMinimum<'_>>(line)
763776
else {
764777
// If we can't recover, prefer the original error
765778
return Err(err);
@@ -833,9 +846,13 @@ impl<'a> RegistryDependency<'a> {
833846
default
834847
};
835848

836-
let mut dep = Dependency::parse(package.unwrap_or(name), Some(&req), id)?;
849+
let interned_name = package
850+
.as_ref()
851+
.map(|p| InternedString::new(&p))
852+
.unwrap_or(InternedString::new(&name));
853+
let mut dep = Dependency::parse(interned_name, Some(&req), id)?;
837854
if package.is_some() {
838-
dep.set_explicit_name_in_toml(name);
855+
dep.set_explicit_name_in_toml(InternedString::new(&name));
839856
}
840857
let kind = match kind.as_deref().unwrap_or("") {
841858
"dev" => DepKind::Development,
@@ -869,6 +886,10 @@ impl<'a> RegistryDependency<'a> {
869886
dep.set_artifact(artifact);
870887
}
871888

889+
let features = features
890+
.iter()
891+
.map(|f| InternedString::new(&f))
892+
.collect::<Vec<_>>();
872893
dep.set_optional(optional)
873894
.set_default_features(default_features)
874895
.set_features(features)

0 commit comments

Comments
 (0)