1use crate::{
2 common::cast::Cast,
3 mem::{manager::Dealloc, ref_::Ref},
4};
5
6use super::{any::Any, any_internal::AnyInternal, ref_cast::RefCast, value_cast::ValueCast};
7
8pub trait AnyCast<D: Dealloc>: Sized {
9 unsafe fn has_same_type(any_internal: u64) -> bool;
10 unsafe fn move_to_any_internal(self) -> u64;
11 unsafe fn from_any_internal(any_internal: u64) -> Self;
12 #[inline(always)]
14 fn move_to_any(self) -> Any<D> {
15 unsafe { Any::from_internal(AnyInternal::new(self.move_to_any_internal())) }
16 }
17}
18
19impl<D: Dealloc, T: ValueCast> AnyCast<D> for T
20where
21 u64: Cast<T>,
22{
23 #[inline(always)]
24 unsafe fn has_same_type(any_internal: u64) -> bool {
25 T::SUBSET.has(any_internal)
26 }
27 #[inline(always)]
28 unsafe fn move_to_any_internal(self) -> u64 {
29 T::SUBSET.typed_value_to_subset_value(self)
30 }
31 #[inline(always)]
32 unsafe fn from_any_internal(any_internal: u64) -> Self {
33 T::SUBSET.subset_value_to_typed_value(any_internal)
34 }
35}
36
37impl<D: Dealloc, T: RefCast<D>> AnyCast<D> for Ref<T, D> {
38 #[inline(always)]
39 unsafe fn has_same_type(any_internal: u64) -> bool {
40 T::REF_SUBSET.has(any_internal)
41 }
42 #[inline(always)]
43 unsafe fn move_to_any_internal(self) -> u64 {
44 T::REF_SUBSET.typed_value_to_subset_value(self.move_to_internal())
45 }
46 #[inline(always)]
47 unsafe fn from_any_internal(any_internal: u64) -> Self {
48 Self::from_internal(T::REF_SUBSET.subset_value_to_typed_value(any_internal))
49 }
50}
51
52#[cfg(test)]
53mod test {
54 use wasm_bindgen_test::wasm_bindgen_test;
55
56 use crate::{js::any::Any, mem::global::Global};
57
58 use super::AnyCast;
59
60 #[wasm_bindgen_test]
61 #[test]
62 fn test() {
63 let _x: Any<Global> = 1.0.move_to_any();
64 }
65}