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