wasmtime_c_api/component/types/
val.rs

1use crate::wasmtime_component_resource_type_t;
2use std::mem::{ManuallyDrop, MaybeUninit};
3use wasmtime::component::types::*;
4
5#[derive(Clone, PartialEq)]
6#[repr(C, u8)]
7pub enum wasmtime_component_valtype_t {
8    Bool,
9    S8,
10    S16,
11    S32,
12    S64,
13    U8,
14    U16,
15    U32,
16    U64,
17    F32,
18    F64,
19    Char,
20    String,
21    List(Box<wasmtime_component_list_type_t>),
22    Record(Box<wasmtime_component_record_type_t>),
23    Tuple(Box<wasmtime_component_tuple_type_t>),
24    Variant(Box<wasmtime_component_variant_type_t>),
25    Enum(Box<wasmtime_component_enum_type_t>),
26    Option(Box<wasmtime_component_option_type_t>),
27    Result(Box<wasmtime_component_result_type_t>),
28    Flags(Box<wasmtime_component_flags_type_t>),
29    Own(Box<wasmtime_component_resource_type_t>),
30    Borrow(Box<wasmtime_component_resource_type_t>),
31    Future(Box<wasmtime_component_future_type_t>),
32    Stream(Box<wasmtime_component_stream_type_t>),
33    ErrorContext,
34}
35
36impl From<Type> for wasmtime_component_valtype_t {
37    fn from(item: Type) -> Self {
38        match item {
39            Type::Bool => Self::Bool,
40            Type::S8 => Self::S8,
41            Type::S16 => Self::S16,
42            Type::S32 => Self::S32,
43            Type::S64 => Self::S64,
44            Type::U8 => Self::U8,
45            Type::U16 => Self::U16,
46            Type::U32 => Self::U32,
47            Type::U64 => Self::U64,
48            Type::Float32 => Self::F32,
49            Type::Float64 => Self::F64,
50            Type::Char => Self::Char,
51            Type::String => Self::String,
52            Type::List(ty) => Self::List(Box::new(ty.into())),
53            Type::Record(ty) => Self::Record(Box::new(ty.into())),
54            Type::Tuple(ty) => Self::Tuple(Box::new(ty.into())),
55            Type::Variant(ty) => Self::Variant(Box::new(ty.into())),
56            Type::Enum(ty) => Self::Enum(Box::new(ty.into())),
57            Type::Option(ty) => Self::Option(Box::new(ty.into())),
58            Type::Result(ty) => Self::Result(Box::new(ty.into())),
59            Type::Flags(ty) => Self::Flags(Box::new(ty.into())),
60            Type::Own(ty) => Self::Own(Box::new(ty.into())),
61            Type::Borrow(ty) => Self::Borrow(Box::new(ty.into())),
62            Type::Future(ty) => Self::Future(Box::new(ty.into())),
63            Type::Stream(ty) => Self::Stream(Box::new(ty.into())),
64            Type::ErrorContext => Self::ErrorContext,
65        }
66    }
67}
68
69#[unsafe(no_mangle)]
70pub extern "C" fn wasmtime_component_valtype_clone(
71    ty: &wasmtime_component_valtype_t,
72    ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
73) {
74    ret.write(ty.clone());
75}
76
77#[unsafe(no_mangle)]
78pub extern "C" fn wasmtime_component_valtype_equal(
79    a: &wasmtime_component_valtype_t,
80    b: &wasmtime_component_valtype_t,
81) -> bool {
82    a == b
83}
84
85#[unsafe(no_mangle)]
86pub unsafe extern "C" fn wasmtime_component_valtype_delete(
87    ret: &mut ManuallyDrop<wasmtime_component_valtype_t>,
88) {
89    unsafe { ManuallyDrop::drop(ret) };
90}
91
92type_wrapper! {
93    #[derive(PartialEq)]
94    pub struct wasmtime_component_list_type_t {
95        pub(crate) ty: List,
96    }
97
98    clone: wasmtime_component_list_type_clone,
99    delete: wasmtime_component_list_type_delete,
100    equal: wasmtime_component_list_type_equal,
101}
102
103#[unsafe(no_mangle)]
104pub extern "C" fn wasmtime_component_list_type_element(
105    ty: &wasmtime_component_list_type_t,
106    type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
107) {
108    type_ret.write(ty.ty.ty().into());
109}
110
111type_wrapper! {
112    #[derive(PartialEq)]
113    pub struct wasmtime_component_record_type_t {
114        pub(crate) ty: Record,
115    }
116    clone: wasmtime_component_record_type_clone,
117    delete: wasmtime_component_record_type_delete,
118    equal: wasmtime_component_record_type_equal,
119}
120
121#[unsafe(no_mangle)]
122pub extern "C" fn wasmtime_component_record_type_field_count(
123    ty: &wasmtime_component_record_type_t,
124) -> usize {
125    ty.ty.fields().len()
126}
127
128#[unsafe(no_mangle)]
129pub extern "C" fn wasmtime_component_record_type_field_nth(
130    ty: &wasmtime_component_record_type_t,
131    nth: usize,
132    name_ret: &mut MaybeUninit<*const u8>,
133    name_len_ret: &mut MaybeUninit<usize>,
134    type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
135) -> bool {
136    match ty.ty.fields().nth(nth) {
137        Some(Field { name, ty }) => {
138            let name: &str = name;
139            name_ret.write(name.as_ptr());
140            name_len_ret.write(name.len());
141            type_ret.write(ty.into());
142            true
143        }
144        None => false,
145    }
146}
147
148type_wrapper! {
149    #[derive(PartialEq)]
150    pub struct wasmtime_component_tuple_type_t {
151        pub(crate) ty: Tuple,
152    }
153    clone: wasmtime_component_tuple_type_clone,
154    delete: wasmtime_component_tuple_type_delete,
155    equal: wasmtime_component_tuple_type_equal,
156}
157
158#[unsafe(no_mangle)]
159pub extern "C" fn wasmtime_component_tuple_type_types_count(
160    ty: &wasmtime_component_tuple_type_t,
161) -> usize {
162    ty.ty.types().len()
163}
164
165#[unsafe(no_mangle)]
166pub extern "C" fn wasmtime_component_tuple_type_types_nth(
167    ty: &wasmtime_component_tuple_type_t,
168    nth: usize,
169    type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
170) -> bool {
171    match ty.ty.types().nth(nth) {
172        Some(item) => {
173            type_ret.write(item.into());
174            true
175        }
176        None => false,
177    }
178}
179
180type_wrapper! {
181    #[derive(PartialEq)]
182    pub struct wasmtime_component_variant_type_t {
183        pub(crate) ty: Variant,
184    }
185    clone: wasmtime_component_variant_type_clone,
186    delete: wasmtime_component_variant_type_delete,
187    equal: wasmtime_component_variant_type_equal,
188}
189
190#[unsafe(no_mangle)]
191pub extern "C" fn wasmtime_component_variant_type_case_count(
192    ty: &wasmtime_component_variant_type_t,
193) -> usize {
194    ty.ty.cases().len()
195}
196
197#[unsafe(no_mangle)]
198pub extern "C" fn wasmtime_component_variant_type_case_nth(
199    ty: &wasmtime_component_variant_type_t,
200    nth: usize,
201    name_ret: &mut MaybeUninit<*const u8>,
202    name_len_ret: &mut MaybeUninit<usize>,
203    has_payload_ret: &mut MaybeUninit<bool>,
204    payload_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
205) -> bool {
206    match ty.ty.cases().nth(nth) {
207        Some(Case { name, ty }) => {
208            let name: &str = name;
209            name_ret.write(name.as_ptr());
210            name_len_ret.write(name.len());
211            match ty {
212                Some(payload) => {
213                    has_payload_ret.write(true);
214                    payload_ret.write(payload.into());
215                }
216                None => {
217                    has_payload_ret.write(false);
218                }
219            }
220            true
221        }
222        None => false,
223    }
224}
225
226type_wrapper! {
227    #[derive(PartialEq)]
228    pub struct wasmtime_component_enum_type_t {
229        pub(crate) ty: Enum,
230    }
231    clone: wasmtime_component_enum_type_clone,
232    delete: wasmtime_component_enum_type_delete,
233    equal: wasmtime_component_enum_type_equal,
234}
235
236#[unsafe(no_mangle)]
237pub extern "C" fn wasmtime_component_enum_type_names_count(
238    ty: &wasmtime_component_enum_type_t,
239) -> usize {
240    ty.ty.names().len()
241}
242
243#[unsafe(no_mangle)]
244pub extern "C" fn wasmtime_component_enum_type_names_nth(
245    ty: &wasmtime_component_enum_type_t,
246    nth: usize,
247    name_ret: &mut MaybeUninit<*const u8>,
248    name_len_ret: &mut MaybeUninit<usize>,
249) -> bool {
250    match ty.ty.names().nth(nth) {
251        Some(name) => {
252            let name: &str = name;
253            name_ret.write(name.as_ptr());
254            name_len_ret.write(name.len());
255            true
256        }
257        None => false,
258    }
259}
260
261type_wrapper! {
262    #[derive(PartialEq)]
263    pub struct wasmtime_component_option_type_t {
264        pub(crate) ty: OptionType,
265    }
266    clone: wasmtime_component_option_type_clone,
267    delete: wasmtime_component_option_type_delete,
268    equal: wasmtime_component_option_type_equal,
269}
270
271#[unsafe(no_mangle)]
272pub extern "C" fn wasmtime_component_option_type_ty(
273    ty: &wasmtime_component_option_type_t,
274    ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
275) {
276    ret.write(ty.ty.ty().into());
277}
278
279type_wrapper! {
280    #[derive(PartialEq)]
281    pub struct wasmtime_component_result_type_t {
282        pub(crate) ty: ResultType,
283    }
284    clone: wasmtime_component_result_type_clone,
285    delete: wasmtime_component_result_type_delete,
286    equal: wasmtime_component_result_type_equal,
287}
288
289#[unsafe(no_mangle)]
290pub extern "C" fn wasmtime_component_result_type_ok(
291    ty: &wasmtime_component_result_type_t,
292    ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
293) -> bool {
294    match ty.ty.ok() {
295        Some(ok) => {
296            ret.write(ok.into());
297            true
298        }
299        None => false,
300    }
301}
302
303#[unsafe(no_mangle)]
304pub extern "C" fn wasmtime_component_result_type_err(
305    ty: &wasmtime_component_result_type_t,
306    ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
307) -> bool {
308    match ty.ty.err() {
309        Some(err) => {
310            ret.write(err.into());
311            true
312        }
313        None => false,
314    }
315}
316
317type_wrapper! {
318    #[derive(PartialEq)]
319    pub struct wasmtime_component_flags_type_t {
320        pub(crate) ty: Flags,
321    }
322    clone: wasmtime_component_flags_type_clone,
323    delete: wasmtime_component_flags_type_delete,
324    equal: wasmtime_component_flags_type_equal,
325}
326
327#[unsafe(no_mangle)]
328pub extern "C" fn wasmtime_component_flags_type_names_count(
329    ty: &wasmtime_component_flags_type_t,
330) -> usize {
331    ty.ty.names().len()
332}
333
334#[unsafe(no_mangle)]
335pub extern "C" fn wasmtime_component_flags_type_names_nth(
336    ty: &wasmtime_component_flags_type_t,
337    nth: usize,
338    name_ret: &mut MaybeUninit<*const u8>,
339    name_len_ret: &mut MaybeUninit<usize>,
340) -> bool {
341    match ty.ty.names().nth(nth) {
342        Some(name) => {
343            let name: &str = name;
344            name_ret.write(name.as_ptr());
345            name_len_ret.write(name.len());
346            true
347        }
348        None => false,
349    }
350}
351
352type_wrapper! {
353    #[derive(PartialEq)]
354    pub struct wasmtime_component_future_type_t {
355        pub(crate) ty: FutureType,
356    }
357    clone: wasmtime_component_future_type_clone,
358    delete: wasmtime_component_future_type_delete,
359    equal: wasmtime_component_future_type_equal,
360}
361
362#[unsafe(no_mangle)]
363pub extern "C" fn wasmtime_component_future_type_ty(
364    ty: &wasmtime_component_future_type_t,
365    ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
366) -> bool {
367    match ty.ty.ty() {
368        Some(ty) => {
369            ret.write(ty.into());
370            true
371        }
372        None => false,
373    }
374}
375
376type_wrapper! {
377    #[derive(PartialEq)]
378    pub struct wasmtime_component_stream_type_t {
379        pub(crate) ty: StreamType,
380    }
381    clone: wasmtime_component_stream_type_clone,
382    delete: wasmtime_component_stream_type_delete,
383    equal: wasmtime_component_stream_type_equal,
384}
385
386#[unsafe(no_mangle)]
387pub extern "C" fn wasmtime_component_stream_type_ty(
388    ty: &wasmtime_component_stream_type_t,
389    ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
390) -> bool {
391    match ty.ty.ty() {
392        Some(ty) => {
393            ret.write(ty.into());
394            true
395        }
396        None => false,
397    }
398}