Skip to main content

MqttString

Struct MqttString 

Source
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>

Source

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.

Source

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
Source

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.

Source

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
Source

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.

Source

pub const fn len(&self) -> u16

Returns the length of the underlying data in bytes.

Source

pub const fn is_empty(&self) -> bool

Returns whether the underlying data is empty.

Source

pub const fn as_str(&self) -> &str

Returns the underlying string as &str

Source

pub const fn as_borrowed(&'s self) -> Self

Trait Implementations§

Source§

impl<'t> AsRef<MqttString<'t>> for TopicFilter<'t>

Source§

fn as_ref(&self) -> &MqttString<'t>

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

impl<'t> AsRef<MqttString<'t>> for TopicName<'t>

Source§

fn as_ref(&self) -> &MqttString<'t>

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

impl AsRef<str> for MqttString<'_>

Source§

fn as_ref(&self) -> &str

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

impl<'s> Clone for MqttString<'s>

Source§

fn clone(&self) -> MqttString<'s>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for MqttString<'_>

Source§

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

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

impl<'s> Default for MqttString<'s>

Source§

fn default() -> MqttString<'s>

Returns the “default value” for a type. Read more
Source§

impl<'b> From<MqttString<'b>> for MqttBinary<'b>

Source§

fn from(value: MqttString<'b>) -> Self

Converts to this type from the input type.
Source§

impl<'t> From<TopicFilter<'t>> for MqttString<'t>

Source§

fn from(value: TopicFilter<'t>) -> Self

Converts to this type from the input type.
Source§

impl<'t> From<TopicName<'t>> for MqttString<'t>

Source§

fn from(value: TopicName<'t>) -> Self

Converts to this type from the input type.
Source§

impl<'s> PartialEq for MqttString<'s>

Source§

fn eq(&self, other: &MqttString<'s>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'s> TryFrom<&'s str> for MqttString<'s>

Source§

type Error = MqttStringError

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

fn try_from(value: &'s str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'s> TryFrom<MqttBinary<'s>> for MqttString<'s>

Source§

type Error = MqttStringError

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

fn try_from(value: MqttBinary<'s>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'s> Eq for MqttString<'s>

Source§

impl<'s> StructuralPartialEq for MqttString<'s>

Auto Trait Implementations§

§

impl<'s> Freeze for MqttString<'s>

§

impl<'s> RefUnwindSafe for MqttString<'s>

§

impl<'s> Send for MqttString<'s>

§

impl<'s> Sync for MqttString<'s>

§

impl<'s> Unpin for MqttString<'s>

§

impl<'s> UnsafeUnpin for MqttString<'s>

§

impl<'s> UnwindSafe for MqttString<'s>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.