Skip to main content

wry_bindgen_core/
batchable.rs

1use alloc::boxed::Box;
2use alloc::string::String;
3use alloc::vec::Vec;
4
5use wry_bindgen_runtime::wire::{BinaryDecode, BinaryEncode, EncodedData, JsRef, ObjectHandle};
6
7use crate::Runtime;
8
9pub trait BatchableResult: BinaryDecode {
10    fn try_placeholder(_: &mut Runtime) -> Option<Self> {
11        None
12    }
13}
14
15#[derive(Clone, Copy, Default)]
16pub struct RequireFlush;
17
18impl BinaryEncode for RequireFlush {
19    fn encode(self, encoder: &mut EncodedData) {
20        encoder.mark_needs_flush();
21    }
22}
23
24impl BatchableResult for () {
25    fn try_placeholder(_: &mut Runtime) -> Option<Self> {
26        Some(())
27    }
28}
29
30macro_rules! impl_value_type {
31    ($($ty:ty),*) => {
32        $(impl BatchableResult for $ty {})*
33    };
34}
35
36impl_value_type!(
37    bool, char, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, isize, usize, f32, f64, String
38);
39
40impl<T: BinaryDecode> BatchableResult for Option<T> {}
41
42impl<T: BinaryDecode, E: BinaryDecode> BatchableResult for Result<T, E> {}
43
44impl<T: BinaryDecode> BatchableResult for Vec<T> {}
45
46impl<T: BinaryDecode> BatchableResult for Box<[T]> {}
47
48impl BatchableResult for JsRef {
49    fn try_placeholder(batch: &mut Runtime) -> Option<Self> {
50        Some(batch.next_placeholder_ref())
51    }
52}
53
54impl BatchableResult for ObjectHandle {}