pub struct Hostname(/* private fields */);Available on crate feature
net only.Expand description
A DNS hostname.
This type provides type-safe hostnames with RFC 1123 validation.
It uses the newtype pattern with #[repr(transparent)] for zero-cost abstraction.
§Invariants
- Total length is 1-253 characters
- Each label is 1-63 characters
- Only ASCII letters, digits, and hyphens are allowed
- Labels cannot start or end with hyphens
- Stored in lowercase for case-insensitive comparison
§Examples
use bare_types::net::Hostname;
// Create a hostname
let hostname = Hostname::new("example.com")?;
// Access the string representation
assert_eq!(hostname.as_str(), "example.com");
// Check if it's localhost
assert!(!hostname.is_localhost());
// Iterate over labels
let labels: Vec<&str> = hostname.labels().collect();
assert_eq!(labels, vec!["example", "com"]);
// Parse from string
let hostname: Hostname = "www.example.com".parse()?;Implementations§
Source§impl Hostname
impl Hostname
Sourcepub fn new(s: &str) -> Result<Self, HostnameError>
pub fn new(s: &str) -> Result<Self, HostnameError>
Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the hostname as a string slice.
§Examples
use bare_types::net::Hostname;
let hostname = Hostname::new("example.com").unwrap();
assert_eq!(hostname.as_str(), "example.com");Sourcepub const fn as_inner(&self) -> &String<253>
pub const fn as_inner(&self) -> &String<253>
Returns a reference to the underlying heapless::String.
§Examples
use bare_types::net::Hostname;
let hostname = Hostname::new("example.com").unwrap();
let inner: &heapless::String<253> = hostname.as_inner();
assert_eq!(inner.as_str(), "example.com");Sourcepub fn into_inner(self) -> String<253>
pub fn into_inner(self) -> String<253>
Consumes this hostname and returns the underlying string.
§Examples
use bare_types::net::Hostname;
let hostname = Hostname::new("example.com").unwrap();
let inner = hostname.into_inner();
assert_eq!(inner.as_str(), "example.com");Sourcepub fn is_localhost(&self) -> bool
pub fn is_localhost(&self) -> bool
Returns true if this is the localhost hostname.
§Examples
use bare_types::net::Hostname;
assert!(Hostname::new("localhost").unwrap().is_localhost());
assert!(!Hostname::new("example.com").unwrap().is_localhost());Sourcepub fn labels(&self) -> impl Iterator<Item = &str>
pub fn labels(&self) -> impl Iterator<Item = &str>
Returns an iterator over the labels in this hostname.
§Examples
use bare_types::net::Hostname;
let hostname = Hostname::new("www.example.com").unwrap();
let labels: Vec<&str> = hostname.labels().collect();
assert_eq!(labels, vec!["www", "example", "com"]);Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Hostname
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Hostname
Available on crate feature
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Generate an arbitrary value of
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Generate an arbitrary value of
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Get a size hint for how many bytes out of an
Unstructured this type
needs to construct itself. Read moreSource§impl<'de> Deserialize<'de> for Hostname
impl<'de> Deserialize<'de> for Hostname
Source§fn 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
Source§impl Ord for Hostname
impl Ord for Hostname
Source§impl PartialOrd for Hostname
impl PartialOrd for Hostname
impl Eq for Hostname
impl StructuralPartialEq for Hostname
Auto Trait Implementations§
impl Freeze for Hostname
impl RefUnwindSafe for Hostname
impl Send for Hostname
impl Sync for Hostname
impl Unpin for Hostname
impl UnwindSafe for Hostname
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
Mutably borrows from an owned value. Read more