Struct w5500_hl::Hostname

source ·
pub struct Hostname<'a> { /* private fields */ }
Expand description

A validated hostname.

This is not used within this crate, it is provided here for crates implementing protocols such as DNS and DHCP to use.

Implementations§

source§

impl<'a> Hostname<'a>

source

pub const fn new(hostname: &'a str) -> Option<Self>

Create a new hostname.

This validates the hostname for RFC-1035 compliance:

A hostname is valid if the following condition are true:

  • It does not start or end with '-' or '.'.
  • It does not contain any characters outside of the alphanumeric range, except for '-' and '.'.
  • It is not empty.
  • It is 253 or fewer characters.
  • Its labels (characters separated by .) are not empty.
  • Its labels are 63 or fewer characters.
  • Its labels do not start or end with '-' or '.'.
Example
use w5500_hl::Hostname;

assert!(Hostname::new("is-valid-example").is_some());
assert!(Hostname::new("this-is-not-?-valid").is_none());
source

pub const fn new_unwrapped(hostname: &'a str) -> Self

Create a new hostname, panicking if the hostname is invalid.

Panics

This is the same as new, but it will panic on invalid hostnames.

This should only be used in const contexts where the evaluation will fail at compile time.

Example
use w5500_hl::Hostname;

const MY_HOSTNAME: Hostname = Hostname::new_unwrapped("valid.hostname");
source

pub fn labels(&self) -> Split<'a, char>

Returns an iterator over the labels of the hostname.

Example
use core::str::Split;
use w5500_hl::Hostname;

const DOCS_RS: Hostname = Hostname::new_unwrapped("docs.rs");
let mut lables: Split<char> = DOCS_RS.labels();

assert_eq!(lables.next(), Some("docs"));
assert_eq!(lables.next(), Some("rs"));
assert_eq!(lables.next(), None);
source

pub fn len(&self) -> u8

Length of the hostname in bytes.

Example
use w5500_hl::Hostname;

const DOCS_RS: Hostname = Hostname::new_unwrapped("docs.rs");

assert_eq!(DOCS_RS.len(), 7);
source

pub const unsafe fn new_unchecked(hostname: &'a str) -> Self

Create a new hostname without checking for validity.

Safety

The hostname argument must meet all the conditions for validity described in new.

Example
use w5500_hl::Hostname;

// safety: doc.rs is a valid hostname
const DOCS_RS: Hostname = unsafe { Hostname::new_unchecked("docs.rs") };
source

pub const fn as_bytes(&self) -> &[u8]

Converts the hostname to a byte slice.

Example
use w5500_hl::Hostname;

const DOCS_RS: Hostname = Hostname::new_unwrapped("docs.rs");
assert_eq!(DOCS_RS.as_bytes(), [100, 111, 99, 115, 46, 114, 115]);

Trait Implementations§

source§

impl<'a> Clone for Hostname<'a>

source§

fn clone(&self) -> Hostname<'a>

Returns a copy 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<'a> Debug for Hostname<'a>

source§

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

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

impl<'a> Format for Hostname<'a>

source§

fn format(&self, f: Formatter<'_>)

Writes the defmt representation of self to fmt.
source§

impl<'a> From<Hostname<'a>> for &'a str

source§

fn from(hostname: Hostname<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> TryFrom<&'a str> for Hostname<'a>

§

type Error = TryFromStrError

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

fn try_from(hostname: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> Copy for Hostname<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Hostname<'a>

§

impl<'a> Send for Hostname<'a>

§

impl<'a> Sync for Hostname<'a>

§

impl<'a> Unpin for Hostname<'a>

§

impl<'a> UnwindSafe for Hostname<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.