use std::any::{Any, TypeId};
pub trait Node: Any {
}
impl dyn Node {
pub fn is<T: Any + 'static>(&self) -> bool {
let t = TypeId::of::<T>();
let concrete = self.type_id();
t == concrete
}
pub fn downcast<T>(&mut self) -> Option<&mut T>
where
T: Node + 'static,
{
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut dyn Node as *mut T)) }
} else {
None
}
}
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: Any + 'static,
{
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn Node as *const T)) }
} else {
None
}
}
}
trait Downcast {
fn as_any (self: &'_ Self)
-> &'_ dyn Any
where
Self : 'static,
;
}
impl<T: Node> Downcast for T {
fn as_any (self: &'_ Self)
-> &'_ dyn Any
where
Self : 'static,
{
self
}
}
impl<T: 'static> Node for T {
}