1use core::{future::Future, marker::PhantomData};
2
3use crate::js::value::JsValue;
4
5pub trait JsCast
9where
10 Self: AsRef<JsValue> + Into<JsValue>,
11{
12 fn unchecked_from_js(val: JsValue) -> Self;
14 fn unchecked_from_js_ref(val: &JsValue) -> &Self;
15
16 fn unchecked_into<T>(self) -> T
23 where
24 T: JsCast,
25 {
26 T::unchecked_from_js(self.into())
27 }
28 fn unchecked_ref<T>(&self) -> &T
29 where
30 T: JsCast,
31 {
32 T::unchecked_from_js_ref(self.as_ref())
33 }
34 }
38pin_project_lite::pin_project! {
39 #[derive(Clone, Copy)]
40 pub struct Cast<T,U>{
41 #[pin]
42 pub value: T,
43 pub phantom: PhantomData<U>
44 }
45}
46impl<T: Future<Output: JsCast>, U: JsCast> Future for Cast<T, U> {
47 type Output = U;
48
49 fn poll(
50 self: core::pin::Pin<&mut Self>,
51 cx: &mut core::task::Context<'_>,
52 ) -> core::task::Poll<Self::Output> {
53 self.project().value.poll(cx).map(|a| a.unchecked_into())
54 }
55}