Avoid some additional clones for PackageName (#896)

This commit is contained in:
Charlie Marsh 2024-01-12 12:54:40 -05:00 committed by GitHub
parent aee6aed684
commit d3f65c317d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View file

@ -35,7 +35,7 @@ pub trait DistributionMetadata: Name {
VersionOrUrl::Version(version) => {
// https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-dist-info-directory
// `version` is normalized by its `ToString` impl
format!("{}-{}", self.name().as_dist_info_name(), version)
format!("{}-{}", self.name(), version)
}
VersionOrUrl::Url(url) => cache_key::digest(&cache_key::CanonicalUrl::new(url)),
})

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize};
@ -22,8 +23,26 @@ impl PackageName {
/// Escape this name with underscores (`_`) instead of dashes (`-`)
///
/// See: <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#recording-installed-packages>
pub fn as_dist_info_name(&self) -> String {
self.0.replace('-', "_")
pub fn as_dist_info_name(&self) -> Cow<'_, str> {
if let Some(dash_position) = self.0.find('-') {
// Initialize `replaced` with the start of the string up to the current character.
let mut owned_string = String::with_capacity(self.0.len());
owned_string.push_str(&self.0[..dash_position]);
owned_string.push('_');
// Iterate over the rest of the string.
owned_string.extend(self.0[dash_position + 1..].chars().map(|character| {
if character == '-' {
'_'
} else {
character
}
}));
Cow::Owned(owned_string)
} else {
Cow::Borrowed(self.0.as_str())
}
}
}