Skip to main content

wasm_wave/wasm/
val.rs

1use crate::wasm::{WasmType, WasmTypeKind, WasmValueError};
2use alloc::{borrow::Cow, boxed::Box};
3
4/// The WasmValue trait may be implemented to represent values to be
5/// (de)serialized with WAVE, notably [`value::Value`](crate::value::Value).
6/// The `wasmtime` crate provides an impl for `wasmtime::component::Val`.
7///
8/// The `make_*` and `unwrap_*` methods should be called only for corresponding
9/// [`WasmTypeKind`](crate::wasm::WasmTypeKind)s.
10#[allow(unused_variables)]
11pub trait WasmValue: Clone + Sized {
12    /// A type representing types of these values.
13    type Type: WasmType;
14
15    /// The kind of type of this value.
16    fn kind(&self) -> WasmTypeKind;
17
18    /// Returns a new WasmValue of the given type.
19    /// # Panics
20    /// Panics if the type is not implemented (the trait default).
21    fn make_bool(val: bool) -> Self {
22        unimplemented!()
23    }
24    /// Returns a new WasmValue of the given type.
25    /// # Panics
26    /// Panics if the type is not implemented (the trait default).
27    fn make_s8(val: i8) -> Self {
28        unimplemented!()
29    }
30    /// Returns a new WasmValue of the given type.
31    /// # Panics
32    /// Panics if the type is not implemented (the trait default).
33    fn make_s16(val: i16) -> Self {
34        unimplemented!()
35    }
36    /// Returns a new WasmValue of the given type.
37    /// # Panics
38    /// Panics if the type is not implemented (the trait default).
39    fn make_s32(val: i32) -> Self {
40        unimplemented!()
41    }
42    /// Returns a new WasmValue of the given type.
43    /// # Panics
44    /// Panics if the type is not implemented (the trait default).
45    fn make_s64(val: i64) -> Self {
46        unimplemented!()
47    }
48    /// Returns a new WasmValue of the given type.
49    /// # Panics
50    /// Panics if the type is not implemented (the trait default).
51    fn make_u8(val: u8) -> Self {
52        unimplemented!()
53    }
54    /// Returns a new WasmValue of the given type.
55    /// # Panics
56    /// Panics if the type is not implemented (the trait default).
57    fn make_u16(val: u16) -> Self {
58        unimplemented!()
59    }
60    /// Returns a new WasmValue of the given type.
61    /// # Panics
62    /// Panics if the type is not implemented (the trait default).
63    fn make_u32(val: u32) -> Self {
64        unimplemented!()
65    }
66    /// Returns a new WasmValue of the given type.
67    /// # Panics
68    /// Panics if the type is not implemented (the trait default).
69    fn make_u64(val: u64) -> Self {
70        unimplemented!()
71    }
72    /// Returns a new WasmValue of the given type.
73    ///
74    /// The Rust `f32` type has many distinct NaN bitpatterns, however the
75    /// component-model `f32` type only has a single NaN value, so this
76    /// function does not preserve NaN bitpatterns.
77    ///
78    /// # Panics
79    /// Panics if the type is not implemented (the trait default).
80    fn make_f32(val: f32) -> Self {
81        unimplemented!()
82    }
83    /// Returns a new WasmValue of the given type.
84    ///
85    /// The Rust `f64` type has many distinct NaN bitpatterns, however the
86    /// component-model `f64` type only has a single NaN value, so this
87    /// function does not preserve NaN bitpatterns.
88    ///
89    /// # Panics
90    /// Panics if the type is not implemented (the trait default).
91    fn make_f64(val: f64) -> Self {
92        unimplemented!()
93    }
94    /// Returns a new WasmValue of the given type.
95    /// # Panics
96    /// Panics if the type is not implemented (the trait default).
97    fn make_char(val: char) -> Self {
98        unimplemented!()
99    }
100    /// Returns a new WasmValue of the given type.
101    /// # Panics
102    /// Panics if the type is not implemented (the trait default).
103    fn make_string(val: Cow<str>) -> Self {
104        unimplemented!()
105    }
106    /// Returns a new WasmValue of the given type.
107    /// # Panics
108    /// Panics if the type is not implemented (the trait default).
109    fn make_list(
110        ty: &Self::Type,
111        vals: impl IntoIterator<Item = Self>,
112    ) -> Result<Self, WasmValueError> {
113        unimplemented!()
114    }
115    /// Returns a new WasmValue of the given type.
116    ///
117    /// The fields provided by `fields` are not necessarily sorted; the callee
118    /// should perform sorting itself if needed.
119    ///
120    /// # Panics
121    /// Panics if the type is not implemented (the trait default).
122    fn make_record<'a>(
123        ty: &Self::Type,
124        fields: impl IntoIterator<Item = (&'a str, Self)>,
125    ) -> Result<Self, WasmValueError> {
126        unimplemented!()
127    }
128    /// Returns a new WasmValue of the given type.
129    /// # Panics
130    /// Panics if the type is not implemented (the trait default).
131    fn make_tuple(
132        ty: &Self::Type,
133        vals: impl IntoIterator<Item = Self>,
134    ) -> Result<Self, WasmValueError> {
135        unimplemented!()
136    }
137    /// Returns a new WasmValue of the given type.
138    /// # Panics
139    /// Panics if the type is not implemented (the trait default).
140    fn make_variant(
141        ty: &Self::Type,
142        case: &str,
143        val: Option<Self>,
144    ) -> Result<Self, WasmValueError> {
145        unimplemented!()
146    }
147    /// Returns a new WasmValue of the given type.
148    /// # Panics
149    /// Panics if the type is not implemented (the trait default).
150    fn make_enum(ty: &Self::Type, case: &str) -> Result<Self, WasmValueError> {
151        unimplemented!()
152    }
153    /// Returns a new WasmValue of the given type.
154    /// # Panics
155    /// Panics if the type is not implemented (the trait default).
156    fn make_option(ty: &Self::Type, val: Option<Self>) -> Result<Self, WasmValueError> {
157        unimplemented!()
158    }
159    /// Returns a new WasmValue of the given type.
160    /// # Panics
161    /// Panics if the type is not implemented (the trait default).
162    fn make_result(
163        ty: &Self::Type,
164        val: Result<Option<Self>, Option<Self>>,
165    ) -> Result<Self, WasmValueError> {
166        unimplemented!()
167    }
168    /// Returns a new WasmValue of the given type.
169    ///
170    /// The strings provided by `names` are not necessarily sorted; the callee
171    /// should perform sorting itself if needed.
172    ///
173    /// # Panics
174    /// Panics if the type is not implemented (the trait default).
175    fn make_flags<'a>(
176        ty: &Self::Type,
177        names: impl IntoIterator<Item = &'a str>,
178    ) -> Result<Self, WasmValueError> {
179        unimplemented!()
180    }
181
182    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
183    /// # Panics
184    /// Panics if `self` is not of the right type.
185    fn unwrap_bool(&self) -> bool {
186        unimplemented!()
187    }
188    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
189    /// # Panics
190    /// Panics if `self` is not of the right type.
191    fn unwrap_s8(&self) -> i8 {
192        unimplemented!()
193    }
194    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
195    /// # Panics
196    /// Panics if `self` is not of the right type.
197    fn unwrap_s16(&self) -> i16 {
198        unimplemented!()
199    }
200    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
201    /// # Panics
202    /// Panics if `self` is not of the right type.
203    fn unwrap_s32(&self) -> i32 {
204        unimplemented!()
205    }
206    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
207    /// # Panics
208    /// Panics if `self` is not of the right type.
209    fn unwrap_s64(&self) -> i64 {
210        unimplemented!()
211    }
212    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
213    /// # Panics
214    /// Panics if `self` is not of the right type.
215    fn unwrap_u8(&self) -> u8 {
216        unimplemented!()
217    }
218    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
219    /// # Panics
220    /// Panics if `self` is not of the right type.
221    fn unwrap_u16(&self) -> u16 {
222        unimplemented!()
223    }
224    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
225    /// # Panics
226    /// Panics if `self` is not of the right type.
227    fn unwrap_u32(&self) -> u32 {
228        unimplemented!()
229    }
230    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
231    /// # Panics
232    /// Panics if `self` is not of the right type.
233    fn unwrap_u64(&self) -> u64 {
234        unimplemented!()
235    }
236    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
237    ///
238    /// The Rust `f32` type has many distinct NaN bitpatterns, however the
239    /// component-model `f64` type only has a single NaN value, so this
240    /// function does not preserve NaN bitpatterns.
241    ///
242    /// # Panics
243    /// Panics if `self` is not of the right type.
244    fn unwrap_f32(&self) -> f32 {
245        unimplemented!()
246    }
247    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
248    ///
249    /// The Rust `f64` type has many distinct NaN bitpatterns, however the
250    /// component-model `f64` type only has a single NaN value, so this
251    /// function does not preserve NaN bitpatterns.
252    ///
253    /// # Panics
254    /// Panics if `self` is not of the right type.
255    fn unwrap_f64(&self) -> f64 {
256        unimplemented!()
257    }
258    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
259    /// # Panics
260    /// Panics if `self` is not of the right type.
261    fn unwrap_char(&self) -> char {
262        unimplemented!()
263    }
264    /// Returns the underlying value of the WasmValue, panicking if it's the wrong type.
265    /// # Panics
266    /// Panics if `self` is not of the right type.
267    fn unwrap_string(&self) -> Cow<'_, str> {
268        unimplemented!()
269    }
270    /// Returns an iterator of the element Vals of the list.
271    /// # Panics
272    /// Panics if `self` is not of the right type.
273    fn unwrap_list(&self) -> Box<dyn Iterator<Item = Cow<'_, Self>> + '_> {
274        unimplemented!()
275    }
276    /// Returns an iterator of the field names and Vals of the record.
277    /// # Panics
278    /// Panics if `self` is not of the right type.
279    fn unwrap_record(&self) -> Box<dyn Iterator<Item = (Cow<'_, str>, Cow<'_, Self>)> + '_> {
280        unimplemented!()
281    }
282    /// Returns an iterator of the field Vals of the tuple.
283    /// # Panics
284    /// Panics if `self` is not of the right type.
285    fn unwrap_tuple(&self) -> Box<dyn Iterator<Item = Cow<'_, Self>> + '_> {
286        unimplemented!()
287    }
288    /// Returns the variant case name and optional payload WasmValue of the variant.
289    /// # Panics
290    /// Panics if `self` is not of the right type.
291    fn unwrap_variant(&self) -> (Cow<'_, str>, Option<Cow<'_, Self>>) {
292        unimplemented!()
293    }
294    /// Returns the case name of the enum.
295    /// # Panics
296    /// Panics if `self` is not of the right type.
297    fn unwrap_enum(&self) -> Cow<'_, str> {
298        unimplemented!()
299    }
300    /// Returns the optional WasmValue.
301    /// # Panics
302    /// Panics if `self` is not of the right type.
303    fn unwrap_option(&self) -> Option<Cow<'_, Self>> {
304        unimplemented!()
305    }
306    /// Returns Ok(_) or Err(_) with the optional payload WasmValue.
307    /// # Panics
308    /// Panics if `self` is not of the right type.
309    fn unwrap_result(&self) -> Result<Option<Cow<'_, Self>>, Option<Cow<'_, Self>>> {
310        unimplemented!()
311    }
312    /// Returns an iterator of the names of the flags WasmValue.
313    /// # Panics
314    /// Panics if `self` is not of the right type.
315    fn unwrap_flags(&self) -> Box<dyn Iterator<Item = Cow<'_, str>> + '_> {
316        unimplemented!()
317    }
318}
319
320macro_rules! unwrap_val {
321    ($val:expr, $case:path, $name:expr) => {
322        match $val {
323            $case(v) => v,
324            _ => panic!("called unwrap_{name} on non-{name} value", name = $name),
325        }
326    };
327}
328pub(crate) use unwrap_val;