wsdom_core/js_cast.rs
1use crate::js::value::JsValue;
2
3/// For converting between JavaScript types.
4///
5/// Note that most class types generated by WSDOM also comes with `Into` and `AsRef` impls for converting to their ancestors in the inheritance chain.
6pub trait JsCast
7where
8 Self: AsRef<JsValue> + Into<JsValue>,
9{
10 // fn instanceof(val: &JsValue) -> bool;
11 fn unchecked_from_js(val: JsValue) -> Self;
12 fn unchecked_from_js_ref(val: &JsValue) -> &Self;
13
14 // fn has_type<T>(&self) -> bool
15 // where T: JsCast { ... }
16 // fn dyn_into<T>(self) -> Result<T, Self>
17 // where T: JsCast { ... }
18 // fn dyn_ref<T>(&self) -> Option<&T>
19 // where T: JsCast { ... }
20 fn unchecked_into<T>(self) -> T
21 where
22 T: JsCast,
23 {
24 T::unchecked_from_js(self.into())
25 }
26 fn unchecked_ref<T>(&self) -> &T
27 where
28 T: JsCast,
29 {
30 T::unchecked_from_js_ref(self.as_ref())
31 }
32 // fn is_instance_of<T>(&self) -> bool
33 // where T: JsCast { ... }
34 // fn is_type_of(val: &JsValue) -> bool { ... }
35}