wasm_wave/wasm/
val.rs

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