pub struct MqttString<'s>(/* private fields */);Expand description
Arbitrary UTF-8 encoded string with a length in bytes less than or equal to
MqttString::MAX_LENGTH (u16::MAX) and no null characters.
Exceeding this size ultimately leads to malformed packets.
§Examples
use rust_mqtt::types::{MqttBinary, MqttString, MqttStringError};
let bytes = [b'a'; MqttString::MAX_LENGTH];
let too_long = [b'a'; MqttString::MAX_LENGTH + 1];
let null_character = "hi\0there";
let slice = core::str::from_utf8(&bytes)?;
let too_long = core::str::from_utf8(&too_long)?;
let b = MqttBinary::from_slice(&bytes)?;
let s = MqttString::from_utf8_binary(b)?;
assert_eq!(s.as_str(), slice);
let b = MqttBinary::from_slice(null_character.as_bytes())?;
assert_eq!(MqttString::from_utf8_binary(b).unwrap_err(), MqttStringError::NullCharacter);
let s = MqttString::from_str(slice)?;
assert_eq!(s.as_str(), slice);
assert_eq!(MqttString::from_str(too_long).unwrap_err(), MqttStringError::TooLargeToEncode);
assert_eq!(MqttString::from_str(&null_character).unwrap_err(), MqttStringError::NullCharacter);
let s = MqttString::from_str_unchecked(slice);
assert_eq!(s.as_str(), slice);
let b = MqttBinary::from_slice_unchecked(slice.as_bytes());
let s = unsafe { MqttString::from_utf8_binary_unchecked(b) };
assert_eq!(s.as_str(), slice);
Implementations§
Source§impl<'s> MqttString<'s>
impl<'s> MqttString<'s>
Sourcepub const MAX_LENGTH: usize = MqttBinary::MAX_LENGTH
pub const MAX_LENGTH: usize = MqttBinary::MAX_LENGTH
The maximum length of a string in bytes so that it can be encoded. This value is limited by the 2-byte length field.
Sourcepub fn from_utf8_binary(b: MqttBinary<'s>) -> Result<Self, MqttStringError>
pub fn from_utf8_binary(b: MqttBinary<'s>) -> Result<Self, MqttStringError>
Converts MqttBinary into MqttString by checking for null characters and valid UTF-8.
Valid length is guaranteed by MqttBinary’s invariant.
§Errors
MqttStringError::Utf8Errorifbis not valid UTF-8.MqttStringError::NullCharacterifbcontains an ASCII\0character.MqttStringError::TooLargeToEncodeifb’s length exceedsMqttString::MAX_LENGTH.
Sourcepub const unsafe fn from_utf8_binary_unchecked(b: MqttBinary<'s>) -> Self
pub const unsafe fn from_utf8_binary_unchecked(b: MqttBinary<'s>) -> Self
Converts MqttBinary into MqttString without checking for null characters or valid UTF-8.
Valid length is guaranteed by MqttBinary’s invariant.
§Safety
The binary passed in must be valid UTF-8.
§Invariants
The binary data does not contain any null characters.
§Panics
In debug builds, this function will panic if the binary contains a null character or is not valid UTF-8.
Sourcepub const fn from_str(s: &'s str) -> Result<Self, MqttStringError>
pub const fn from_str(s: &'s str) -> Result<Self, MqttStringError>
Converts a string slice into MqttString by checking for null characters and the max
length of MqttString::MAX_LENGTH.
§Errors
MqttStringError::NullCharacterifscontains an ASCII\0character.MqttStringError::TooLargeToEncodeifs’ length exceedsMqttString::MAX_LENGTH.
Sourcepub const fn from_str_unchecked(s: &'s str) -> Self
pub const fn from_str_unchecked(s: &'s str) -> Self
Converts a string slice into MqttString without checking for null characters or the max
length of MqttString::MAX_LENGTH.
§Invariants
The length of the string slice must be less than or equal to MqttString::MAX_LENGTH. The
string must not contain any null characters.
§Panics
In debug builds, this function will panic if the slice contains a null character or its length is greater
than MqttString::MAX_LENGTH.
Sourcepub const fn as_borrowed(&'s self) -> Self
pub const fn as_borrowed(&'s self) -> Self
Delegates to crate::Bytes::as_borrowed.
Trait Implementations§
Source§impl<'t> AsRef<MqttString<'t>> for TopicFilter<'t>
impl<'t> AsRef<MqttString<'t>> for TopicFilter<'t>
Source§fn as_ref(&self) -> &MqttString<'t>
fn as_ref(&self) -> &MqttString<'t>
Source§impl<'t> AsRef<MqttString<'t>> for TopicName<'t>
impl<'t> AsRef<MqttString<'t>> for TopicName<'t>
Source§fn as_ref(&self) -> &MqttString<'t>
fn as_ref(&self) -> &MqttString<'t>
Source§impl AsRef<str> for MqttString<'_>
impl AsRef<str> for MqttString<'_>
Source§impl<'s> Clone for MqttString<'s>
impl<'s> Clone for MqttString<'s>
Source§fn clone(&self) -> MqttString<'s>
fn clone(&self) -> MqttString<'s>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more