pub struct HostAddr<S> { /* private fields */ }Expand description
A host address, which consists of a Host and an optional port number.
Implementations§
Source§impl<S> HostAddr<S>
impl<S> HostAddr<S>
Sourcepub const fn new(host: Host<S>) -> HostAddr<S>
pub const fn new(host: Host<S>) -> HostAddr<S>
Create a new host address
§Example
use hostaddr::HostAddr;
let host = HostAddr::<String>::new("example.com".parse().unwrap());
println!("{}", host);Sourcepub fn from_domain(domain: Domain<S>) -> HostAddr<S>
pub fn from_domain(domain: Domain<S>) -> HostAddr<S>
Create a new host address from a domain name
§Example
use hostaddr::{HostAddr, Domain};
let host = HostAddr::<String>::from_domain("example.com".parse().unwrap());
println!("{}", host);Sourcepub const fn from_ip_addr(ip: IpAddr) -> HostAddr<S>
pub const fn from_ip_addr(ip: IpAddr) -> HostAddr<S>
Create a new host address from an IP address
§Example
use hostaddr::HostAddr;
let host = HostAddr::<String>::from_ip_addr("127.0.0.1".parse().unwrap());
println!("{}", host);Sourcepub const fn from_sock_addr(addr: SocketAddr) -> HostAddr<S>
pub const fn from_sock_addr(addr: SocketAddr) -> HostAddr<S>
Create a new host address from a SocketAddr
§Example
use hostaddr::HostAddr;
let host = HostAddr::<String>::from_sock_addr("127.0.0.1:8080".parse().unwrap());
println!("{}", host);Sourcepub const fn host(&self) -> &Host<S>
pub const fn host(&self) -> &Host<S>
Get the host name
§Example
use hostaddr::HostAddr;
let addr: HostAddr<String> = "example.com:8080".parse().unwrap();
println!("{}", addr.host());Sourcepub const fn ip(&self) -> Option<&IpAddr>
pub const fn ip(&self) -> Option<&IpAddr>
Get the ip address
§Example
use hostaddr::HostAddr;
let addr: HostAddr<String> = HostAddr::from_ip_addr("127.0.0.1".parse().unwrap());
println!("{}", addr.ip().unwrap());Sourcepub const fn port(&self) -> Option<u16>
pub const fn port(&self) -> Option<u16>
Get the port number
§Example
use hostaddr::HostAddr;
let addr: HostAddr<String> = "example.com:8080".parse().unwrap();
assert_eq!(Some(8080), addr.port());Sourcepub const fn set_port(&mut self, port: u16) -> &mut HostAddr<S>
pub const fn set_port(&mut self, port: u16) -> &mut HostAddr<S>
Set the port number
§Example
use hostaddr::HostAddr;
let mut host: HostAddr<String> = "example.com".parse().unwrap();
host
.set_port(8080)
.set_host("example.org".parse().unwrap());
assert_eq!(Some(8080), host.port());Sourcepub const fn maybe_port(&mut self, port: Option<u16>) -> &mut HostAddr<S>
pub const fn maybe_port(&mut self, port: Option<u16>) -> &mut HostAddr<S>
Set the port number
See also maybe_with_port.
§Example
use hostaddr::HostAddr;
let mut host: HostAddr<String> = "example.com".parse().unwrap();
host
.maybe_port(Some(8080))
.set_host("example.org".parse().unwrap());
assert_eq!(Some(8080), host.port());Sourcepub const fn maybe_with_port(self, port: Option<u16>) -> HostAddr<S>
pub const fn maybe_with_port(self, port: Option<u16>) -> HostAddr<S>
Set the port number
See also maybe_port.
§Example
use hostaddr::HostAddr;
let host = "example.com".parse::<HostAddr<String>>().unwrap().maybe_with_port(Some(8080));
assert_eq!(Some(8080), host.port());Sourcepub const fn with_default_port(self, default: u16) -> HostAddr<S>
pub const fn with_default_port(self, default: u16) -> HostAddr<S>
Set a default port if no port is currently set.
If a port is already set, this method does nothing.
§Example
use hostaddr::HostAddr;
let addr = "example.com".parse::<HostAddr<String>>().unwrap()
.with_default_port(443);
assert_eq!(Some(443), addr.port());
let addr = "example.com:8080".parse::<HostAddr<String>>().unwrap()
.with_default_port(443);
assert_eq!(Some(8080), addr.port());Sourcepub const fn clear_port(&mut self) -> &mut HostAddr<S>
pub const fn clear_port(&mut self) -> &mut HostAddr<S>
Clear the port number.
§Example
use hostaddr::HostAddr;
let mut addr: HostAddr<String> = "example.com:8080".parse().unwrap();
addr.clear_port();
assert_eq!(None, addr.port());Sourcepub const fn has_port(&self) -> bool
pub const fn has_port(&self) -> bool
Returns true if a port is set.
§Example
use hostaddr::HostAddr;
let addr: HostAddr<String> = "example.com:8080".parse().unwrap();
assert!(addr.has_port());
let addr: HostAddr<String> = "example.com".parse().unwrap();
assert!(!addr.has_port());Sourcepub fn set_host(&mut self, host: Host<S>) -> &mut HostAddr<S>
pub fn set_host(&mut self, host: Host<S>) -> &mut HostAddr<S>
Set the host name
§Example
use hostaddr::HostAddr;
let mut addr: HostAddr<String> = "example.com".parse().unwrap();
addr
.set_host("example.org".parse().unwrap())
.set_port(8080);
assert_eq!("example.org", addr.as_ref().host().unwrap_domain().as_str());Sourcepub fn with_host(self, host: Host<S>) -> HostAddr<S>
pub fn with_host(self, host: Host<S>) -> HostAddr<S>
Set the host name
§Example
use hostaddr::HostAddr;
let addr: HostAddr<String> = HostAddr::from_sock_addr("127.0.0.1:8080".parse().unwrap())
.with_host("example.com".parse().unwrap());
assert_eq!("example.com", addr.as_ref().host().unwrap_domain().as_str());Sourcepub const fn is_ip(&self) -> bool
pub const fn is_ip(&self) -> bool
Returns true if the host is an IP address
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = HostAddr::from_sock_addr("127.0.0.1:8080".parse().unwrap());
assert!(host.is_ip());Sourcepub const fn is_ipv4(&self) -> bool
pub const fn is_ipv4(&self) -> bool
Returns true if the host is an Ipv4 address
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = HostAddr::from_sock_addr("127.0.0.1:8080".parse().unwrap());
assert!(host.is_ipv4());Sourcepub const fn is_ipv6(&self) -> bool
pub const fn is_ipv6(&self) -> bool
Returns true if the host is an Ipv6 address
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = HostAddr::from_sock_addr("[::1]:8080".parse().unwrap());
assert!(host.is_ipv6());Sourcepub const fn is_domain(&self) -> bool
pub const fn is_domain(&self) -> bool
Returns true if the host is a domain name
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = "example.com".parse().unwrap();
assert!(host.is_domain());Sourcepub fn is_localhost(&self) -> bool
pub fn is_localhost(&self) -> bool
Returns true if the host represents localhost.
This method checks if the host is:
- An IPv4 loopback address (127.0.0.0/8)
- An IPv6 loopback address (::1)
- The domain name “localhost”
§Example
use hostaddr::HostAddr;
let addr = HostAddr::try_from_ascii_str("127.0.0.1").unwrap();
assert!(addr.is_localhost());
let addr = HostAddr::try_from_ascii_str("::1").unwrap();
assert!(addr.is_localhost());
let addr = HostAddr::try_from_ascii_str("localhost").unwrap();
assert!(addr.is_localhost());
let addr = HostAddr::try_from_ascii_str("example.com").unwrap();
assert!(!addr.is_localhost());Sourcepub const fn to_socket_addr(&self) -> Option<SocketAddr>
pub const fn to_socket_addr(&self) -> Option<SocketAddr>
Converts to a SocketAddr if the host is an IP address and a port is set.
Returns None if the host is a domain name or if no port is set.
§Example
use hostaddr::HostAddr;
use std::net::{IpAddr, SocketAddr};
let addr: HostAddr<String> = "127.0.0.1:8080".parse().unwrap();
assert_eq!(
Some(SocketAddr::new("127.0.0.1".parse::<IpAddr>().unwrap(), 8080)),
addr.to_socket_addr()
);
// Domain names cannot be converted to SocketAddr
let addr: HostAddr<String> = "example.com:8080".parse().unwrap();
assert_eq!(None, addr.to_socket_addr());
// Missing port returns None
let addr: HostAddr<String> = "127.0.0.1".parse().unwrap();
assert_eq!(None, addr.to_socket_addr());Sourcepub const fn as_ref(&self) -> HostAddr<&S>
pub const fn as_ref(&self) -> HostAddr<&S>
Converts from &HostAddr<S> to HostAddr<&S>.
§Example
use std::sync::Arc;
use hostaddr::HostAddr;
let host: HostAddr<Arc<str>> = "example.com:8080".try_into().unwrap();
assert_eq!("example.com", &**host.as_ref().host().unwrap_domain());Sourcepub fn as_deref(&self) -> HostAddr<&<S as Deref>::Target>where
S: Deref,
pub fn as_deref(&self) -> HostAddr<&<S as Deref>::Target>where
S: Deref,
Converts from HostAddr<S> (or &HostAddr<S>) to HostAddr<&S::Target>.
§Example
use std::sync::Arc;
use hostaddr::HostAddr;
let host = "example.com:9090".parse::<HostAddr<Arc<str>>>().unwrap();
assert_eq!("example.com", host.as_deref().host().unwrap_domain());Sourcepub fn into_components(self) -> (Host<S>, Option<u16>)
pub fn into_components(self) -> (Host<S>, Option<u16>)
Consumes the HostAddr and returns the host name and port number.
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = "example.com:8080".parse().unwrap();
let (host, port) = host.into_components();
assert_eq!("example.com", host.unwrap_domain().as_str());
assert_eq!(Some(8080), port);Sourcepub fn unwrap_domain(self) -> (S, Option<u16>)
pub fn unwrap_domain(self) -> (S, Option<u16>)
Unwraps the domain, panics if the host is an IP address.
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = "example.com".parse().unwrap();
let (domain, port) = host.unwrap_domain();
assert_eq!("example.com", domain.as_str());
assert_eq!(None, port);Sourcepub fn unwrap_ip(self) -> (IpAddr, Option<u16>)
pub fn unwrap_ip(self) -> (IpAddr, Option<u16>)
Unwraps the IP address, panics if the host is a domain name.
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = HostAddr::from_sock_addr("[::1]:8080".parse().unwrap());
let (ip, port) = host.unwrap_ip();
assert_eq!(ip, "::1".parse::<std::net::IpAddr>().unwrap());
assert_eq!(Some(8080), port);Source§impl<S> HostAddr<&S>
impl<S> HostAddr<&S>
Sourcepub const fn copied(self) -> HostAddr<S>where
S: Copy,
pub const fn copied(self) -> HostAddr<S>where
S: Copy,
Maps an HostAddr<&S> to an HostAddr<S> by copying the contents of the
host addr.
§Example
use hostaddr::{HostAddr, Buffer};
let host: HostAddr<Buffer> = HostAddr::try_from("example.com").unwrap();
assert_eq!("example.com", host.as_ref().copied().unwrap_domain().0.as_str());Sourcepub fn cloned(self) -> HostAddr<S>where
S: Clone,
pub fn cloned(self) -> HostAddr<S>where
S: Clone,
Maps an HostAddr<&S> to an HostAddr<S> by cloning the contents of the
host addr.
§Example
use hostaddr::HostAddr;
let host: HostAddr<String> = "example.com".parse().unwrap();
assert_eq!("example.com", host.as_ref().cloned().unwrap_domain().0.as_str());Source§impl<'a> HostAddr<&'a str>
impl<'a> HostAddr<&'a str>
Sourcepub fn try_from_ascii_str(
input: &'a str,
) -> Result<HostAddr<&'a str>, ParseAsciiHostAddrError>
pub fn try_from_ascii_str( input: &'a str, ) -> Result<HostAddr<&'a str>, ParseAsciiHostAddrError>
Parses a HostAddr name from &str.
Unlike HostAddr::try_from or HostAddr::from_str, this method does not perform any percent decoding
or punycode decoding. If the input is not ASCII, it will return an error.
§Example
use hostaddr::HostAddr;
let host = HostAddr::try_from_ascii_str("example.com").unwrap();
assert_eq!(host.unwrap_domain().0, "example.com");
// This will return an error because the domain is not ASCII.
assert!(HostAddr::try_from_ascii_str("测试.中国").is_err());
// Thie will not return an error, even though the human-readable domain is not ASCII.
let host = HostAddr::try_from_ascii_str("xn--0zwm56d.xn--fiqs8s").unwrap();
assert_eq!(host.unwrap_domain().0, "xn--0zwm56d.xn--fiqs8s");Source§impl<'a> HostAddr<&'a [u8]>
impl<'a> HostAddr<&'a [u8]>
Sourcepub fn try_from_ascii_bytes(
input: &'a [u8],
) -> Result<HostAddr<&'a [u8]>, ParseAsciiHostAddrError>
pub fn try_from_ascii_bytes( input: &'a [u8], ) -> Result<HostAddr<&'a [u8]>, ParseAsciiHostAddrError>
Parses a HostAddr from &[u8].
Unlike HostAddr::try_from or HostAddr::from_str, this method does not perform any percent decoding
or punycode decoding. If the input is not ASCII, it will return an error.
§Example
use hostaddr::HostAddr;
let host = HostAddr::try_from_ascii_bytes(b"example.com").unwrap();
assert_eq!(host.unwrap_domain().0, b"example.com");
// This will return an error because the domain is not ASCII.
assert!(HostAddr::try_from_ascii_bytes("测试.中国".as_bytes()).is_err());
// Thie will not return an error, even though the human-readable domain is not ASCII.
let host = HostAddr::try_from_ascii_bytes(b"xn--0zwm56d.xn--fiqs8s").unwrap();
assert_eq!(host.unwrap_domain().0, b"xn--0zwm56d.xn--fiqs8s");Trait Implementations§
Source§impl<S> CheapClone for HostAddr<S>where
S: CheapClone,
impl<S> CheapClone for HostAddr<S>where
S: CheapClone,
Source§fn cheap_clone(&self) -> Self
fn cheap_clone(&self) -> Self
Source§impl Data for HostAddr<SmolStr>
impl Data for HostAddr<SmolStr>
Source§fn from_ref(
val: <HostAddr<SmolStr> as Data>::Ref<'_>,
) -> Result<HostAddr<SmolStr>, DecodeError>
fn from_ref( val: <HostAddr<SmolStr> as Data>::Ref<'_>, ) -> Result<HostAddr<SmolStr>, DecodeError>
Source§fn encoded_len(&self) -> usize
fn encoded_len(&self) -> usize
Source§fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
Source§fn encoded_len_with_length_delimited(&self) -> usize
fn encoded_len_with_length_delimited(&self) -> usize
Source§fn encode_to_vec(&self) -> Result<Vec<u8>, EncodeError>
fn encode_to_vec(&self) -> Result<Vec<u8>, EncodeError>
Source§fn encode_to_bytes(&self) -> Result<Bytes, EncodeError>
fn encode_to_bytes(&self) -> Result<Bytes, EncodeError>
Bytes.Source§fn encode_length_delimited(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
fn encode_length_delimited(&self, buf: &mut [u8]) -> Result<usize, EncodeError>
Source§fn encode_length_delimited_to_vec(&self) -> Result<Vec<u8>, EncodeError>
fn encode_length_delimited_to_vec(&self) -> Result<Vec<u8>, EncodeError>
Source§fn encode_length_delimited_to_bytes(&self) -> Result<Bytes, EncodeError>
fn encode_length_delimited_to_bytes(&self) -> Result<Bytes, EncodeError>
Bytes.Source§fn decode(src: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
fn decode(src: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
Source§fn decode_length_delimited(buf: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
fn decode_length_delimited(buf: &[u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
Source§impl<'a> DataRef<'a, HostAddr<SmolStr>> for HostAddr<Buffer>
impl<'a> DataRef<'a, HostAddr<SmolStr>> for HostAddr<Buffer>
Source§fn decode(buf: &'a [u8]) -> Result<(usize, HostAddr<Buffer>), DecodeError>
fn decode(buf: &'a [u8]) -> Result<(usize, HostAddr<Buffer>), DecodeError>
Source§fn decode_length_delimited(src: &'a [u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
fn decode_length_delimited(src: &'a [u8]) -> Result<(usize, Self), DecodeError>where
Self: Sized,
Source§impl<S> From<SocketAddr> for HostAddr<S>
impl<S> From<SocketAddr> for HostAddr<S>
Source§fn from(addr: SocketAddr) -> HostAddr<S>
fn from(addr: SocketAddr) -> HostAddr<S>
use hostaddr::HostAddr;
use std::net::SocketAddr;
let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
let host = HostAddr::<String>::from(addr);Source§impl<S> From<SocketAddrV4> for HostAddr<S>
impl<S> From<SocketAddrV4> for HostAddr<S>
Source§fn from(addr: SocketAddrV4) -> HostAddr<S>
fn from(addr: SocketAddrV4) -> HostAddr<S>
use hostaddr::HostAddr;
use std::net::SocketAddrV4;
let addr = "127.0.0.1:8080".parse::<SocketAddrV4>().unwrap();
let host = HostAddr::<String>::from(addr);Source§impl<S> From<SocketAddrV6> for HostAddr<S>
impl<S> From<SocketAddrV6> for HostAddr<S>
Source§fn from(addr: SocketAddrV6) -> HostAddr<S>
fn from(addr: SocketAddrV6) -> HostAddr<S>
use hostaddr::HostAddr;
use std::net::SocketAddrV6;
let addr = "[::1]:8080".parse::<SocketAddrV6>().unwrap();
let host = HostAddr::<String>::from(addr);Source§impl<S> Ord for HostAddr<S>where
S: Ord,
impl<S> Ord for HostAddr<S>where
S: Ord,
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<S> PartialOrd for HostAddr<S>where
S: PartialOrd,
impl<S> PartialOrd for HostAddr<S>where
S: PartialOrd,
impl<S> Copy for HostAddr<S>where
S: Copy,
impl<S> Eq for HostAddr<S>where
S: Eq,
impl<S> StructuralPartialEq for HostAddr<S>
Auto Trait Implementations§
impl<S> Freeze for HostAddr<S>where
S: Freeze,
impl<S> RefUnwindSafe for HostAddr<S>where
S: RefUnwindSafe,
impl<S> Send for HostAddr<S>where
S: Send,
impl<S> Sync for HostAddr<S>where
S: Sync,
impl<S> Unpin for HostAddr<S>where
S: Unpin,
impl<S> UnwindSafe for HostAddr<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more