Host

Enum Host 

Source
pub enum Host<S> {
    Ip(IpAddr),
    Domain(S),
}
Expand description

The host name

Variants§

§

Ip(IpAddr)

An IP address

§

Domain(S)

A DNS domain name

Implementations§

Source§

impl<S> Host<S>

Source

pub const fn is_ip(&self) -> bool

Returns true if this value is of type Ip. Returns false otherwise

Source

pub const fn is_domain(&self) -> bool

Returns true if this value is of type Domain. Returns false otherwise

Source§

impl<S> Host<S>

Source

pub fn unwrap_ip(self) -> IpAddr

Unwraps this value to the Host::Ip variant. Panics if this value is of any other type.

Source

pub fn unwrap_ip_ref(&self) -> &IpAddr

Unwraps this reference to the Host::Ip variant. Panics if this value is of any other type.

Source

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.

Source

pub fn unwrap_domain(self) -> S

Unwraps this value to the Host::Domain variant. Panics if this value is of any other type.

Source

pub fn unwrap_domain_ref(&self) -> &S

Unwraps this reference to the Host::Domain variant. Panics if this value is of any other type.

Source

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>

Source

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");
Source

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]>

Source

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");
Source

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>

Source

pub const fn from_ip(ip: IpAddr) -> Host<S>

Creates a new Host from an IP address.

Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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>

Source

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());
Source

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,

Source§

fn cheap_clone(&self) -> Self

Returns a copy of the value.
Source§

impl<S> Clone for Host<S>
where S: Clone,

Source§

fn clone(&self) -> Host<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Debug for Host<S>
where S: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<S> Display for Host<S>
where S: Display,

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<S> From<Domain<S>> for Host<S>

Source§

fn from(value: Domain<S>) -> Host<S>

use hostaddr::{Domain, Host};

let domain: Domain<String> = "example.com".parse().unwrap();
let host: Host<String> = domain.into();
Source§

impl<S> From<IpAddr> for Host<S>

Source§

fn from(value: IpAddr) -> Host<S>

use hostaddr::Host;
use std::net::IpAddr;

let ip: IpAddr = "127.0.0.1".parse().unwrap();
let host: Host<String> = ip.into();
Source§

impl<S> From<Ipv4Addr> for Host<S>

Source§

fn from(value: Ipv4Addr) -> Host<S>

use hostaddr::Host;
use std::net::Ipv4Addr;

let ip: Ipv4Addr = "127.0.0.1".parse().unwrap();
let host: Host<String> = ip.into();
Source§

impl<S> From<Ipv6Addr> for Host<S>

Source§

fn from(value: Ipv6Addr) -> Host<S>

use hostaddr::Host;
use std::net::Ipv6Addr;

let ip: Ipv6Addr = "::1".parse().unwrap();
let host: Host<String> = ip.into();
Source§

impl<S> FromStr for Host<S>
where Domain<S>: FromStr,

Source§

type Err = ParseHostError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Host<S>, <Host<S> as FromStr>::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<S> Hash for Host<S>
where S: Hash,

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<S> Ord for Host<S>
where S: Ord,

Source§

fn cmp(&self, other: &Host<S>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<S> PartialEq for Host<S>
where S: PartialEq,

Source§

fn eq(&self, other: &Host<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S> PartialOrd for Host<S>
where S: PartialOrd,

Source§

fn partial_cmp(&self, other: &Host<S>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, S> TryFrom<&'a str> for Host<S>
where Domain<S>: TryFrom<&'a str>,

Source§

type Error = ParseHostError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &'a str) -> Result<Host<S>, <Host<S> as TryFrom<&'a str>>::Error>

Performs the conversion.
Source§

impl<S> Copy for Host<S>
where S: Copy,

Source§

impl<S> Eq for Host<S>
where S: Eq,

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Address for T
where T: CheapClone + Eq + Ord + Hash + Debug + Display + Unpin + 'static,

Source§

impl<T> Id for T
where T: CheapClone + Eq + Ord + Hash + Debug + Display + Unpin + 'static,