From d8b5e7e7c016d72a6b169a77400babbf2c01c898 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 14 Jan 2025 11:12:14 -0500 Subject: [PATCH] Use `ArcStr` in verbatim URL (#10600) ## Summary No need to use `String` here. --- crates/uv-distribution-types/src/lib.rs | 4 +-- crates/uv-pep508/src/unnamed.rs | 18 ++++++------- crates/uv-pep508/src/verbatim_url.rs | 36 +++++++++++++------------ crates/uv-pypi-types/src/parsed_url.rs | 2 +- crates/uv-resolver/src/redirect.rs | 2 +- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/crates/uv-distribution-types/src/lib.rs b/crates/uv-distribution-types/src/lib.rs index 0ce995728..c7c6d1f29 100644 --- a/crates/uv-distribution-types/src/lib.rs +++ b/crates/uv-distribution-types/src/lib.rs @@ -1343,10 +1343,10 @@ mod test { /// Ensure that we don't accidentally grow the `Dist` sizes. #[test] fn dist_size() { - assert!(size_of::() <= 248, "{}", size_of::()); + assert!(size_of::() <= 232, "{}", size_of::()); assert!(size_of::() <= 216, "{}", size_of::()); assert!( - size_of::() <= 248, + size_of::() <= 232, "{}", size_of::() ); diff --git a/crates/uv-pep508/src/unnamed.rs b/crates/uv-pep508/src/unnamed.rs index 0893cc989..7985ad76f 100644 --- a/crates/uv-pep508/src/unnamed.rs +++ b/crates/uv-pep508/src/unnamed.rs @@ -29,7 +29,7 @@ pub trait UnnamedRequirementUrl: Pep508Url { /// Set the verbatim representation of the URL. #[must_use] - fn with_given(self, given: impl Into) -> Self; + fn with_given(self, given: impl AsRef) -> 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) -> Self { + fn with_given(self, given: impl AsRef) -> Self { self.with_given(given) } @@ -258,7 +258,7 @@ fn preprocess_unnamed_url( 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( 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( len, input: cursor.to_string(), })? - .with_given(url.to_string()); + .with_given(url); Ok((url, extras)) } @@ -296,7 +296,7 @@ fn preprocess_unnamed_url( 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( len, input: cursor.to_string(), })? - .with_given(url.to_string()); + .with_given(url); Ok((url, extras)) } } @@ -321,7 +321,7 @@ fn preprocess_unnamed_url( 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( len, input: cursor.to_string(), })? - .with_given(url.to_string()); + .with_given(url); Ok((url, extras)) } } diff --git a/crates/uv-pep508/src/verbatim_url.rs b/crates/uv-pep508/src/verbatim_url.rs index 4d715b389..817675dc8 100644 --- a/crates/uv-pep508/src/verbatim_url.rs +++ b/crates/uv-pep508/src/verbatim_url.rs @@ -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, + given: Option, } 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) -> Self { + pub fn with_given(self, given: impl AsRef) -> 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 { - 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"))] diff --git a/crates/uv-pypi-types/src/parsed_url.rs b/crates/uv-pypi-types/src/parsed_url.rs index c5d8b5aed..510a61091 100644 --- a/crates/uv-pypi-types/src/parsed_url.rs +++ b/crates/uv-pypi-types/src/parsed_url.rs @@ -133,7 +133,7 @@ impl UnnamedRequirementUrl for VerbatimParsedUrl { }) } - fn with_given(self, given: impl Into) -> Self { + fn with_given(self, given: impl AsRef) -> Self { Self { verbatim: self.verbatim.with_given(given), ..self diff --git a/crates/uv-resolver/src/redirect.rs b/crates/uv-resolver/src/redirect.rs index af9cb64dd..49c654861 100644 --- a/crates/uv-resolver/src/redirect.rs +++ b/crates/uv-resolver/src/redirect.rs @@ -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")?;