wasm_bindgen/
describe.rs

1//! This is an internal module, no stability guarantees are provided. Use at
2//! your own risk.
3
4#![doc(hidden)]
5
6use alloc::boxed::Box;
7use alloc::string::String;
8use alloc::vec::Vec;
9use core::panic::AssertUnwindSafe;
10use core::{mem::MaybeUninit, ptr::NonNull};
11
12use crate::{Clamped, JsCast, JsError, JsValue};
13use cfg_if::cfg_if;
14
15pub use wasm_bindgen_shared::tys::*;
16
17#[inline(always)] // see the wasm-interpreter module
18#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
19pub fn inform(a: u32) {
20    unsafe { super::__wbindgen_describe(a) }
21}
22
23pub trait WasmDescribe {
24    fn describe();
25}
26
27/// Trait for element types to implement WasmDescribe for vectors of
28/// themselves.
29pub trait WasmDescribeVector {
30    fn describe_vector();
31}
32
33macro_rules! simple {
34    ($($t:ident => $d:ident)*) => ($(
35        impl WasmDescribe for $t {
36            #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
37            fn describe() { inform($d) }
38        }
39    )*)
40}
41
42simple! {
43    i8 => I8
44    u8 => U8
45    i16 => I16
46    u16 => U16
47    i32 => I32
48    u32 => U32
49    i64 => I64
50    u64 => U64
51    i128 => I128
52    u128 => U128
53    isize => I32
54    usize => U32
55    f32 => F32
56    f64 => F64
57    bool => BOOLEAN
58    char => CHAR
59    JsValue => EXTERNREF
60}
61
62cfg_if! {
63    if #[cfg(feature = "enable-interning")] {
64        simple! {
65            str => CACHED_STRING
66        }
67
68    } else {
69        simple! {
70            str => STRING
71        }
72    }
73}
74
75impl<T> WasmDescribe for *const T {
76    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
77    fn describe() {
78        inform(U32)
79    }
80}
81
82impl<T> WasmDescribe for *mut T {
83    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
84    fn describe() {
85        inform(U32)
86    }
87}
88
89impl<T> WasmDescribe for NonNull<T> {
90    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
91    fn describe() {
92        inform(NONNULL)
93    }
94}
95
96impl<T: WasmDescribe> WasmDescribe for [T] {
97    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
98    fn describe() {
99        inform(SLICE);
100        T::describe();
101    }
102}
103
104impl<T: WasmDescribe + ?Sized> WasmDescribe for &T {
105    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
106    fn describe() {
107        inform(REF);
108        T::describe();
109    }
110}
111
112impl<T: WasmDescribe + ?Sized> WasmDescribe for &mut T {
113    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
114    fn describe() {
115        inform(REFMUT);
116        T::describe();
117    }
118}
119
120cfg_if! {
121    if #[cfg(feature = "enable-interning")] {
122        simple! {
123            String => CACHED_STRING
124        }
125
126    } else {
127        simple! {
128            String => STRING
129        }
130    }
131}
132
133impl<T: JsCast + WasmDescribe> WasmDescribeVector for T {
134    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
135    fn describe_vector() {
136        inform(VECTOR);
137        T::describe();
138    }
139}
140
141impl<T: WasmDescribeVector> WasmDescribe for Box<[T]> {
142    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
143    fn describe() {
144        T::describe_vector();
145    }
146}
147
148impl<T> WasmDescribe for Vec<T>
149where
150    Box<[T]>: WasmDescribe,
151{
152    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
153    fn describe() {
154        <Box<[T]>>::describe();
155    }
156}
157
158impl<T: WasmDescribe> WasmDescribe for Option<T> {
159    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
160    fn describe() {
161        inform(OPTIONAL);
162        T::describe();
163    }
164}
165
166impl WasmDescribe for () {
167    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
168    fn describe() {
169        inform(UNIT)
170    }
171}
172
173impl<T: WasmDescribe, E: Into<JsValue>> WasmDescribe for Result<T, E> {
174    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
175    fn describe() {
176        inform(RESULT);
177        T::describe();
178    }
179}
180
181impl<T: WasmDescribe> WasmDescribe for MaybeUninit<T> {
182    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
183    fn describe() {
184        T::describe();
185    }
186}
187
188impl<T: WasmDescribe> WasmDescribe for Clamped<T> {
189    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
190    fn describe() {
191        inform(CLAMPED);
192        T::describe();
193    }
194}
195
196impl WasmDescribe for JsError {
197    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
198    fn describe() {
199        JsValue::describe();
200    }
201}
202
203impl<T> WasmDescribe for AssertUnwindSafe<T>
204where
205    T: WasmDescribe,
206{
207    fn describe() {
208        T::describe();
209    }
210}