Skip to main content

MqttBinary

Struct MqttBinary 

Source
pub struct MqttBinary<'b>(/* private fields */);
Expand description

Arbitrary binary data with a length less than or equal to MqttBinary::MAX_LENGTH (u16::MAX). Exceeding this size ultimately leads to malformed packets.

§Examples

use rust_mqtt::Bytes;
use rust_mqtt::types::{MqttBinary, MqttString, TooLargeToEncode};

let slice = [0x00; MqttBinary::MAX_LENGTH];
let too_long = [0x00; MqttBinary::MAX_LENGTH + 1];

let b = MqttBinary::from_slice(&slice)?;
assert_eq!(b.as_bytes(), &slice);
assert!(MqttBinary::from_slice(&too_long).is_err());

let b = MqttBinary::from_bytes(Bytes::Borrowed(&slice))?;
assert_eq!(b.as_bytes(), &slice);
assert!(MqttBinary::from_bytes(Bytes::Borrowed(&too_long)).is_err());

let from_slice_unchecked = MqttBinary::from_slice_unchecked(&slice);
assert_eq!(from_slice_unchecked.as_bytes(), &slice);

let from_bytes_unchecked = MqttBinary::from_bytes_unchecked(Bytes::Borrowed(&slice));
assert_eq!(from_bytes_unchecked.as_bytes(), &slice);

Implementations§

Source§

impl<'b> MqttBinary<'b>

Source

pub const MAX_LENGTH: usize

The maximum length of binary data so that it can be encoded. This value is limited by the 2-byte length field.

Source

pub fn from_bytes(bytes: Bytes<'b>) -> Result<Self, TooLargeToEncode>

Converts Bytes into MqttBinary by checking for the max length of MqttBinary::MAX_LENGTH.

§Errors

Returns TooLargeToEncode if bytes’ length exceeds MqttBinary::MAX_LENGTH.

Source

pub const fn from_slice(slice: &'b [u8]) -> Result<Self, TooLargeToEncode>

Converts a slice into MqttBinary by cloning the reference and checking for the max length of MqttBinary::MAX_LENGTH.

§Errors

Returns TooLargeToEncode if slice’s length exceeds MqttBinary::MAX_LENGTH.

Source

pub const fn from_bytes_unchecked(bytes: Bytes<'b>) -> Self

Converts Bytes into MqttBinary without checking for the max length of MqttBinary::MAX_LENGTH.

§Invariants

The length of the slice parameter in bytes is less than or equal to MqttBinary::MAX_LENGTH.

§Panics

In debug builds, this function will panic if the bytes’ length is greater than MqttBinary::MAX_LENGTH.

Source

pub const fn from_slice_unchecked(slice: &'b [u8]) -> Self

Converts a slice into MqttBinary without checking for the max length of MqttBinary::MAX_LENGTH.

§Invariants

The length of the slice parameter in bytes is less than or equal to MqttBinary::MAX_LENGTH.

§Panics

In debug builds, this function will panic if the slice’s length is greater than MqttBinary::MAX_LENGTH.

Source

pub const fn len(&self) -> u16

Returns the length of the underlying data.

Source

pub const fn is_empty(&self) -> bool

Returns whether the underlying data is empty.

Source

pub const fn as_bytes(&self) -> &[u8]

Returns the underlying bytes as &[u8]

Source

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

Delegates to Bytes::as_borrowed.

Trait Implementations§

Source§

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

Source§

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

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

impl<'b> Clone for MqttBinary<'b>

Source§

fn clone(&self) -> MqttBinary<'b>

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 MqttBinary<'_>

Source§

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

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

impl<'b> Default for MqttBinary<'b>

Source§

fn default() -> MqttBinary<'b>

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<'b> PartialEq for MqttBinary<'b>

Source§

fn eq(&self, other: &MqttBinary<'b>) -> 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<'b> TryFrom<&'b [u8]> for MqttBinary<'b>

Source§

type Error = TooLargeToEncode

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

fn try_from(value: &'b [u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'b> TryFrom<&'b str> for MqttBinary<'b>

Source§

type Error = TooLargeToEncode

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

fn try_from(value: &'b 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<'b> Eq for MqttBinary<'b>

Source§

impl<'b> StructuralPartialEq for MqttBinary<'b>

Auto Trait Implementations§

§

impl<'b> Freeze for MqttBinary<'b>

§

impl<'b> RefUnwindSafe for MqttBinary<'b>

§

impl<'b> Send for MqttBinary<'b>

§

impl<'b> Sync for MqttBinary<'b>

§

impl<'b> Unpin for MqttBinary<'b>

§

impl<'b> UnsafeUnpin for MqttBinary<'b>

§

impl<'b> UnwindSafe for MqttBinary<'b>

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.