pub trait PropertyValueAccess {
// Required methods
fn as_u8(&self) -> Option<u8>;
fn as_u16(&self) -> Option<u16>;
fn as_u32(&self) -> Option<u32>;
fn as_str(&self) -> Option<&str>;
fn as_bytes(&self) -> Option<&[u8]>;
fn as_key_value(&self) -> Option<(&str, &str)>;
}
Expand description
Trait for accessing property values in a type-safe manner
This trait provides methods to extract values from Property
enum variants
without having to match on each variant explicitly. Methods return Option
to handle cases where the property type doesn’t match the requested type.
§Examples
use mqtt_protocol_core::mqtt;
let prop = mqtt::packet::Property::MessageExpiryInterval(
mqtt::packet::MessageExpiryInterval::new(3600).unwrap()
);
// Extract the u32 value
if let Some(interval) = prop.as_u32() {
println!("Message expires in {} seconds", interval);
}
Required Methods§
Sourcefn as_u8(&self) -> Option<u8>
fn as_u8(&self) -> Option<u8>
Extract u8 value from byte-based properties
Returns Some(u8)
for properties that store single-byte values,
None
for other property types.
Sourcefn as_u16(&self) -> Option<u16>
fn as_u16(&self) -> Option<u16>
Extract u16 value from two-byte properties
Returns Some(u16)
for properties that store two-byte values,
None
for other property types.
Sourcefn as_u32(&self) -> Option<u32>
fn as_u32(&self) -> Option<u32>
Extract u32 value from four-byte properties
Returns Some(u32)
for properties that store four-byte values,
None
for other property types.
Sourcefn as_str(&self) -> Option<&str>
fn as_str(&self) -> Option<&str>
Extract string value from string-based properties
Returns Some(&str)
for properties that store UTF-8 strings,
None
for other property types.
Sourcefn as_bytes(&self) -> Option<&[u8]>
fn as_bytes(&self) -> Option<&[u8]>
Extract binary data from binary-based properties
Returns Some(&[u8])
for properties that store binary data,
None
for other property types.
Sourcefn as_key_value(&self) -> Option<(&str, &str)>
fn as_key_value(&self) -> Option<(&str, &str)>
Extract key-value pair from UserProperty
Returns Some((key, value))
for UserProperty, None
for other property types.