Skip to main content

Label

Struct Label 

Source
pub struct Label<'a> { /* private fields */ }
Expand description

Represents a label in a domain name

A label is a sequence of 1 to 63 octets. On the wire it can hold any octet, including dots (RFC 1035 section 3.1), so no escaping exists in the binary format.

What counts as a valid label depends on the role it plays in a name:

MethodRoleRule
Label::is_valid_hostnamehost nameletters, digits and hyphens
Label::is_valid_service_labelservice name_ + RFC 6335 service name
Label::is_valid_instance_labelDNS-SD instance or subtypeany content

Label::new only checks the length. To build labels with content that is not valid for a host name (unicode, dots, spaces), use Label::new or Label::new_unchecked and Name::new_with_labels.

The Display implementation uses std::string::String::from_utf8_lossy to display the label, and escapes . as \. and \ as \\, so that labels joined in a Name can be told apart from the dots that separate them (RFC 6763 section 4.3).

Implementations§

Source§

impl<'a> Label<'a>

Source

pub fn new<T: Into<Cow<'a, [u8]>>>(data: T) -> Result<Self>

Create a new Label from text that may contain escape sequences.

The escapes follow the presentation format of RFC 1035 section 5.1 and RFC 4343 section 2.1:

  • \DDD, exactly three decimal digits (at most 255), is the octet with that value
  • \X, where X is not a digit, is the character X (\. and \\ included)

Returns SimpleDnsError::InvalidServiceLabel if the label is empty or longer than 63 octets after unescaping, or if an escape is malformed (a trailing \, or \ followed by fewer than three digits or a value above 255).

The content is not checked against a naming rule. Use the is_valid_* methods for that.

let label = Label::new(r"My\.Device".as_bytes()).unwrap();
assert_eq!(b"My.Device", label.as_ref());

assert!(Label::new(r"bad\1".as_bytes()).is_err());
Source

pub fn new_unchecked<T: Into<Cow<'a, [u8]>>>(data: T) -> Self

Create a new Label from raw bytes, without unescaping and without checking for size limits or valid content. This function can be used to create labels with unicode characters

Source

pub fn len(&self) -> usize

Returns the length of the label

Source

pub fn is_empty(&self) -> bool

Returns true if the label is empty

Source

pub fn into_owned<'b>(self) -> Label<'b>

Transforms the inner data into its owned type

Source

pub fn is_valid_hostname(&self) -> bool

Returns true if the label is a valid host name label (RFC 952, RFC 1123).

It has 1 to 63 octets, made only of ASCII letters, digits and hyphens, and starts and ends with a letter or a digit.

assert!(Label::new_unchecked(&b"my-host1"[..]).is_valid_hostname());
assert!(!Label::new_unchecked(&b"_srv"[..]).is_valid_hostname());
assert!(!Label::new_unchecked(&b"-host"[..]).is_valid_hostname());
Source

pub fn is_valid_service_label(&self) -> bool

Returns true if the label is a valid DNS-SD service label, such as _http (RFC 6763 section 7).

It is an underscore followed by a service name as defined in RFC 6335 section 5.1: 1 to 15 ASCII letters, digits or hyphens, with at least one letter, that does not begin or end with a hyphen and has no adjacent hyphens.

This only checks the service name. The _tcp or _udp label that follows it is checked by Name::is_valid_service_name.

assert!(Label::new_unchecked(&b"_http"[..]).is_valid_service_label());
assert!(Label::new_unchecked(&b"_9p"[..]).is_valid_service_label());
assert!(!Label::new_unchecked(&b"http"[..]).is_valid_service_label());
assert!(!Label::new_unchecked(&b"_123"[..]).is_valid_service_label());
Source

pub fn is_valid_instance_label(&self) -> bool

Returns true if the label can be used as a DNS-SD instance name or subtype (RFC 6763 sections 4.3 and 7.1).

Any content is accepted, including dots, spaces and UTF-8, as long as the label has 1 to 63 octets. The limit counts octets, not characters.

assert!(Label::new_unchecked(&b"My Printer. Floor 2"[..]).is_valid_instance_label());
assert!(!Label::new_unchecked(&b""[..]).is_valid_instance_label());

Trait Implementations§

Source§

impl AsRef<[u8]> for Label<'_>

Source§

fn as_ref(&self) -> &[u8] ⓘ

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Clone for Label<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Label<'_>

Source§

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

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

impl Display for Label<'_>

Source§

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

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

impl<'a> Eq for Label<'a>

Source§

impl Hash for Label<'_>

Source§

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

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 Ord for Label<'_>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Label<'_>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Label<'_>

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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

Auto Trait Implementations§

§

impl<'a> Freeze for Label<'a>

§

impl<'a> RefUnwindSafe for Label<'a>

§

impl<'a> Send for Label<'a>

§

impl<'a> Sync for Label<'a>

§

impl<'a> Unpin for Label<'a>

§

impl<'a> UnsafeUnpin for Label<'a>

§

impl<'a> UnwindSafe for Label<'a>

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.