pub struct CharStr<Octs: ?Sized>(/* private fields */);
Expand description
The content of a DNS character string.
A character string consists of up to 255 octets of binary data. This type wraps an octets sequence. It is guaranteed to always be at most 255 octets in length. It derefs into the underlying octets for working with the content in a familiar way.
As per RFC 1035, character strings compare ignoring ASCII case.
CharStr
’s implementations of the std::cmp
traits act accordingly.
§Presentation format
The text representation of a character string comes in two flavors: Quoted and unquoted. In both cases, the content is interpreted as ASCII text and those octets that aren’t printable ASCII characters, as well as some special symbols, are escaped.
There are two escaping mechanisms: octets that are printable ASCII characters but need to be escaped anyway use what we call a “simple escape” that precedes the character with an ASCII backslash. For all non-printable octets “decimal escapes” are used: an ASCII backslash is followed by three decimal digits representing the decimal value of the octet. A consequence if this is that you cannot escape the digits 0, 1, and 2 using simple escapes and you probably shouldn’t do it for the other digits.
In the quoted form, the content is preceded and followed by exactly one ASCII double quote. Inside, only double quotes, backslashes, and non-printable octets need to be escaped.
In the unquoted form, the content is formatted without any explicit delimiters. Instead, it ends at the first ASCII space or any other delimiting symbol, normally ASCII control characters or an ASCII semicolon which marks the start of a comment. These characters, as well as the double quote, also need to be escaped.
§Display
and FromStr
When formatting a character string using the Display
trait, a variation
of the unquoted form is used where only backslashes and non-printable
octets are escaped. Two methods are available that make it possible to
format the character string in quoted and unquoted formats,
display_quoted
and
display_unquoted
. They return a temporary
value that can be given to a formatting macro.
The FromStr
implementation reads a character string from a Rust string
in the format created by Display
but is more relaxed about escape
sequences – it accepts all of them as long as they lead to a valid
character string.
§Serde support
When the serde
feature is enabled, the type supports serialization and
deserialization. The format differs for human readable and compact
serialization formats.
For human readable formats, character strings are serialized as a newtype
CharStr
wrapping a string with the content as an ASCII string.
Non-printable ASCII characters (i.e., those with a byte value below 32
and above 176) are escaped using the decimal escape sequences as used by
the presentation format. In addition, backslashes are escaped using a
simple escape sequence and thus are doubled.
This leads to a slightly unfortunate situation in serialization formats that in turn use backslash as an escape sequence marker in their own string representation, such as JSON, where a backslash ends up as four backslashes.
When deserializing, escape sequences are excepted for all octets and translated. Non-ASCII characters are not accepted and lead to error.
For compact formats, character strings are serialized as a
newtype CharStr
wrapping a byte array with the content as is.
Implementations§
source§impl<Octs: ?Sized> CharStr<Octs>
impl<Octs: ?Sized> CharStr<Octs>
sourcepub fn from_octets(octets: Octs) -> Result<Self, CharStrError>
pub fn from_octets(octets: Octs) -> Result<Self, CharStrError>
Creates a new character string from an octets value.
Returns succesfully if octets
can indeed be used as a
character string, i.e., it is not longer than 255 bytes.
sourcepub unsafe fn from_octets_unchecked(octets: Octs) -> Selfwhere
Octs: Sized,
pub unsafe fn from_octets_unchecked(octets: Octs) -> Selfwhere
Octs: Sized,
Creates a character string from octets without length check.
§Safety
The caller has to make sure that octets
is at most 255 octets
long. Otherwise, the behavior is undefined.
source§impl CharStr<[u8]>
impl CharStr<[u8]>
sourcepub fn from_slice(slice: &[u8]) -> Result<&Self, CharStrError>
pub fn from_slice(slice: &[u8]) -> Result<&Self, CharStrError>
Creates a character string from an octets slice.
sourcepub fn empty_slice() -> &'static Self
pub fn empty_slice() -> &'static Self
Creates a new empty character string on an octets slice.
sourcepub unsafe fn from_slice_unchecked(slice: &[u8]) -> &Self
pub unsafe fn from_slice_unchecked(slice: &[u8]) -> &Self
Creates a character string from an octets slice without checking.
§Safety
The caller has to make sure that octets
is at most 255 octets
long. Otherwise, the behaviour is undefined.
source§impl<Octs: ?Sized> CharStr<Octs>
impl<Octs: ?Sized> CharStr<Octs>
sourcepub fn builder() -> CharStrBuilder<Octs::Builder>
pub fn builder() -> CharStrBuilder<Octs::Builder>
Creates a new empty builder for this character string type.
sourcepub fn into_builder(self) -> CharStrBuilder<Octs::Builder>
pub fn into_builder(self) -> CharStrBuilder<Octs::Builder>
Converts the character string into a builder.
sourcepub fn into_octets(self) -> Octswhere
Octs: Sized,
pub fn into_octets(self) -> Octswhere
Octs: Sized,
Converts the character string into its underlying octets value.
sourcepub fn for_slice(&self) -> &CharStr<[u8]>
pub fn for_slice(&self) -> &CharStr<[u8]>
Returns a character string atop a slice of the content.
sourcepub fn for_slice_mut(&mut self) -> &mut CharStr<[u8]>
pub fn for_slice_mut(&mut self) -> &mut CharStr<[u8]>
Returns a character string atop a mutable slice of the content.
sourcepub fn as_slice(&self) -> &[u8] ⓘ
pub fn as_slice(&self) -> &[u8] ⓘ
Returns a reference to a slice of the character string’s data.
sourcepub fn as_slice_mut(&mut self) -> &mut [u8] ⓘ
pub fn as_slice_mut(&mut self) -> &mut [u8] ⓘ
Returns a reference to a mutable slice of the character string’s data.
source§impl CharStr<[u8]>
impl CharStr<[u8]>
sourcepub fn parse_slice<'a>(
parser: &mut Parser<'a, [u8]>,
) -> Result<&'a Self, ParseError>
pub fn parse_slice<'a>( parser: &mut Parser<'a, [u8]>, ) -> Result<&'a Self, ParseError>
Parses a character string from a parser atop a slice.
source§impl<Octs: AsRef<[u8]> + ?Sized> CharStr<Octs>
impl<Octs: AsRef<[u8]> + ?Sized> CharStr<Octs>
source§impl<Octs: AsRef<[u8]> + ?Sized> CharStr<Octs>
impl<Octs: AsRef<[u8]> + ?Sized> CharStr<Octs>
sourcepub fn compose_len(&self) -> u16
pub fn compose_len(&self) -> u16
Returns the length of the wire format representation.
sourcepub fn compose<Target: OctetsBuilder + ?Sized>(
&self,
target: &mut Target,
) -> Result<(), Target::AppendError>
pub fn compose<Target: OctetsBuilder + ?Sized>( &self, target: &mut Target, ) -> Result<(), Target::AppendError>
Appends the wire format representation to an octets builder.
source§impl<Octs: AsRef<[u8]> + ?Sized> CharStr<Octs>
impl<Octs: AsRef<[u8]> + ?Sized> CharStr<Octs>
sourcepub fn display_quoted(&self) -> DisplayQuoted<'_>
pub fn display_quoted(&self) -> DisplayQuoted<'_>
Returns an object that formats in quoted presentation format.
The returned object will display the content surrounded by double quotes. It will escape double quotes, backslashes, and non-printable octets only.
sourcepub fn display_unquoted(&self) -> DisplayUnquoted<'_>
pub fn display_unquoted(&self) -> DisplayUnquoted<'_>
Returns an object that formats in unquoted presentation format.
The returned object will display the content without explicit delimiters and escapes space, double quotes, semicolons, backslashes, and non-printable octets.
Trait Implementations§
source§impl<T, U> CanonicalOrd<CharStr<U>> for CharStr<T>
impl<T, U> CanonicalOrd<CharStr<U>> for CharStr<T>
source§fn canonical_cmp(&self, other: &CharStr<U>) -> Ordering
fn canonical_cmp(&self, other: &CharStr<U>) -> Ordering
self
and other
.source§fn canonical_lt(&self, other: &Rhs) -> bool
fn canonical_lt(&self, other: &Rhs) -> bool
self
is canonically less than other
.source§fn canonical_le(&self, other: &Rhs) -> bool
fn canonical_le(&self, other: &Rhs) -> bool
self
is canonically less than or equal to other
.source§fn canonical_gt(&self, other: &Rhs) -> bool
fn canonical_gt(&self, other: &Rhs) -> bool
self
is canonically greater than other
.source§fn canonical_ge(&self, other: &Rhs) -> bool
fn canonical_ge(&self, other: &Rhs) -> bool
self
is canonically greater than or equal to other
.source§impl<Octets> FromStr for CharStr<Octets>where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: OctetsBuilder + FreezeBuilder<Octets = Octets> + EmptyBuilder + AsRef<[u8]>,
impl<Octets> FromStr for CharStr<Octets>where
Octets: FromBuilder,
<Octets as FromBuilder>::Builder: OctetsBuilder + FreezeBuilder<Octets = Octets> + EmptyBuilder + AsRef<[u8]>,
source§impl<Octs, SrcOcts> OctetsFrom<CharStr<SrcOcts>> for CharStr<Octs>where
Octs: OctetsFrom<SrcOcts>,
impl<Octs, SrcOcts> OctetsFrom<CharStr<SrcOcts>> for CharStr<Octs>where
Octs: OctetsFrom<SrcOcts>,
source§impl<T, U> PartialOrd<U> for CharStr<T>
impl<T, U> PartialOrd<U> for CharStr<T>
impl<T: AsRef<[u8]> + ?Sized> Eq for CharStr<T>
Auto Trait Implementations§
impl<Octs> Freeze for CharStr<Octs>
impl<Octs> RefUnwindSafe for CharStr<Octs>where
Octs: RefUnwindSafe + ?Sized,
impl<Octs> Send for CharStr<Octs>
impl<Octs> Sync for CharStr<Octs>
impl<Octs> Unpin for CharStr<Octs>
impl<Octs> UnwindSafe for CharStr<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
)