mirror of
https://github.com/astral-sh/uv.git
synced 2025-12-11 12:29:43 +00:00
Avoid some additional clones for PackageName (#896)
This commit is contained in:
parent
aee6aed684
commit
d3f65c317d
2 changed files with 22 additions and 3 deletions
|
|
@ -35,7 +35,7 @@ pub trait DistributionMetadata: Name {
|
||||||
VersionOrUrl::Version(version) => {
|
VersionOrUrl::Version(version) => {
|
||||||
// https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-dist-info-directory
|
// https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-dist-info-directory
|
||||||
// `version` is normalized by its `ToString` impl
|
// `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)),
|
VersionOrUrl::Url(url) => cache_key::digest(&cache_key::CanonicalUrl::new(url)),
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use serde::{Deserialize, Deserializer, Serialize};
|
use serde::{Deserialize, Deserializer, Serialize};
|
||||||
|
|
@ -22,8 +23,26 @@ impl PackageName {
|
||||||
/// Escape this name with underscores (`_`) instead of dashes (`-`)
|
/// Escape this name with underscores (`_`) instead of dashes (`-`)
|
||||||
///
|
///
|
||||||
/// See: <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#recording-installed-packages>
|
/// See: <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#recording-installed-packages>
|
||||||
pub fn as_dist_info_name(&self) -> String {
|
pub fn as_dist_info_name(&self) -> Cow<'_, str> {
|
||||||
self.0.replace('-', "_")
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue