Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra
2021-09-01 16:19:01 +02:00
co-authored by Nathan Sobo
parent c8d67f428d
commit 20e18ac3ad
5 changed files with 206 additions and 134 deletions
+11 -2
View File
@@ -274,9 +274,9 @@ impl<T: Element> Default for Lifecycle<T> {
}
impl ElementBox {
pub fn metadata(&self) -> Option<&dyn Any> {
pub fn metadata<T: 'static>(&self) -> Option<&T> {
let element = unsafe { &*self.0.element.as_ptr() };
element.metadata()
element.metadata().and_then(|m| m.downcast_ref())
}
}
@@ -331,6 +331,15 @@ impl ElementRc {
value
}
pub fn with_metadata<T, F, R>(&self, f: F) -> R
where
T: 'static,
F: FnOnce(Option<&T>) -> R,
{
let element = self.element.borrow();
f(element.metadata().and_then(|m| m.downcast_ref()))
}
}
pub trait ParentElement<'a>: Extend<ElementBox> + Sized {