pub enum Host<S> {
Ip(IpAddr),
Domain(S),
}Expand description
The host name
Variants§
Implementations§
Source§impl<S> Host<S>
impl<S> Host<S>
Sourcepub fn unwrap_ip(self) -> IpAddr
pub fn unwrap_ip(self) -> IpAddr
Unwraps this value to the Host::Ip variant.
Panics if this value is of any other type.
Sourcepub fn unwrap_ip_ref(&self) -> &IpAddr
pub fn unwrap_ip_ref(&self) -> &IpAddr
Unwraps this reference to the Host::Ip variant.
Panics if this value is of any other type.
Sourcepub fn unwrap_ip_mut(&mut self) -> &mut IpAddr
pub fn unwrap_ip_mut(&mut self) -> &mut IpAddr
Unwraps this mutable reference to the Host::Ip variant.
Panics if this value is of any other type.
Sourcepub fn unwrap_domain(self) -> S
pub fn unwrap_domain(self) -> S
Unwraps this value to the Host::Domain variant.
Panics if this value is of any other type.
Sourcepub fn unwrap_domain_ref(&self) -> &S
pub fn unwrap_domain_ref(&self) -> &S
Unwraps this reference to the Host::Domain variant.
Panics if this value is of any other type.
Sourcepub fn unwrap_domain_mut(&mut self) -> &mut S
pub fn unwrap_domain_mut(&mut self) -> &mut S
Unwraps this mutable reference to the Host::Domain variant.
Panics if this value is of any other type.
Source§impl<'a> Host<&'a str>
impl<'a> Host<&'a str>
Sourcepub fn try_from_ascii_str(
input: &'a str,
) -> Result<Host<&'a str>, ParseAsciiHostError>
pub fn try_from_ascii_str( input: &'a str, ) -> Result<Host<&'a str>, ParseAsciiHostError>
Parses an ASCII Host from &str.
Unlike Host::try_from or Host::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::Host;
let host = Host::try_from_ascii_str("example.com").unwrap();
assert_eq!(host.unwrap_domain(), "example.com");
// This will return an error because the domain is not ASCII.
assert!(Host::try_from_ascii_str("测试.中国").is_err());
// Thie will not return an error, even though the human-readable domain is not ASCII.
let host = Host::try_from_ascii_str("xn--0zwm56d.xn--fiqs8s").unwrap();
assert_eq!(host.unwrap_domain(), "xn--0zwm56d.xn--fiqs8s");Sourcepub const fn as_bytes(&self) -> Host<&'a [u8]>
pub const fn as_bytes(&self) -> Host<&'a [u8]>
Converts the domain to a Host<&'a str>.
§Example
use hostaddr::Host;
let host = Host::try_from_ascii_str("example.com").unwrap();
assert_eq!(host.as_bytes().unwrap_domain(), b"example.com");
let host = Host::try_from_ascii_str("127.0.0.1").unwrap();
assert_eq!(host.as_bytes().unwrap_ip(), "127.0.0.1".parse::<core::net::IpAddr>().unwrap());Source§impl<'a> Host<&'a [u8]>
impl<'a> Host<&'a [u8]>
Sourcepub fn try_from_ascii_bytes(
input: &'a [u8],
) -> Result<Host<&'a [u8]>, ParseAsciiHostError>
pub fn try_from_ascii_bytes( input: &'a [u8], ) -> Result<Host<&'a [u8]>, ParseAsciiHostError>
Parses an ASCII Host from &[u8].
Unlike Host::try_from or Host::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::Host;
let host = Host::try_from_ascii_bytes(b"example.com").unwrap();
assert_eq!(host.unwrap_domain(), b"example.com");
// This will return an error because the domain is not ASCII.
assert!(Host::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 = Host::try_from_ascii_bytes(b"xn--0zwm56d.xn--fiqs8s").unwrap();
assert_eq!(host.unwrap_domain(), b"xn--0zwm56d.xn--fiqs8s");Sourcepub const fn as_str(&self) -> Host<&'a str>
pub const fn as_str(&self) -> Host<&'a str>
Converts the domain to a Host<&'a str>.
§Example
use hostaddr::Host;
let domain = Host::try_from_ascii_bytes(b"example.com").unwrap();
assert_eq!(domain.as_str().unwrap_domain(), "example.com");
let host = Host::try_from_ascii_bytes(b"127.0.0.1").unwrap();
assert_eq!(host.as_str().unwrap_ip(), "127.0.0.1".parse::<core::net::IpAddr>().unwrap());Source§impl<S> Host<S>
impl<S> Host<S>
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::Host;
let host: Host<&str> = Host::from_ip("127.0.0.1".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::Host;
let host: Host<&str> = Host::from_ip("::1".parse().unwrap());
assert!(host.is_ipv6());Sourcepub const fn as_ref(&self) -> Host<&S>
pub const fn as_ref(&self) -> Host<&S>
Converts from &Host<S> to Host<&S>.
§Example
use std::sync::Arc;
use hostaddr::Host;
let host = "example.com".parse::<Host<Arc<str>>>().unwrap();
assert_eq!("example.com", &**host.as_ref().unwrap_domain());Sourcepub fn as_deref(&self) -> Host<&<S as Deref>::Target>where
S: Deref,
pub fn as_deref(&self) -> Host<&<S as Deref>::Target>where
S: Deref,
Converts from Host<S> (or &Host<S>) to Host<&S::Target>.
§Example
use std::{sync::Arc, net::IpAddr};
use hostaddr::Host;
let host: Host<Arc<str>> = "example.com".try_into().unwrap();
assert_eq!("example.com", host.as_deref().unwrap_domain());
let host: Host<Arc<str>> = "127.0.0.1".try_into().unwrap();
assert_eq!("127.0.0.1".parse::<IpAddr>().unwrap(), host.as_deref().unwrap_ip());Sourcepub const fn ip(&self) -> Option<&IpAddr>
pub const fn ip(&self) -> Option<&IpAddr>
Returns Some(ip) if the host is an IP address.
§Example
use hostaddr::Host;
use std::net::IpAddr;
let host: Host<String> = "127.0.0.1".parse().unwrap();
assert_eq!(Some("127.0.0.1".parse().unwrap()), host.ip().copied());
let host: Host<String> = "example.com".parse().unwrap();
assert!(host.ip().is_none());Sourcepub const fn domain(&self) -> Option<&S>
pub const fn domain(&self) -> Option<&S>
Returns Some(domain) if the host is a domain name.
§Example
use hostaddr::Host;
let host: Host<String> = "example.com".parse().unwrap();
assert_eq!(Some("example.com".to_string()), host.domain().cloned());
let host: Host<String> = "127.0.0.1".parse().unwrap();
assert!(host.domain().is_none());Source§impl<S> Host<&S>
impl<S> Host<&S>
Sourcepub const fn copied(self) -> Host<S>where
S: Copy,
pub const fn copied(self) -> Host<S>where
S: Copy,
Maps an Host<&S> to an Host<S> by copying the contents of the
host.
§Example
use hostaddr::{Host, Buffer};
use std::net::IpAddr;
let host: Host<Buffer> = Host::try_from("example.com").unwrap();
assert_eq!("example.com", host.as_ref().copied().unwrap_domain().as_str());
let host: Host<Buffer> = "127.0.0.1".parse().unwrap();
assert_eq!("127.0.0.1".parse::<IpAddr>().unwrap(), host.as_ref().copied().unwrap_ip());Sourcepub fn cloned(self) -> Host<S>where
S: Clone,
pub fn cloned(self) -> Host<S>where
S: Clone,
Maps an Host<&S> to an Host<S> by cloning the contents of the
host.
§Example
use hostaddr::Host;
use std::net::IpAddr;
let host: Host<String> = "example.com".parse().unwrap();
assert_eq!("example.com", host.as_ref().cloned().unwrap_domain().as_str());
let host: Host<String> = "127.0.0.1".parse().unwrap();
assert_eq!("127.0.0.1".parse::<IpAddr>().unwrap(), host.as_ref().cloned().unwrap_ip());Trait Implementations§
Source§impl<S> CheapClone for Host<S>where
S: CheapClone,
impl<S> CheapClone for Host<S>where
S: CheapClone,
Source§fn cheap_clone(&self) -> Self
fn cheap_clone(&self) -> Self
Source§impl<S> Ord for Host<S>where
S: Ord,
impl<S> Ord for Host<S>where
S: Ord,
Source§impl<S> PartialOrd for Host<S>where
S: PartialOrd,
impl<S> PartialOrd for Host<S>where
S: PartialOrd,
impl<S> Copy for Host<S>where
S: Copy,
impl<S> Eq for Host<S>where
S: Eq,
impl<S> StructuralPartialEq for Host<S>
Auto Trait Implementations§
impl<S> Freeze for Host<S>where
S: Freeze,
impl<S> RefUnwindSafe for Host<S>where
S: RefUnwindSafe,
impl<S> Send for Host<S>where
S: Send,
impl<S> Sync for Host<S>where
S: Sync,
impl<S> Unpin for Host<S>where
S: Unpin,
impl<S> UnwindSafe for Host<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