script_bindings/
inheritance.rs1use std::mem;
8
9use crate::conversions::{DerivedFrom, IDLInterface, get_dom_class};
10use crate::reflector::DomObject;
11use crate::script_runtime::runtime_is_alive;
12
13pub trait Castable: IDLInterface + DomObject + Sized {
16 fn is<T>(&self) -> bool
18 where
19 T: DerivedFrom<Self>,
20 {
21 debug_assert!(
25 runtime_is_alive(),
26 "Attempting to interact with DOM objects after JS runtime has shut down."
27 );
28
29 let class = unsafe { get_dom_class(self.reflector().get_jsobject().get()).unwrap() };
30 T::derives(class)
31 }
32
33 fn upcast<T>(&self) -> &T
35 where
36 T: Castable,
37 Self: DerivedFrom<T>,
38 {
39 unsafe { mem::transmute::<&Self, &T>(self) }
40 }
41
42 fn downcast<T>(&self) -> Option<&T>
44 where
45 T: DerivedFrom<Self>,
46 {
47 if self.is::<T>() {
48 Some(unsafe { mem::transmute::<&Self, &T>(self) })
49 } else {
50 None
51 }
52 }
53}
54
55#[allow(missing_docs)]
56pub trait HasParent {
57 #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
58 type Parent;
59 fn as_parent(&self) -> &Self::Parent;
60}