Skip to main content

wasm_bindgen/rt/
marker.rs

1/// Marker trait for types that support `#[wasm_bindgen(constructor)]`.
2#[cfg_attr(
3    wbg_diagnostic,
4    diagnostic::on_unimplemented(
5        message = "JavaScript constructors are not supported for `{Self}`",
6        label = "this function cannot be the constructor of `{Self}`",
7        note = "`#[wasm_bindgen(constructor)]` is only supported for `struct`s and cannot be used for `enum`s.",
8        note = "Consider removing the `constructor` option and using a regular static method instead."
9    )
10)]
11pub trait SupportsConstructor {}
12pub struct CheckSupportsConstructor<T: SupportsConstructor>(T);
13
14/// Marker trait for types that support `#[wasm_bindgen(getter)]` or
15/// `#[wasm_bindgen(Setter)]` on instance methods.
16#[cfg_attr(
17    wbg_diagnostic,
18    diagnostic::on_unimplemented(
19        message = "JavaScript instance getters and setters are not supported for `{Self}`",
20        label = "this method cannot be a getter or setter for `{Self}`",
21        note = "`#[wasm_bindgen(getter)]` and `#[wasm_bindgen(setter)]` are only supported for `struct`s and cannot be used for `enum`s.",
22    )
23)]
24pub trait SupportsInstanceProperty {}
25pub struct CheckSupportsInstanceProperty<T: SupportsInstanceProperty>(T);
26
27/// Marker trait for types that support `#[wasm_bindgen(getter)]` or
28/// `#[wasm_bindgen(Setter)]` on static methods.
29#[cfg_attr(
30    wbg_diagnostic,
31    diagnostic::on_unimplemented(
32        message = "JavaScript static getters and setters are not supported for `{Self}`",
33        label = "this static function cannot be a static getter or setter on `{Self}`",
34        note = "`#[wasm_bindgen(getter)]` and `#[wasm_bindgen(setter)]` are only supported for `struct`s and cannot be used for `enum`s.",
35    )
36)]
37pub trait SupportsStaticProperty {}
38pub struct CheckSupportsStaticProperty<T: SupportsStaticProperty>(T);
39
40#[cfg(all(
41    feature = "std",
42    all(target_family = "wasm", not(target_os = "wasi")),
43    panic = "unwind"
44))]
45use core::panic::UnwindSafe;
46
47/// Marker trait for types that are UnwindSafe only when building with panic unwind
48pub trait MaybeUnwindSafe {}
49
50#[cfg(all(
51    feature = "std",
52    all(target_family = "wasm", not(target_os = "wasi")),
53    panic = "unwind"
54))]
55impl<T: UnwindSafe + ?Sized> MaybeUnwindSafe for T {}
56
57#[cfg(not(all(
58    feature = "std",
59    all(target_family = "wasm", not(target_os = "wasi")),
60    panic = "unwind"
61)))]
62impl<T: ?Sized> MaybeUnwindSafe for T {}
63
64/// Private marker trait for erasable generics - types with this trait have the same
65/// repr for all generic param values, and can therefore be transmuted on
66/// the singular Repr type representation on ABI boundaries.
67///
68/// # Safety
69/// This type must only be implemented on types known to be repr equivalent
70/// to their Repr type.
71// #[cfg_attr(
72//     wbg_diagnostic,
73//     diagnostic::on_unimplemented(
74//         label = "generic parameter is not a valid Wasm Bindgen ErasableGeneric type",
75//         note = "\nRecommendation: Add the direct `: wasm_bindgen::JsGeneric` convenience trait bound for JsValue generics, instead of `ErasableGeneric`.\n",
76//     )
77// )]
78pub unsafe trait ErasableGeneric {
79    /// The singular concrete type that all generic variants can be transmuted on
80    type Repr: 'static;
81}
82
83unsafe impl<T: ErasableGeneric> ErasableGeneric for &mut T {
84    type Repr = &'static mut T::Repr;
85}
86
87unsafe impl<T: ErasableGeneric> ErasableGeneric for &T {
88    type Repr = &'static T::Repr;
89}
90
91/// Trait bound marker for types that are passed as an own generic type.
92/// Encapsulating the ErasableGeneric invariant that must be maintained, that
93/// the repr of the type is the type of the concrete target type repr.
94/// This is useful to provide simple debuggable trait bounds for codegen.
95#[cfg_attr(
96    wbg_diagnostic,
97    diagnostic::on_unimplemented(
98        message = "Unable to call function, since the concrete generic argument or return value cannot be type-erased into the expected generic repr type for the function",
99        label = "passed concrete generic type does not match the expected generic repr type",
100        note = "Make sure that all erasable generic parameters satisfy the trait bound `ErasableGeneric` with the correct repr. Wasm Bindgen generic parameters and return values for functions are defined to work for specific type-erasable generic repr types only.",
101    )
102)]
103pub trait ErasableGenericOwn<ConcreteTarget>: ErasableGeneric {}
104
105impl<T, ConcreteTarget> ErasableGenericOwn<ConcreteTarget> for T
106where
107    ConcreteTarget: ErasableGeneric,
108    T: ErasableGeneric<Repr = <ConcreteTarget as ErasableGeneric>::Repr>,
109{
110}
111
112/// Trait bound marker for types that are passed as a borrowed generic type.
113/// Encapsulating the ErasableGeneric invariant that must be maintained, that
114/// the repr of the type is the type of the concrete target type repr.
115/// This is useful to provide simple debuggable trait bounds for codegen.
116#[cfg_attr(
117    wbg_diagnostic,
118    diagnostic::on_unimplemented(
119        message = "Unable to call this function, since the concrete generic argument or return value cannot be type-erased into the expected generic repr type for the function",
120        label = "concrete generic type does not match the expected generic repr type",
121        note = "Make sure that all erasable generic parameters satisfy the trait bound `ErasableGeneric` with the correct repr. Wasm Bindgen generic parameters and return values for functions are defined to work for specific type-erasable generic repr types only.",
122    )
123)]
124pub trait ErasableGenericBorrow<Target: ?Sized> {}
125
126impl<'a, T: ?Sized + 'a, ConcreteTarget: ?Sized + 'static> ErasableGenericBorrow<ConcreteTarget>
127    for T
128where
129    &'static ConcreteTarget: ErasableGeneric,
130    &'a T: ErasableGeneric<Repr = <&'static ConcreteTarget as ErasableGeneric>::Repr>,
131{
132}
133
134/// Trait bound marker for types that are passed as a mutable borrowed generic type.
135/// Encapsulating the ErasableGeneric invariant that must be maintained, that
136/// the repr of the type is the type of the concrete target type repr.
137/// This is useful to provide simple debuggable trait bounds for codegen.
138#[cfg_attr(
139    wbg_diagnostic,
140    diagnostic::on_unimplemented(
141        message = "Unable to call this function, since the concrete generic argument or return value cannot be type-erased into the expected generic repr type for the function",
142        label = "concrete generic type does not match the expected generic repr type",
143        note = "Make sure that all erasable generic parameters satisfy the trait bound `ErasableGeneric` with the correct repr. Wasm Bindgen generic parameters and return values for functions are defined to work for specific type-erasable generic repr types only.",
144    )
145)]
146pub trait ErasableGenericBorrowMut<Target: ?Sized> {}
147
148impl<'a, T: ?Sized + 'a, ConcreteTarget: ?Sized + 'static> ErasableGenericBorrowMut<ConcreteTarget>
149    for T
150where
151    &'static mut ConcreteTarget: ErasableGeneric,
152    &'a mut T: ErasableGeneric<Repr = <&'static mut ConcreteTarget as ErasableGeneric>::Repr>,
153{
154}