Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ArcStr in verbatim URL #10600

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use arcstr in verbatim URL
  • Loading branch information
charliermarsh committed Jan 14, 2025
commit 33fda9a8fee6b93e7215d22b7e7007ad67169023
4 changes: 2 additions & 2 deletions crates/uv-distribution-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
);
Expand Down
18 changes: 9 additions & 9 deletions crates/uv-pep508/src/unnamed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -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)
}

Expand Down Expand Up @@ -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));
}

Expand All @@ -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`
Expand All @@ -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))
}

Expand All @@ -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));
}

Expand All @@ -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))
}
}
Expand All @@ -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));
}

Expand All @@ -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))
}
}
Expand Down
36 changes: 19 additions & 17 deletions crates/uv-pep508/src/verbatim_url.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use regex::Regex;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt::Debug;
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};

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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))?)
}
}

Expand Down Expand Up @@ -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`
Expand All @@ -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()))
Expand All @@ -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"))]
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-pypi-types/src/parsed_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-resolver/src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;

Expand Down
Loading