pub struct RelativeName<Octs: ?Sized>(/* private fields */);
Expand description
An uncompressed, relative domain name.
A relative domain name is one that doesn’t end with the root label. As the
name suggests, it is relative to some other domain name. This type wraps
a octets sequence containing such a relative name similarly to the way
Name
wraps an absolute one. In fact, it behaves very similarly to
Name
taking into account differences when slicing and dicing names.
RelativeName
guarantees that the name is at most 254 bytes long. As the
length limit for a domain name is actually 255 bytes, this means that you
can always safely turn a RelativeName
into a Name
by adding the root
label (which is exactly one byte long).
Implementations§
source§impl<Octs> RelativeName<Octs>
impl<Octs> RelativeName<Octs>
§Creating Values
sourcepub const unsafe fn from_octets_unchecked(octets: Octs) -> Self
pub const unsafe fn from_octets_unchecked(octets: Octs) -> Self
Creates a relative domain name from octets without checking.
Since the content of the octets sequence can be anything, really, this is an unsafe function.
§Safety
The octets sequence passed via octets
must contain a correctly
encoded relative domain name. It must be at most 254 octets long.
There must be no root labels anywhere in the name.
sourcepub fn from_octets(octets: Octs) -> Result<Self, RelativeNameError>
pub fn from_octets(octets: Octs) -> Result<Self, RelativeNameError>
Creates a relative domain name from an octets sequence.
This checks that octets
contains a properly encoded relative domain
name and fails if it doesn’t.
sourcepub fn wildcard() -> Self
pub fn wildcard() -> Self
Creates a relative domain name representing the wildcard label.
The wildcard label is intended to match any label. There are special rules for names with wildcard labels. Note that the comparison traits implemented for domain names do not consider wildcards and treat them as regular labels.
sourcepub fn from_chars<C>(chars: C) -> Result<Self, RelativeFromStrError>where
Octs: FromBuilder,
<Octs as FromBuilder>::Builder: EmptyBuilder + FreezeBuilder<Octets = Octs> + AsRef<[u8]> + AsMut<[u8]>,
C: IntoIterator<Item = char>,
pub fn from_chars<C>(chars: C) -> Result<Self, RelativeFromStrError>where
Octs: FromBuilder,
<Octs as FromBuilder>::Builder: EmptyBuilder + FreezeBuilder<Octets = Octs> + AsRef<[u8]> + AsMut<[u8]>,
C: IntoIterator<Item = char>,
Creates a domain name from a sequence of characters.
The sequence must result in a domain name in representation format. That is, its labels should be separated by dots. Actual dots, white space and backslashes should be escaped by a preceeding backslash, and any byte value that is not a printable ASCII character should be encoded by a backslash followed by its three digit decimal value.
If Internationalized Domain Names are to be used, the labels already need to be in punycode-encoded form.
source§impl RelativeName<[u8]>
impl RelativeName<[u8]>
sourcepub fn from_slice(slice: &[u8]) -> Result<&Self, RelativeNameError>
pub fn from_slice(slice: &[u8]) -> Result<&Self, RelativeNameError>
Creates a relative domain name from an octet slice.
Note that the input must be in wire format, as shown below.
§Example
use domain::base::name::RelativeName;
RelativeName::from_slice(b"\x0c_submissions\x04_tcp");
sourcepub fn empty_slice() -> &'static Self
pub fn empty_slice() -> &'static Self
Returns an empty relative name atop a unsized slice.
pub fn wildcard_slice() -> &'static Self
source§impl RelativeName<&'static [u8]>
impl RelativeName<&'static [u8]>
sourcepub fn wildcard_ref() -> Self
pub fn wildcard_ref() -> Self
Creates a wildcard relative name atop a slice reference.
source§impl RelativeName<Vec<u8>>
impl RelativeName<Vec<u8>>
sourcepub fn wildcard_vec() -> Self
pub fn wildcard_vec() -> Self
Creates a wildcard relative name atop a Vec<u8>
.
sourcepub fn vec_from_str(s: &str) -> Result<Self, RelativeFromStrError>
pub fn vec_from_str(s: &str) -> Result<Self, RelativeFromStrError>
Parses a string into a relative name atop a Vec<u8>
.
source§impl RelativeName<Bytes>
impl RelativeName<Bytes>
sourcepub fn empty_bytes() -> Self
pub fn empty_bytes() -> Self
Creates an empty relative name atop a bytes value.
sourcepub fn wildcard_bytes() -> Self
pub fn wildcard_bytes() -> Self
Creates a wildcard relative name atop a bytes value.
sourcepub fn bytes_from_str(s: &str) -> Result<Self, RelativeFromStrError>
pub fn bytes_from_str(s: &str) -> Result<Self, RelativeFromStrError>
Parses a string into a relative name atop a Bytes
.
source§impl<Octs: ?Sized> RelativeName<Octs>
impl<Octs: ?Sized> RelativeName<Octs>
§Conversions
sourcepub fn into_octets(self) -> Octswhere
Octs: Sized,
pub fn into_octets(self) -> Octswhere
Octs: Sized,
Converts the name into the underlying octets.
sourcepub fn for_ref(&self) -> RelativeName<&Octs>
pub fn for_ref(&self) -> RelativeName<&Octs>
Returns a domain name using a reference to the octets.
sourcepub fn as_slice(&self) -> &[u8] ⓘ
pub fn as_slice(&self) -> &[u8] ⓘ
Returns a reference to an octets slice with the content of the name.
sourcepub fn for_slice(&self) -> &RelativeName<[u8]>
pub fn for_slice(&self) -> &RelativeName<[u8]>
Returns a domain name for the octets slice of the content.
sourcepub fn make_canonical(&mut self)
pub fn make_canonical(&mut self)
Converts the name into its canonical form.
source§impl<Octs> RelativeName<Octs>
impl<Octs> RelativeName<Octs>
sourcepub fn into_builder(self) -> NameBuilder<<Octs as IntoBuilder>::Builder>where
Octs: IntoBuilder,
pub fn into_builder(self) -> NameBuilder<<Octs as IntoBuilder>::Builder>where
Octs: IntoBuilder,
Converts the name into a domain name builder for appending data.
This method is only available for octets sequences that have an
associated octets builder such as Vec<u8>
or Bytes
.
sourcepub fn into_absolute(self) -> Result<Name<Octs>, PushError>where
Octs: IntoBuilder,
<Octs as IntoBuilder>::Builder: FreezeBuilder<Octets = Octs> + AsRef<[u8]> + AsMut<[u8]>,
pub fn into_absolute(self) -> Result<Name<Octs>, PushError>where
Octs: IntoBuilder,
<Octs as IntoBuilder>::Builder: FreezeBuilder<Octets = Octs> + AsRef<[u8]> + AsMut<[u8]>,
Converts the name into an absolute name by appending the root label.
This manipulates the name itself and thus is only available for
octets sequences that can be converted into an octets builder and back
such as Vec<u8>
.
sourcepub fn chain<N: ToLabelIter>(
self,
other: N,
) -> Result<Chain<Self, N>, LongChainError>
pub fn chain<N: ToLabelIter>( self, other: N, ) -> Result<Chain<Self, N>, LongChainError>
Chains another name to the end of this name.
Depending on whether other
is an absolute or relative domain name,
the resulting name will behave like an absolute or relative name.
The method will fail if the combined length of the two names is
greater than the size limit of 255. Note that in this case you will
loose both self
and other
, so it might be worthwhile to check
first.
source§impl<Octs: AsRef<[u8]> + ?Sized> RelativeName<Octs>
impl<Octs: AsRef<[u8]> + ?Sized> RelativeName<Octs>
§Working with Labels
sourcepub fn label_count(&self) -> usize
pub fn label_count(&self) -> usize
Returns the number of labels in the name.
sourcepub fn first(&self) -> Option<&Label>
pub fn first(&self) -> Option<&Label>
Returns a reference to the first label if the name isn’t empty.
sourcepub fn last(&self) -> Option<&Label>
pub fn last(&self) -> Option<&Label>
Returns a reference to the last label if the name isn’t empty.
sourcepub fn ndots(&self) -> usize
pub fn ndots(&self) -> usize
Returns the number of dots in the string representation of the name.
Specifically, returns a value equal to the number of labels minus one, except for an empty name where it returns a zero, also.
sourcepub fn starts_with<N: ToLabelIter>(&self, base: &N) -> bool
pub fn starts_with<N: ToLabelIter>(&self, base: &N) -> bool
Determines whether base
is a prefix of self
.
sourcepub fn ends_with<N: ToLabelIter>(&self, base: &N) -> bool
pub fn ends_with<N: ToLabelIter>(&self, base: &N) -> bool
Determines whether base
is a suffix of self
.
sourcepub fn is_label_start(&self, index: usize) -> bool
pub fn is_label_start(&self, index: usize) -> bool
Returns whether an index points to the first octet of a label.
sourcepub fn slice(&self, range: impl RangeBounds<usize>) -> &RelativeName<[u8]>
pub fn slice(&self, range: impl RangeBounds<usize>) -> &RelativeName<[u8]>
Returns a part of the name indicated by start and end positions.
The returned name will start at position begin
and end right before
position end
. Both positions are given as indexes into the
underlying octets sequence and must point to the begining of a label.
The method returns a reference to an unsized relative domain name and
is thus best suited for temporary referencing. If you want to keep the
part of the name around, range
is likely a better choice.
§Panics
The method panics if either position is not the beginning of a label or is out of bounds.
sourcepub fn range(
&self,
range: impl RangeBounds<usize>,
) -> RelativeName<<Octs as Octets>::Range<'_>>where
Octs: Octets,
pub fn range(
&self,
range: impl RangeBounds<usize>,
) -> RelativeName<<Octs as Octets>::Range<'_>>where
Octs: Octets,
Returns a part of the name indicated by start and end positions.
The returned name will start at position begin
and end right before
position end
. Both positions are given as indexes into the
underlying octets sequence and must point to the begining of a label.
§Panics
The method panics if either position is not the beginning of a label or is out of bounds.
source§impl<Octs: AsRef<[u8]> + ?Sized> RelativeName<Octs>
impl<Octs: AsRef<[u8]> + ?Sized> RelativeName<Octs>
sourcepub fn split(
&self,
mid: usize,
) -> (RelativeName<Octs::Range<'_>>, RelativeName<Octs::Range<'_>>)where
Octs: Octets,
pub fn split(
&self,
mid: usize,
) -> (RelativeName<Octs::Range<'_>>, RelativeName<Octs::Range<'_>>)where
Octs: Octets,
Splits the name into two at the given position.
Returns a pair of the left and right part of the split name.
§Panics
The method panics if the position is not the beginning of a label or is beyond the end of the name.
sourcepub fn truncate(&mut self, len: usize)where
Octs: Truncate,
pub fn truncate(&mut self, len: usize)where
Octs: Truncate,
Truncates the name to the given length.
§Panics
The method panics if the position is not the beginning of a label or is beyond the end of the name.
sourcepub fn split_first(&self) -> Option<(&Label, RelativeName<Octs::Range<'_>>)>where
Octs: Octets,
pub fn split_first(&self) -> Option<(&Label, RelativeName<Octs::Range<'_>>)>where
Octs: Octets,
Splits off the first label.
If there is at least one label in the name, returns the first label
as a relative domain name with exactly one label and makes self
contain the domain name starting after that first label. If the name
is empty, returns None
.
sourcepub fn parent(&self) -> Option<RelativeName<Octs::Range<'_>>>where
Octs: Octets,
pub fn parent(&self) -> Option<RelativeName<Octs::Range<'_>>>where
Octs: Octets,
Returns the parent name.
Returns None
if the name was empty.
sourcepub fn strip_suffix<N: ToRelativeName>(
&mut self,
base: &N,
) -> Result<(), StripSuffixError>where
Octs: Truncate,
pub fn strip_suffix<N: ToRelativeName>(
&mut self,
base: &N,
) -> Result<(), StripSuffixError>where
Octs: Truncate,
Strips the suffix base
from the domain name.
This will fail if base
isn’t actually a suffix, i.e., if
ends_with
doesn’t return true
.
Trait Implementations§
source§impl<Octs> AsRef<Octs> for RelativeName<Octs>
impl<Octs> AsRef<Octs> for RelativeName<Octs>
source§impl<Octs> AsRef<RelativeName<[u8]>> for RelativeName<Octs>
impl<Octs> AsRef<RelativeName<[u8]>> for RelativeName<Octs>
source§fn as_ref(&self) -> &RelativeName<[u8]>
fn as_ref(&self) -> &RelativeName<[u8]>
source§impl<Octs> Borrow<RelativeName<[u8]>> for RelativeName<Octs>
impl<Octs> Borrow<RelativeName<[u8]>> for RelativeName<Octs>
Borrow a relative domain name.
Containers holding an owned RelativeName<_>
may be queried with name
over a slice. This Borrow<_>
impl supports user code querying containers
with compatible-but-different types like the following example:
use std::collections::HashMap;
use domain::base::RelativeName;
fn get_description(
hash: &HashMap<RelativeName<Vec<u8>>, String>
) -> Option<&str> {
let lookup_name: &RelativeName<[u8]> =
RelativeName::from_slice(b"\x03ftp").unwrap();
hash.get(lookup_name).map(|x| x.as_ref())
}
source§impl<Octs: Clone + ?Sized> Clone for RelativeName<Octs>
impl<Octs: Clone + ?Sized> Clone for RelativeName<Octs>
source§fn clone(&self) -> RelativeName<Octs>
fn clone(&self) -> RelativeName<Octs>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<Octets> From<RelativeName<Octets>> for UncertainName<Octets>
impl<Octets> From<RelativeName<Octets>> for UncertainName<Octets>
source§fn from(src: RelativeName<Octets>) -> Self
fn from(src: RelativeName<Octets>) -> Self
source§impl<Octs> FromStr for RelativeName<Octs>where
Octs: FromBuilder,
<Octs as FromBuilder>::Builder: EmptyBuilder + FreezeBuilder<Octets = Octs> + AsRef<[u8]> + AsMut<[u8]>,
impl<Octs> FromStr for RelativeName<Octs>where
Octs: FromBuilder,
<Octs as FromBuilder>::Builder: EmptyBuilder + FreezeBuilder<Octets = Octs> + AsRef<[u8]> + AsMut<[u8]>,
source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a string into an absolute domain name.
The name needs to be formatted in representation format, i.e., as a sequence of labels separated by dots. If Internationalized Domain Name (IDN) labels are to be used, these need to be given in punycode encoded form.
This implementation will error if the name ends in a dot since that indicates an absolute name.
source§type Err = RelativeFromStrError
type Err = RelativeFromStrError
source§impl<'a, Octs> IntoIterator for &'a RelativeName<Octs>
impl<'a, Octs> IntoIterator for &'a RelativeName<Octs>
source§impl<Octs, SrcOcts> OctetsFrom<RelativeName<SrcOcts>> for RelativeName<Octs>where
Octs: OctetsFrom<SrcOcts>,
impl<Octs, SrcOcts> OctetsFrom<RelativeName<SrcOcts>> for RelativeName<Octs>where
Octs: OctetsFrom<SrcOcts>,
type Error = <Octs as OctetsFrom<SrcOcts>>::Error
source§fn try_octets_from(source: RelativeName<SrcOcts>) -> Result<Self, Self::Error>
fn try_octets_from(source: RelativeName<SrcOcts>) -> Result<Self, Self::Error>
source§impl<Octs, N> PartialEq<N> for RelativeName<Octs>
impl<Octs, N> PartialEq<N> for RelativeName<Octs>
source§impl<Octs, N> PartialOrd<N> for RelativeName<Octs>
impl<Octs, N> PartialOrd<N> for RelativeName<Octs>
source§impl<Octs> ToLabelIter for RelativeName<Octs>
impl<Octs> ToLabelIter for RelativeName<Octs>
source§type LabelIter<'a> = NameIter<'a>
where
Octs: 'a
type LabelIter<'a> = NameIter<'a> where Octs: 'a
source§fn iter_labels(&self) -> Self::LabelIter<'_>
fn iter_labels(&self) -> Self::LabelIter<'_>
source§fn compose_len(&self) -> u16
fn compose_len(&self) -> u16
source§fn starts_with<N: ToLabelIter + ?Sized>(&self, base: &N) -> bool
fn starts_with<N: ToLabelIter + ?Sized>(&self, base: &N) -> bool
base
is a prefix of self
.source§impl<Octs: AsRef<[u8]> + ?Sized> ToRelativeName for RelativeName<Octs>
impl<Octs: AsRef<[u8]> + ?Sized> ToRelativeName for RelativeName<Octs>
source§fn as_flat_slice(&self) -> Option<&[u8]>
fn as_flat_slice(&self) -> Option<&[u8]>
source§fn try_to_relative_name<Octets>(
&self,
) -> Result<RelativeName<Octets>, BuilderAppendError<Octets>>
fn try_to_relative_name<Octets>( &self, ) -> Result<RelativeName<Octets>, BuilderAppendError<Octets>>
source§fn to_relative_name<Octets>(&self) -> RelativeName<Octets>where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: OctetsBuilder<AppendError = Infallible> + EmptyBuilder,
fn to_relative_name<Octets>(&self) -> RelativeName<Octets>where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: OctetsBuilder<AppendError = Infallible> + EmptyBuilder,
source§fn try_to_canonical_relative_name<Octets>(
&self,
) -> Result<RelativeName<Octets>, BuilderAppendError<Octets>>
fn try_to_canonical_relative_name<Octets>( &self, ) -> Result<RelativeName<Octets>, BuilderAppendError<Octets>>
source§fn to_canonical_relative_name<Octets>(&self) -> RelativeName<Octets>where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: OctetsBuilder<AppendError = Infallible> + EmptyBuilder,
fn to_canonical_relative_name<Octets>(&self) -> RelativeName<Octets>where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: OctetsBuilder<AppendError = Infallible> + EmptyBuilder,
fn compose<Target: OctetsBuilder + ?Sized>( &self, target: &mut Target, ) -> Result<(), Target::AppendError>
fn compose_canonical<Target: OctetsBuilder + ?Sized>( &self, target: &mut Target, ) -> Result<(), Target::AppendError>
source§fn to_cow(&self) -> RelativeName<Cow<'_, [u8]>>
fn to_cow(&self) -> RelativeName<Cow<'_, [u8]>>
source§fn to_bytes(&self) -> RelativeName<Bytes>
fn to_bytes(&self) -> RelativeName<Bytes>
impl<Octs: AsRef<[u8]> + ?Sized> Eq for RelativeName<Octs>
Auto Trait Implementations§
impl<Octs> Freeze for RelativeName<Octs>
impl<Octs> RefUnwindSafe for RelativeName<Octs>where
Octs: RefUnwindSafe + ?Sized,
impl<Octs> Send for RelativeName<Octs>
impl<Octs> Sync for RelativeName<Octs>
impl<Octs> Unpin for RelativeName<Octs>
impl<Octs> UnwindSafe for RelativeName<Octs>where
Octs: UnwindSafe + ?Sized,
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)