wry_bindgen/convert/
traits.rs1use crate::encode::{
2 Anchored, BinaryDecode, BinaryEncode, BorrowScope, CallScoped, EncodeTypeDef, JsRef,
3 ThrowingResult,
4};
5use crate::ipc::EncodedData;
6use crate::{JsCast, JsValue};
7use core::mem::ManuallyDrop;
8use core::ops::Deref;
9
10pub trait IntoWasmAbi: BinaryEncode + EncodeTypeDef {
18 #[inline]
19 fn into_abi(self) -> u32
20 where
21 Self: Sized + IntoAbiId,
22 {
23 self.into_abi_id()
24 }
25}
26
27pub trait FromWasmAbi: BinaryDecode + EncodeTypeDef {
30 #[inline]
41 unsafe fn from_abi(js: u32) -> Self
42 where
43 Self: Sized + FromAbiId,
44 {
45 unsafe { Self::from_abi_id(js) }
46 }
47}
48
49pub trait OptionIntoWasmAbi: IntoWasmAbi {}
51
52pub trait OptionFromWasmAbi: FromWasmAbi {}
54
55pub trait WasmAbi {}
57
58pub trait RefFromWasmAbi {
60 #[inline]
67 unsafe fn ref_from_abi(js: u32) -> AbiRef<Self>
68 where
69 Self: Sized + FromAbiId,
70 {
71 AbiRef(ManuallyDrop::new(unsafe { Self::from_abi_id(js) }))
72 }
73}
74
75pub struct AbiRef<T>(ManuallyDrop<T>);
77
78impl<T> Deref for AbiRef<T> {
79 type Target = T;
80
81 #[inline]
82 fn deref(&self) -> &Self::Target {
83 &self.0
84 }
85}
86
87impl<T> AsRef<T> for AbiRef<T> {
88 #[inline]
89 fn as_ref(&self) -> &T {
90 self
91 }
92}
93
94#[doc(hidden)]
95pub trait IntoAbiId {
96 fn into_abi_id(self) -> u32;
97}
98
99#[doc(hidden)]
100pub trait FromAbiId {
101 unsafe fn from_abi_id(js: u32) -> Self;
102}
103
104impl<T> IntoAbiId for T
105where
106 T: AsRef<JsValue>,
107{
108 #[inline]
109 fn into_abi_id(self) -> u32 {
110 let id = self.as_ref().js_ref().into_abi();
111 core::mem::forget(self);
112 id
113 }
114}
115
116impl<T> FromAbiId for T
117where
118 T: JsCast,
119{
120 #[inline]
121 unsafe fn from_abi_id(js: u32) -> Self {
122 T::unchecked_from_js(JsValue::from_ref(JsRef::from_abi(js)))
123 }
124}
125
126pub trait ReturnAbi<S: BorrowScope> {
134 type Wire: EncodeTypeDef;
136}
137
138pub trait ReturnSync: ReturnAbi<CallScoped> {
143 fn return_abi(self, encoder: &mut EncodedData);
145}
146
147impl<T: IntoWasmAbi> ReturnAbi<CallScoped> for T {
148 type Wire = T;
149}
150impl<T: IntoWasmAbi> ReturnSync for T {
151 #[inline]
152 fn return_abi(self, encoder: &mut EncodedData) {
153 self.encode(encoder);
154 }
155}
156
157impl<T, E> ReturnAbi<CallScoped> for Result<T, E>
158where
159 T: BinaryEncode + EncodeTypeDef,
160 E: Into<JsValue>,
161{
162 type Wire = ThrowingResult<T, JsValue>;
163}
164impl<T, E> ReturnSync for Result<T, E>
165where
166 T: BinaryEncode + EncodeTypeDef,
167 E: Into<JsValue>,
168{
169 #[inline]
170 fn return_abi(self, encoder: &mut EncodedData) {
171 ThrowingResult(self.map_err(Into::into)).encode(encoder);
172 }
173}
174
175impl ReturnAbi<CallScoped> for crate::__rt::object_store::ObjectHandle {
179 type Wire = Self;
180}
181impl ReturnSync for crate::__rt::object_store::ObjectHandle {
182 #[inline]
183 fn return_abi(self, encoder: &mut EncodedData) {
184 self.encode(encoder);
185 }
186}
187
188pub trait TryFromJsValue: Sized {
190 fn try_from_js_value(value: JsValue) -> Result<Self, JsValue> {
191 Self::try_from_js_value_ref(&value).ok_or(value)
192 }
193
194 fn try_from_js_value_ref(value: &JsValue) -> Option<Self>;
195}
196
197pub trait ReturnAsync: ReturnAbi<Anchored> {
203 fn into_js_result(self) -> Result<JsValue, JsValue>;
204}
205
206impl<T> ReturnAbi<Anchored> for T
207where
208 T: Into<JsValue> + crate::sys::Promising,
209 <T as crate::sys::Promising>::Resolution: EncodeTypeDef,
210{
211 type Wire = <T as crate::sys::Promising>::Resolution;
212}
213impl<T> ReturnAsync for T
214where
215 T: Into<JsValue> + crate::sys::Promising,
216 <T as crate::sys::Promising>::Resolution: EncodeTypeDef,
217{
218 #[inline]
219 fn into_js_result(self) -> Result<JsValue, JsValue> {
220 Ok(self.into())
221 }
222}
223
224impl<T, E> ReturnAbi<Anchored> for Result<T, E>
225where
226 T: Into<JsValue> + crate::sys::Promising,
227 <T as crate::sys::Promising>::Resolution: EncodeTypeDef,
228 E: Into<JsValue>,
229{
230 type Wire = <T as crate::sys::Promising>::Resolution;
231}
232impl<T, E> ReturnAsync for Result<T, E>
233where
234 T: Into<JsValue> + crate::sys::Promising,
235 <T as crate::sys::Promising>::Resolution: EncodeTypeDef,
236 E: Into<JsValue>,
237{
238 #[inline]
239 fn into_js_result(self) -> Result<JsValue, JsValue> {
240 match self {
241 Ok(value) => Ok(value.into()),
242 Err(error) => Err(error.into()),
243 }
244 }
245}
246
247pub trait FromJsFuture: Sized {
252 fn from_js_future(result: Result<JsValue, JsValue>) -> Self;
253}
254
255impl<T: TryFromJsValue> FromJsFuture for T {
256 #[inline]
257 fn from_js_future(result: Result<JsValue, JsValue>) -> Self {
258 let value = result.expect("async function failed");
259 T::try_from_js_value(value).expect("async function returned incompatible value")
260 }
261}
262
263impl<T: TryFromJsValue, E: From<JsValue>> FromJsFuture for Result<T, E> {
264 #[inline]
265 fn from_js_future(result: Result<JsValue, JsValue>) -> Self {
266 match result {
267 Ok(value) => Ok(
268 T::try_from_js_value(value).expect("async function returned incompatible value")
269 ),
270 Err(error) => Err(E::from(error)),
271 }
272 }
273}
274
275pub trait UpcastFrom<S: ?Sized> {}
277
278pub trait Upcast<T: ?Sized> {
280 #[inline]
281 fn upcast(&self) -> &T
282 where
283 Self: crate::__rt::marker::ErasableGeneric,
284 T: Sized
285 + crate::__rt::marker::ErasableGeneric<
286 Repr = <Self as crate::__rt::marker::ErasableGeneric>::Repr,
287 >,
288 {
289 unsafe { &*(self as *const Self as *const T) }
290 }
291
292 #[inline]
293 fn upcast_into(self) -> T
294 where
295 Self: Sized + crate::__rt::marker::ErasableGeneric,
296 T: Sized
297 + crate::__rt::marker::ErasableGeneric<
298 Repr = <Self as crate::__rt::marker::ErasableGeneric>::Repr,
299 >,
300 {
301 unsafe { core::mem::transmute_copy(&core::mem::ManuallyDrop::new(self)) }
302 }
303}
304
305impl<S, T> Upcast<T> for S
306where
307 T: UpcastFrom<S> + ?Sized,
308 S: ?Sized,
309{
310}
311
312impl<'a, T, Target> UpcastFrom<&'a T> for &'a Target where Target: UpcastFrom<T> {}
313impl<'a, T, Target> UpcastFrom<&'a mut T> for &'a mut Target where Target: UpcastFrom<T> {}
314
315macro_rules! impl_tuple_upcast {
316 ([$($ty:ident)+] [$($target:ident)+]) => {
317 impl<$($ty,)+ $($target,)+> UpcastFrom<($($ty,)+)> for ($($target,)+)
318 where
319 $($ty: JsGeneric,)+
320 $($target: JsGeneric + UpcastFrom<$ty>,)+
321 {
322 }
323
324 impl<$($ty,)+ $($target,)+> UpcastFrom<($($ty,)+)> for crate::sys::JsOption<($($target,)+)>
325 where
326 $($ty: JsGeneric,)+
327 $($target: JsGeneric + UpcastFrom<$ty>,)+
328 {
329 }
330 };
331}
332
333impl_tuple_upcast!([T1][Target1]);
334impl_tuple_upcast!([T1 T2] [Target1 Target2]);
335impl_tuple_upcast!([T1 T2 T3] [Target1 Target2 Target3]);
336impl_tuple_upcast!([T1 T2 T3 T4] [Target1 Target2 Target3 Target4]);
337impl_tuple_upcast!([T1 T2 T3 T4 T5] [Target1 Target2 Target3 Target4 Target5]);
338impl_tuple_upcast!([T1 T2 T3 T4 T5 T6] [Target1 Target2 Target3 Target4 Target5 Target6]);
339impl_tuple_upcast!([T1 T2 T3 T4 T5 T6 T7] [Target1 Target2 Target3 Target4 Target5 Target6 Target7]);
340impl_tuple_upcast!([T1 T2 T3 T4 T5 T6 T7 T8] [Target1 Target2 Target3 Target4 Target5 Target6 Target7 Target8]);
341
342pub trait JsGeneric:
344 crate::__rt::marker::ErasableGeneric<Repr = JsValue>
345 + UpcastFrom<Self>
346 + Upcast<Self>
347 + Upcast<JsValue>
348 + JsCast
349 + crate::__rt::JsRefEncode
350 + crate::__rt::EncodeTypeDef
351 + crate::__rt::BinaryEncode
352 + crate::__rt::BinaryDecode
353 + crate::__rt::BatchableResult
354 + 'static
355{
356}
357
358impl<T> JsGeneric for T where
359 T: crate::__rt::marker::ErasableGeneric<Repr = JsValue>
360 + UpcastFrom<T>
361 + Upcast<JsValue>
362 + JsCast
363 + crate::__rt::JsRefEncode
364 + crate::__rt::EncodeTypeDef
365 + crate::__rt::BinaryEncode
366 + crate::__rt::BinaryDecode
367 + crate::__rt::BatchableResult
368 + 'static
369{
370}
371
372pub trait IntoJsGeneric {
374 type JsCanon: JsGeneric;
375
376 fn to_js(self) -> Self::JsCanon;
377}
378
379impl IntoJsGeneric for JsValue {
380 type JsCanon = JsValue;
381
382 #[inline]
383 fn to_js(self) -> JsValue {
384 self
385 }
386}
387
388impl<T: IntoJsGeneric + Clone> IntoJsGeneric for &T {
389 type JsCanon = T::JsCanon;
390
391 #[inline]
392 fn to_js(self) -> T::JsCanon {
393 self.clone().to_js()
394 }
395}