pub struct TxUrl(_);
Expand description
New-type for sync ref-counted Urls to make passing around tx2 more efficient.
Implementations
Methods from Deref<Target = Url>
sourcepub fn join(&self, input: &str) -> Result<Url, ParseError>
pub fn join(&self, input: &str) -> Result<Url, ParseError>
Parse a string as an URL, with this URL as the base URL.
The inverse of this is make_relative
.
Note: a trailing slash is significant. Without it, the last path component is considered to be a “file” name to be removed to get at the “directory” that is used as the base:
Examples
use url::Url;
let base = Url::parse("https://example.net/a/b.html")?;
let url = base.join("c.png")?;
assert_eq!(url.as_str(), "https://example.net/a/c.png"); // Not /a/b.html/c.png
let base = Url::parse("https://example.net/a/b/")?;
let url = base.join("c.png")?;
assert_eq!(url.as_str(), "https://example.net/a/b/c.png");
Errors
If the function can not parse an URL from the given string
with this URL as the base URL, a ParseError
variant will be returned.
sourcepub fn make_relative(&self, url: &Url) -> Option<String>
pub fn make_relative(&self, url: &Url) -> Option<String>
Creates a relative URL if possible, with this URL as the base URL.
This is the inverse of join
.
Examples
use url::Url;
let base = Url::parse("https://example.net/a/b.html")?;
let url = Url::parse("https://example.net/a/c.png")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("c.png"));
let base = Url::parse("https://example.net/a/b/")?;
let url = Url::parse("https://example.net/a/b/c.png")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("c.png"));
let base = Url::parse("https://example.net/a/b/")?;
let url = Url::parse("https://example.net/a/d/c.png")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("../d/c.png"));
let base = Url::parse("https://example.net/a/b.html?c=d")?;
let url = Url::parse("https://example.net/a/b.html?e=f")?;
let relative = base.make_relative(&url);
assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("?e=f"));
Errors
If this URL can’t be a base for the given URL, None
is returned.
This is for example the case if the scheme, host or port are not the same.
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Return the serialization of this URL.
This is fast since that serialization is already stored in the Url
struct.
Examples
use url::Url;
let url_str = "https://example.net/";
let url = Url::parse(url_str)?;
assert_eq!(url.as_str(), url_str);
sourcepub fn origin(&self) -> Origin
pub fn origin(&self) -> Origin
Return the origin of this URL (https://url.spec.whatwg.org/#origin)
Note: this returns an opaque origin for file:
URLs, which causes
url.origin() != url.origin()
.
Examples
URL with ftp
scheme:
use url::{Host, Origin, Url};
let url = Url::parse("ftp://example.com/foo")?;
assert_eq!(url.origin(),
Origin::Tuple("ftp".into(),
Host::Domain("example.com".into()),
21));
URL with blob
scheme:
use url::{Host, Origin, Url};
let url = Url::parse("blob:https://example.com/foo")?;
assert_eq!(url.origin(),
Origin::Tuple("https".into(),
Host::Domain("example.com".into()),
443));
URL with file
scheme:
use url::{Host, Origin, Url};
let url = Url::parse("file:///tmp/foo")?;
assert!(!url.origin().is_tuple());
let other_url = Url::parse("file:///tmp/foo")?;
assert!(url.origin() != other_url.origin());
URL with other scheme:
use url::{Host, Origin, Url};
let url = Url::parse("foo:bar")?;
assert!(!url.origin().is_tuple());
sourcepub fn scheme(&self) -> &str
pub fn scheme(&self) -> &str
Return the scheme of this URL, lower-cased, as an ASCII string without the ‘:’ delimiter.
Examples
use url::Url;
let url = Url::parse("file:///tmp/foo")?;
assert_eq!(url.scheme(), "file");
Return whether the URL has an ‘authority’, which can contain a username, password, host, and port number.
URLs that do not are either path-only like unix:/run/foo.socket
or cannot-be-a-base like data:text/plain,Stuff
.
Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert!(url.has_authority());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(!url.has_authority());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(!url.has_authority());
sourcepub fn cannot_be_a_base(&self) -> bool
pub fn cannot_be_a_base(&self) -> bool
Return whether this URL is a cannot-be-a-base URL, meaning that parsing a relative URL string with this URL as the base will return an error.
This is the case if the scheme and :
delimiter are not followed by a /
slash,
as is typically the case of data:
and mailto:
URLs.
Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert!(!url.cannot_be_a_base());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(!url.cannot_be_a_base());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(url.cannot_be_a_base());
sourcepub fn username(&self) -> &str
pub fn username(&self) -> &str
Return the username for this URL (typically the empty string) as a percent-encoded ASCII string.
Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert_eq!(url.username(), "rms");
let url = Url::parse("ftp://:secret123@example.com")?;
assert_eq!(url.username(), "");
let url = Url::parse("https://example.com")?;
assert_eq!(url.username(), "");
sourcepub fn password(&self) -> Option<&str>
pub fn password(&self) -> Option<&str>
Return the password for this URL, if any, as a percent-encoded ASCII string.
Examples
use url::Url;
let url = Url::parse("ftp://rms:secret123@example.com")?;
assert_eq!(url.password(), Some("secret123"));
let url = Url::parse("ftp://:secret123@example.com")?;
assert_eq!(url.password(), Some("secret123"));
let url = Url::parse("ftp://rms@example.com")?;
assert_eq!(url.password(), None);
let url = Url::parse("https://example.com")?;
assert_eq!(url.password(), None);
sourcepub fn has_host(&self) -> bool
pub fn has_host(&self) -> bool
Equivalent to url.host().is_some()
.
Examples
use url::Url;
let url = Url::parse("ftp://rms@example.com")?;
assert!(url.has_host());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(!url.has_host());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(!url.has_host());
sourcepub fn host_str(&self) -> Option<&str>
pub fn host_str(&self) -> Option<&str>
Return the string representation of the host (domain or IP address) for this URL, if any.
Non-ASCII domains are punycode-encoded per IDNA if this is the host
of a special URL, or percent encoded for non-special URLs.
IPv6 addresses are given between [
and ]
brackets.
Cannot-be-a-base URLs (typical of data:
and mailto:
) and some file:
URLs
don’t have a host.
See also the host
method.
Examples
use url::Url;
let url = Url::parse("https://127.0.0.1/index.html")?;
assert_eq!(url.host_str(), Some("127.0.0.1"));
let url = Url::parse("ftp://rms@example.com")?;
assert_eq!(url.host_str(), Some("example.com"));
let url = Url::parse("unix:/run/foo.socket")?;
assert_eq!(url.host_str(), None);
let url = Url::parse("data:text/plain,Stuff")?;
assert_eq!(url.host_str(), None);
sourcepub fn host(&self) -> Option<Host<&str>>
pub fn host(&self) -> Option<Host<&str>>
Return the parsed representation of the host for this URL. Non-ASCII domain labels are punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for non-special URLs.
Cannot-be-a-base URLs (typical of data:
and mailto:
) and some file:
URLs
don’t have a host.
See also the host_str
method.
Examples
use url::Url;
let url = Url::parse("https://127.0.0.1/index.html")?;
assert!(url.host().is_some());
let url = Url::parse("ftp://rms@example.com")?;
assert!(url.host().is_some());
let url = Url::parse("unix:/run/foo.socket")?;
assert!(url.host().is_none());
let url = Url::parse("data:text/plain,Stuff")?;
assert!(url.host().is_none());
sourcepub fn domain(&self) -> Option<&str>
pub fn domain(&self) -> Option<&str>
If this URL has a host and it is a domain name (not an IP address), return it. Non-ASCII domains are punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for non-special URLs.
Examples
use url::Url;
let url = Url::parse("https://127.0.0.1/")?;
assert_eq!(url.domain(), None);
let url = Url::parse("mailto:rms@example.net")?;
assert_eq!(url.domain(), None);
let url = Url::parse("https://example.com/")?;
assert_eq!(url.domain(), Some("example.com"));
sourcepub fn port(&self) -> Option<u16>
pub fn port(&self) -> Option<u16>
Return the port number for this URL, if any.
Note that default port numbers are never reflected by the serialization,
use the port_or_known_default()
method if you want a default port number returned.
Examples
use url::Url;
let url = Url::parse("https://example.com")?;
assert_eq!(url.port(), None);
let url = Url::parse("https://example.com:443/")?;
assert_eq!(url.port(), None);
let url = Url::parse("ssh://example.com:22")?;
assert_eq!(url.port(), Some(22));
sourcepub fn port_or_known_default(&self) -> Option<u16>
pub fn port_or_known_default(&self) -> Option<u16>
Return the port number for this URL, or the default port number if it is known.
This method only knows the default port number
of the http
, https
, ws
, wss
and ftp
schemes.
For URLs in these schemes, this method always returns Some(_)
.
For other schemes, it is the same as Url::port()
.
Examples
use url::Url;
let url = Url::parse("foo://example.com")?;
assert_eq!(url.port_or_known_default(), None);
let url = Url::parse("foo://example.com:1456")?;
assert_eq!(url.port_or_known_default(), Some(1456));
let url = Url::parse("https://example.com")?;
assert_eq!(url.port_or_known_default(), Some(443));
sourcepub fn socket_addrs(
&self,
default_port_number: impl Fn() -> Option<u16>
) -> Result<Vec<SocketAddr, Global>, Error>
pub fn socket_addrs(
&self,
default_port_number: impl Fn() -> Option<u16>
) -> Result<Vec<SocketAddr, Global>, Error>
Resolve a URL’s host and port number to SocketAddr
.
If the URL has the default port number of a scheme that is unknown to this library,
default_port_number
provides an opportunity to provide the actual port number.
In non-example code this should be implemented either simply as || None
,
or by matching on the URL’s .scheme()
.
If the host is a domain, it is resolved using the standard library’s DNS support.
Examples
let url = url::Url::parse("https://example.net/").unwrap();
let addrs = url.socket_addrs(|| None).unwrap();
std::net::TcpStream::connect(&*addrs)
/// With application-specific known default port numbers
fn socket_addrs(url: url::Url) -> std::io::Result<Vec<std::net::SocketAddr>> {
url.socket_addrs(|| match url.scheme() {
"socks5" | "socks5h" => Some(1080),
_ => None,
})
}
sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
Return the path for this URL, as a percent-encoded ASCII string. For cannot-be-a-base URLs, this is an arbitrary string that doesn’t start with ‘/’. For other URLs, this starts with a ‘/’ slash and continues with slash-separated path segments.
Examples
use url::{Url, ParseError};
let url = Url::parse("https://example.com/api/versions?page=2")?;
assert_eq!(url.path(), "/api/versions");
let url = Url::parse("https://example.com")?;
assert_eq!(url.path(), "/");
let url = Url::parse("https://example.com/countries/việt nam")?;
assert_eq!(url.path(), "/countries/vi%E1%BB%87t%20nam");
sourcepub fn path_segments(&self) -> Option<Split<'_, char>>
pub fn path_segments(&self) -> Option<Split<'_, char>>
Unless this URL is cannot-be-a-base, return an iterator of ‘/’ slash-separated path segments, each as a percent-encoded ASCII string.
Return None
for cannot-be-a-base URLs.
When Some
is returned, the iterator always contains at least one string
(which may be empty).
Examples
use url::Url;
let url = Url::parse("https://example.com/foo/bar")?;
let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
assert_eq!(path_segments.next(), Some("foo"));
assert_eq!(path_segments.next(), Some("bar"));
assert_eq!(path_segments.next(), None);
let url = Url::parse("https://example.com")?;
let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
assert_eq!(path_segments.next(), Some(""));
assert_eq!(path_segments.next(), None);
let url = Url::parse("data:text/plain,HelloWorld")?;
assert!(url.path_segments().is_none());
let url = Url::parse("https://example.com/countries/việt nam")?;
let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
assert_eq!(path_segments.next(), Some("countries"));
assert_eq!(path_segments.next(), Some("vi%E1%BB%87t%20nam"));
sourcepub fn query(&self) -> Option<&str>
pub fn query(&self) -> Option<&str>
Return this URL’s query string, if any, as a percent-encoded ASCII string.
Examples
use url::Url;
fn run() -> Result<(), ParseError> {
let url = Url::parse("https://example.com/products?page=2")?;
let query = url.query();
assert_eq!(query, Some("page=2"));
let url = Url::parse("https://example.com/products")?;
let query = url.query();
assert!(query.is_none());
let url = Url::parse("https://example.com/?country=español")?;
let query = url.query();
assert_eq!(query, Some("country=espa%C3%B1ol"));
sourcepub fn query_pairs(&self) -> Parse<'_>
pub fn query_pairs(&self) -> Parse<'_>
Parse the URL’s query string, if any, as application/x-www-form-urlencoded
and return an iterator of (key, value) pairs.
Examples
use std::borrow::Cow;
use url::Url;
let url = Url::parse("https://example.com/products?page=2&sort=desc")?;
let mut pairs = url.query_pairs();
assert_eq!(pairs.count(), 2);
assert_eq!(pairs.next(), Some((Cow::Borrowed("page"), Cow::Borrowed("2"))));
assert_eq!(pairs.next(), Some((Cow::Borrowed("sort"), Cow::Borrowed("desc"))));
sourcepub fn fragment(&self) -> Option<&str>
pub fn fragment(&self) -> Option<&str>
Return this URL’s fragment identifier, if any.
A fragment is the part of the URL after the #
symbol.
The fragment is optional and, if present, contains a fragment identifier
that identifies a secondary resource, such as a section heading
of a document.
In HTML, the fragment identifier is usually the id attribute of a an element that is scrolled to on load. Browsers typically will not send the fragment portion of a URL to the server.
Note: the parser did not percent-encode this component, but the input may have been percent-encoded already.
Examples
use url::Url;
let url = Url::parse("https://example.com/data.csv#row=4")?;
assert_eq!(url.fragment(), Some("row=4"));
let url = Url::parse("https://example.com/data.csv#cell=4,1-6,2")?;
assert_eq!(url.fragment(), Some("cell=4,1-6,2"));
sourcepub fn serialize_internal<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
pub fn serialize_internal<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
Serialize with Serde using the internal representation of the Url
struct.
The corresponding deserialize_internal
method sacrifices some invariant-checking
for speed, compared to the Deserialize
trait impl.
This method is only available if the serde
Cargo feature is enabled.
sourcepub fn to_file_path(&self) -> Result<PathBuf, ()>
pub fn to_file_path(&self) -> Result<PathBuf, ()>
Assuming the URL is in the file
scheme or similar,
convert its path to an absolute std::path::Path
.
Note: This does not actually check the URL’s scheme
,
and may give nonsensical results for other schemes.
It is the user’s responsibility to check the URL’s scheme before calling this.
let path = url.to_file_path();
Returns Err
if the host is neither empty nor "localhost"
(except on Windows, where
file:
URLs may have a non-local host),
or if Path::new_opt()
returns None
.
(That is, if the percent-decoded path contains a NUL byte or,
for a Windows path, is not UTF-8.)
Trait Implementations
sourceimpl<'de> Deserialize<'de> for TxUrl
impl<'de> Deserialize<'de> for TxUrl
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Ord for TxUrl
impl Ord for TxUrl
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl PartialEq<TxUrl> for TxUrl
impl PartialEq<TxUrl> for TxUrl
sourceimpl PartialOrd<TxUrl> for TxUrl
impl PartialOrd<TxUrl> for TxUrl
sourcefn partial_cmp(&self, other: &TxUrl) -> Option<Ordering>
fn partial_cmp(&self, other: &TxUrl) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for TxUrl
impl StructuralEq for TxUrl
impl StructuralPartialEq for TxUrl
Auto Trait Implementations
impl RefUnwindSafe for TxUrl
impl Send for TxUrl
impl Sync for TxUrl
impl Unpin for TxUrl
impl UnwindSafe for TxUrl
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
impl<T> FutureExt for T
impl<T> FutureExt for T
fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Pointable for T
impl<T> Pointable for T
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct self
from the equivalent element of its
superset. Read more
fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if self
is actually part of its subset T
(and can be converted to it).
fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as self.to_subset
but without any property checks. Always succeeds.
fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more