Macro attribute_display

Source
macro_rules! attribute_display {
    ($typ:ty) => { ... };
}
Expand description

Implement an AttributeDisplay closure for an Attribute from a RawAttribute and calls add_display_impl with the generated closure.

ยงExamples

use stun_types::attribute::{AttributeType, Attribute, AttributeStaticType, AttributeFromRaw};
use stun_types::attribute::RawAttribute;
use stun_types::message::StunParseError;
#[derive(Debug)]
struct MyAttribute {}
impl AttributeStaticType for MyAttribute {
   const TYPE: AttributeType = AttributeType::new(0x8852);
}
impl Attribute for MyAttribute {
   fn get_type(&self) -> AttributeType {
       Self::TYPE
   }
   fn length(&self) -> u16 {
       0
   }
}
impl AttributeFromRaw<'_> for MyAttribute {
    fn from_raw_ref(raw: &RawAttribute) -> Result<Self, StunParseError>
    where
        Self: Sized,
    {
        raw.check_type_and_len(Self::TYPE, 0..=0)?;
        Ok(Self {})
   }
}
// An Attribute would also implement AttributeWrite but that has been omitted for brevity.
impl std::fmt::Display for MyAttribute {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MyAttribute")
    }
}
stun_types::attribute_display!(MyAttribute);
let attr = RawAttribute::new(MyAttribute::TYPE, &[]);
let display_str = format!("{attr}");
assert_eq!(display_str, "MyAttribute");