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>
impl<'b> MqttBinary<'b>
Sourcepub const MAX_LENGTH: usize
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.
Sourcepub fn from_bytes(bytes: Bytes<'b>) -> Result<Self, TooLargeToEncode>
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.
Sourcepub const fn from_slice(slice: &'b [u8]) -> Result<Self, TooLargeToEncode>
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.
Sourcepub const fn from_bytes_unchecked(bytes: Bytes<'b>) -> Self
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.
Sourcepub const fn from_slice_unchecked(slice: &'b [u8]) -> Self
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.
Sourcepub const fn as_borrowed(&'b self) -> Self
pub const fn as_borrowed(&'b self) -> Self
Delegates to Bytes::as_borrowed.
Trait Implementations§
Source§impl AsRef<[u8]> for MqttBinary<'_>
impl AsRef<[u8]> for MqttBinary<'_>
Source§impl<'b> Clone for MqttBinary<'b>
impl<'b> Clone for MqttBinary<'b>
Source§fn clone(&self) -> MqttBinary<'b>
fn clone(&self) -> MqttBinary<'b>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more