Add direct downcast methods to AnyView

This commit is contained in:
Alexandre Bury 2019-04-08 17:44:36 -07:00
parent 070508f0cf
commit 158bc65c33

View File

@ -43,3 +43,26 @@ impl<T: View> AnyView for T {
self self
} }
} }
impl dyn AnyView {
/// Attempts to downcast `self` to a concrete type.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
self.as_any().downcast_ref()
}
/// Attempts to downcast `self` to a concrete type.
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
self.as_any_mut().downcast_mut()
}
/// Attempts to downcast `Box<Self>` to a concrete type.
pub fn downcast<T: Any>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
// Do the check here + unwrap, so the error
// value is `Self` and not `dyn Any`.
if self.as_any().is::<T>() {
Ok(self.as_boxed_any().downcast().unwrap())
} else {
Err(self)
}
}
}