1use std::any::{Any, TypeId};
2pub trait Node: Any {
3
4}
5
6impl dyn Node {
7 pub fn is<T: Any + 'static>(&self) -> bool {
9 let t = TypeId::of::<T>();
11
12 let concrete = self.type_id();
14
15 t == concrete
17 }
18
19 pub fn downcast<T>(&mut self) -> Option<&mut T>
21 where
22 T: Node + 'static,
23 {
24 if self.is::<T>() {
25 unsafe { Some(&mut *(self as *mut dyn Node as *mut T)) }
26 } else {
27 None
28 }
29 }
30
31 pub fn downcast_ref<T>(&self) -> Option<&T>
32 where
33 T: Any + 'static,
34 {
35 if self.is::<T>() {
36 unsafe { Some(&*(self as *const dyn Node as *const T)) }
37 } else {
38 None
39 }
40 }
41}
42
43trait Downcast {
44 fn as_any (self: &'_ Self)
45 -> &'_ dyn Any
46 where
47 Self : 'static,
48 ;
49}
50
51impl<T: Node> Downcast for T {
52 fn as_any (self: &'_ Self)
53 -> &'_ dyn Any
54 where
55 Self : 'static,
56 {
57 self
58 }
59}
60
61impl<T: 'static> Node for T {
62
63}