Use ArcStr in verbatim URL (#10600)

## Summary

No need to use `String` here.
This commit is contained in:
Charlie Marsh
2025-01-14 11:12:14 -05:00
committed by GitHub
parent 279043f864
commit d8b5e7e7c0
5 changed files with 32 additions and 30 deletions
+2 -2
View File
@@ -1343,10 +1343,10 @@ mod test {
/// Ensure that we don't accidentally grow the `Dist` sizes.
#[test]
fn dist_size() {
assert!(size_of::<Dist>() <= 248, "{}", size_of::<Dist>());
assert!(size_of::<Dist>() <= 232, "{}", size_of::<Dist>());
assert!(size_of::<BuiltDist>() <= 216, "{}", size_of::<BuiltDist>());
assert!(
size_of::<SourceDist>() <= 248,
size_of::<SourceDist>() <= 232,
"{}",
size_of::<SourceDist>()
);
+9 -9
View File
@@ -29,7 +29,7 @@ pub trait UnnamedRequirementUrl: Pep508Url {
/// Set the verbatim representation of the URL.
#[must_use]
fn with_given(self, given: impl Into<String>) -> Self;
fn with_given(self, given: impl AsRef<str>) -> Self;
/// Return the original string as given by the user, if available.
fn given(&self) -> Option<&str>;
@@ -51,7 +51,7 @@ impl UnnamedRequirementUrl for VerbatimUrl {
Ok(Self::parse_url(given)?)
}
fn with_given(self, given: impl Into<String>) -> Self {
fn with_given(self, given: impl AsRef<str>) -> Self {
self.with_given(given)
}
@@ -258,7 +258,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
return Ok((url, extras));
}
@@ -269,7 +269,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
Ok((url, extras))
}
// Ex) `https://download.pytorch.org/whl/torch_stable.html`
@@ -282,7 +282,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
Ok((url, extras))
}
@@ -296,7 +296,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
return Ok((url, extras));
}
@@ -307,7 +307,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
Ok((url, extras))
}
}
@@ -321,7 +321,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
return Ok((url, extras));
}
@@ -332,7 +332,7 @@ fn preprocess_unnamed_url<Url: UnnamedRequirementUrl>(
len,
input: cursor.to_string(),
})?
.with_given(url.to_string());
.with_given(url);
Ok((url, extras))
}
}
+19 -17
View File
@@ -1,4 +1,3 @@
use regex::Regex;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::Debug;
@@ -6,6 +5,9 @@ use std::hash::Hash;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use arcstr::ArcStr;
use regex::Regex;
use thiserror::Error;
use url::{ParseError, Url};
@@ -19,7 +21,7 @@ pub struct VerbatimUrl {
/// The parsed URL.
url: Url,
/// The URL as it was provided by the user.
given: Option<String>,
given: Option<ArcStr>,
}
impl Hash for VerbatimUrl {
@@ -112,9 +114,9 @@ impl VerbatimUrl {
/// Set the verbatim representation of the URL.
#[must_use]
pub fn with_given(self, given: impl Into<String>) -> Self {
pub fn with_given(self, given: impl AsRef<str>) -> Self {
Self {
given: Some(given.into()),
given: Some(ArcStr::from(given.as_ref())),
..self
}
}
@@ -164,7 +166,7 @@ impl std::str::FromStr for VerbatimUrl {
type Err = VerbatimUrlError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::parse_url(s).map(|url| url.with_given(s.to_owned()))?)
Ok(Self::parse_url(s).map(|url| url.with_given(s))?)
}
}
@@ -232,21 +234,21 @@ impl Pep508Url for VerbatimUrl {
let path = normalize_url_path(path);
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(path.as_ref(), working_dir)?
.with_given(url.to_string()));
return Ok(
VerbatimUrl::from_path(path.as_ref(), working_dir)?.with_given(url)
);
}
Ok(VerbatimUrl::from_absolute_path(path.as_ref())?
.with_given(url.to_string()))
Ok(VerbatimUrl::from_absolute_path(path.as_ref())?.with_given(url))
}
#[cfg(not(feature = "non-pep508-extensions"))]
Ok(VerbatimUrl::parse_url(expanded)?.with_given(url.to_string()))
Ok(VerbatimUrl::parse_url(expanded)?.with_given(url))
}
// Ex) `https://download.pytorch.org/whl/torch_stable.html`
Some(_) => {
// Ex) `https://download.pytorch.org/whl/torch_stable.html`
Ok(VerbatimUrl::parse_url(expanded.as_ref())?.with_given(url.to_string()))
Ok(VerbatimUrl::parse_url(expanded.as_ref())?.with_given(url))
}
// Ex) `C:\Users\ferris\wheel-0.42.0.tar.gz`
@@ -255,11 +257,10 @@ impl Pep508Url for VerbatimUrl {
{
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
.with_given(url.to_string()));
.with_given(url));
}
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?
.with_given(url.to_string()))
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url))
}
#[cfg(not(feature = "non-pep508-extensions"))]
Err(Self::Err::NotAUrl(expanded.to_string()))
@@ -270,11 +271,12 @@ impl Pep508Url for VerbatimUrl {
#[cfg(feature = "non-pep508-extensions")]
{
if let Some(working_dir) = working_dir {
return Ok(VerbatimUrl::from_path(expanded.as_ref(), working_dir)?
.with_given(url.to_string()));
return Ok(
VerbatimUrl::from_path(expanded.as_ref(), working_dir)?.with_given(url)
);
}
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url.to_string()))
Ok(VerbatimUrl::from_absolute_path(expanded.as_ref())?.with_given(url))
}
#[cfg(not(feature = "non-pep508-extensions"))]
+1 -1
View File
@@ -133,7 +133,7 @@ impl UnnamedRequirementUrl for VerbatimParsedUrl {
})
}
fn with_given(self, given: impl Into<String>) -> Self {
fn with_given(self, given: impl AsRef<str>) -> Self {
Self {
verbatim: self.verbatim.with_given(given),
..self
+1 -1
View File
@@ -122,7 +122,7 @@ mod tests {
// If there's a conflict after the `@`, discard the original representation.
let verbatim = VerbatimUrl::parse_url("https://github.com/flask.git@main")?
.with_given("git+https://github.com/flask.git@${TAG}".to_string());
.with_given("git+https://github.com/flask.git@${TAG}");
let redirect =
Url::parse("https://github.com/flask.git@b90a4f1f4a370e92054b9cc9db0efcb864f87ebe")?;