Skip to main content

midenc_hir/attributes/attribute/
value.rs

1use core::clone::CloneToUninit;
2
3use crate::any::AsAny;
4
5pub trait AttributeValue: AsAny + CloneToUninit {}
6
7impl<T: AsAny + CloneToUninit> AttributeValue for T {}
8
9impl dyn AttributeValue {
10    /// Returns true if this attribute is an instance of type `T`
11    pub fn is<T: AsAny>(&self) -> bool {
12        self.as_any().is::<T>()
13    }
14
15    /// Attempts to downcast a `&dyn Attribute` to `&T`, if the value is an instance of type `T`.
16    pub fn downcast_ref<T: AsAny>(&self) -> Option<&T> {
17        self.as_any().downcast_ref::<T>()
18    }
19
20    /// Attempts to downcast this attribute value reference to `&mut T`, if the value is an instance
21    /// of type `T`.
22    ///
23    /// Returns `None` if this value is not of type `T`.
24    pub fn downcast_mut<T: AsAny>(&mut self) -> Option<&mut T> {
25        self.as_any_mut().downcast_mut::<T>()
26    }
27}