mirror of https://github.com/astral-sh/uv
Add a borrowed `Realm` type (#14798)
## Summary Allows zero-cost comparisons against URL references.
This commit is contained in:
parent
a3ea1b69f2
commit
036c9bef3f
|
|
@ -5,7 +5,7 @@ use url::Url;
|
||||||
use uv_static::EnvVars;
|
use uv_static::EnvVars;
|
||||||
|
|
||||||
use crate::Credentials;
|
use crate::Credentials;
|
||||||
use crate::realm::Realm;
|
use crate::realm::{Realm, RealmRef};
|
||||||
|
|
||||||
/// The [`Realm`] for the Hugging Face platform.
|
/// The [`Realm`] for the Hugging Face platform.
|
||||||
static HUGGING_FACE_REALM: LazyLock<Realm> = LazyLock::new(|| {
|
static HUGGING_FACE_REALM: LazyLock<Realm> = LazyLock::new(|| {
|
||||||
|
|
@ -37,7 +37,7 @@ pub(crate) struct HuggingFaceProvider;
|
||||||
impl HuggingFaceProvider {
|
impl HuggingFaceProvider {
|
||||||
/// Returns the credentials for the Hugging Face platform, if available.
|
/// Returns the credentials for the Hugging Face platform, if available.
|
||||||
pub(crate) fn credentials_for(url: &Url) -> Option<Credentials> {
|
pub(crate) fn credentials_for(url: &Url) -> Option<Credentials> {
|
||||||
if Realm::from(url) == *HUGGING_FACE_REALM {
|
if RealmRef::from(url) == *HUGGING_FACE_REALM {
|
||||||
if let Some(token) = HUGGING_FACE_TOKEN.as_ref() {
|
if let Some(token) = HUGGING_FACE_TOKEN.as_ref() {
|
||||||
return Some(Credentials::Bearer {
|
return Some(Credentials::Bearer {
|
||||||
token: token.clone(),
|
token: token.clone(),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
use std::{fmt::Display, fmt::Formatter};
|
use std::{fmt::Display, fmt::Formatter};
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use uv_small_str::SmallString;
|
use uv_small_str::SmallString;
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ use uv_small_str::SmallString;
|
||||||
// The port is only allowed to differ if it matches the "default port" for the scheme.
|
// The port is only allowed to differ if it matches the "default port" for the scheme.
|
||||||
// However, `url` (and therefore `reqwest`) sets the `port` to `None` if it matches the default port
|
// However, `url` (and therefore `reqwest`) sets the `port` to `None` if it matches the default port
|
||||||
// so we do not need any special handling here.
|
// so we do not need any special handling here.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct Realm {
|
pub(crate) struct Realm {
|
||||||
scheme: SmallString,
|
scheme: SmallString,
|
||||||
host: Option<SmallString>,
|
host: Option<SmallString>,
|
||||||
|
|
@ -59,6 +59,76 @@ impl Display for Realm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Realm {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
RealmRef::from(self) == RealmRef::from(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Realm {}
|
||||||
|
|
||||||
|
impl Hash for Realm {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
RealmRef::from(self).hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A reference to a [`Realm`] that can be used for zero-allocation comparisons.
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub(crate) struct RealmRef<'a> {
|
||||||
|
scheme: &'a str,
|
||||||
|
host: Option<&'a str>,
|
||||||
|
port: Option<u16>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a Url> for RealmRef<'a> {
|
||||||
|
fn from(url: &'a Url) -> Self {
|
||||||
|
Self {
|
||||||
|
scheme: url.scheme(),
|
||||||
|
host: url.host_str(),
|
||||||
|
port: url.port(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for RealmRef<'_> {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.scheme == other.scheme && self.host == other.host && self.port == other.port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for RealmRef<'_> {}
|
||||||
|
|
||||||
|
impl Hash for RealmRef<'_> {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.scheme.hash(state);
|
||||||
|
self.host.hash(state);
|
||||||
|
self.port.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> PartialEq<RealmRef<'a>> for Realm {
|
||||||
|
fn eq(&self, rhs: &RealmRef<'a>) -> bool {
|
||||||
|
RealmRef::from(self) == *rhs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq<Realm> for RealmRef<'_> {
|
||||||
|
fn eq(&self, rhs: &Realm) -> bool {
|
||||||
|
*self == RealmRef::from(rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a Realm> for RealmRef<'a> {
|
||||||
|
fn from(realm: &'a Realm) -> Self {
|
||||||
|
Self {
|
||||||
|
scheme: &realm.scheme,
|
||||||
|
host: realm.host.as_deref(),
|
||||||
|
port: realm.port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use url::{ParseError, Url};
|
use url::{ParseError, Url};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue