Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/component-macro/tests/expanded/dead-code_tracing_async.rs
3068 views
1
/// Auto-generated bindings for a pre-instantiated version of a
2
/// component which implements the world `imports`.
3
///
4
/// This structure is created through [`ImportsPre::new`] which
5
/// takes a [`InstancePre`](wasmtime::component::InstancePre) that
6
/// has been created through a [`Linker`](wasmtime::component::Linker).
7
///
8
/// For more information see [`Imports`] as well.
9
pub struct ImportsPre<T: 'static> {
10
instance_pre: wasmtime::component::InstancePre<T>,
11
indices: ImportsIndices,
12
}
13
impl<T: 'static> Clone for ImportsPre<T> {
14
fn clone(&self) -> Self {
15
Self {
16
instance_pre: self.instance_pre.clone(),
17
indices: self.indices.clone(),
18
}
19
}
20
}
21
impl<_T: 'static> ImportsPre<_T> {
22
/// Creates a new copy of `ImportsPre` bindings which can then
23
/// be used to instantiate into a particular store.
24
///
25
/// This method may fail if the component behind `instance_pre`
26
/// does not have the required exports.
27
pub fn new(
28
instance_pre: wasmtime::component::InstancePre<_T>,
29
) -> wasmtime::Result<Self> {
30
let indices = ImportsIndices::new(&instance_pre)?;
31
Ok(Self { instance_pre, indices })
32
}
33
pub fn engine(&self) -> &wasmtime::Engine {
34
self.instance_pre.engine()
35
}
36
pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
37
&self.instance_pre
38
}
39
/// Instantiates a new instance of [`Imports`] within the
40
/// `store` provided.
41
///
42
/// This function will use `self` as the pre-instantiated
43
/// instance to perform instantiation. Afterwards the preloaded
44
/// indices in `self` are used to lookup all exports on the
45
/// resulting instance.
46
pub fn instantiate(
47
&self,
48
mut store: impl wasmtime::AsContextMut<Data = _T>,
49
) -> wasmtime::Result<Imports> {
50
let mut store = store.as_context_mut();
51
let instance = self.instance_pre.instantiate(&mut store)?;
52
self.indices.load(&mut store, &instance)
53
}
54
}
55
impl<_T: Send + 'static> ImportsPre<_T> {
56
/// Same as [`Self::instantiate`], except with `async`.
57
pub async fn instantiate_async(
58
&self,
59
mut store: impl wasmtime::AsContextMut<Data = _T>,
60
) -> wasmtime::Result<Imports> {
61
let mut store = store.as_context_mut();
62
let instance = self.instance_pre.instantiate_async(&mut store).await?;
63
self.indices.load(&mut store, &instance)
64
}
65
}
66
/// Auto-generated bindings for index of the exports of
67
/// `imports`.
68
///
69
/// This is an implementation detail of [`ImportsPre`] and can
70
/// be constructed if needed as well.
71
///
72
/// For more information see [`Imports`] as well.
73
#[derive(Clone)]
74
pub struct ImportsIndices {}
75
/// Auto-generated bindings for an instance a component which
76
/// implements the world `imports`.
77
///
78
/// This structure can be created through a number of means
79
/// depending on your requirements and what you have on hand:
80
///
81
/// * The most convenient way is to use
82
/// [`Imports::instantiate`] which only needs a
83
/// [`Store`], [`Component`], and [`Linker`].
84
///
85
/// * Alternatively you can create a [`ImportsPre`] ahead of
86
/// time with a [`Component`] to front-load string lookups
87
/// of exports once instead of per-instantiation. This
88
/// method then uses [`ImportsPre::instantiate`] to
89
/// create a [`Imports`].
90
///
91
/// * If you've instantiated the instance yourself already
92
/// then you can use [`Imports::new`].
93
///
94
/// These methods are all equivalent to one another and move
95
/// around the tradeoff of what work is performed when.
96
///
97
/// [`Store`]: wasmtime::Store
98
/// [`Component`]: wasmtime::component::Component
99
/// [`Linker`]: wasmtime::component::Linker
100
pub struct Imports {}
101
const _: () = {
102
impl ImportsIndices {
103
/// Creates a new copy of `ImportsIndices` bindings which can then
104
/// be used to instantiate into a particular store.
105
///
106
/// This method may fail if the component does not have the
107
/// required exports.
108
pub fn new<_T>(
109
_instance_pre: &wasmtime::component::InstancePre<_T>,
110
) -> wasmtime::Result<Self> {
111
let _component = _instance_pre.component();
112
let _instance_type = _instance_pre.instance_type();
113
Ok(ImportsIndices {})
114
}
115
/// Uses the indices stored in `self` to load an instance
116
/// of [`Imports`] from the instance provided.
117
///
118
/// Note that at this time this method will additionally
119
/// perform type-checks of all exports.
120
pub fn load(
121
&self,
122
mut store: impl wasmtime::AsContextMut,
123
instance: &wasmtime::component::Instance,
124
) -> wasmtime::Result<Imports> {
125
let _ = &mut store;
126
let _instance = instance;
127
Ok(Imports {})
128
}
129
}
130
impl Imports {
131
/// Convenience wrapper around [`ImportsPre::new`] and
132
/// [`ImportsPre::instantiate`].
133
pub fn instantiate<_T>(
134
store: impl wasmtime::AsContextMut<Data = _T>,
135
component: &wasmtime::component::Component,
136
linker: &wasmtime::component::Linker<_T>,
137
) -> wasmtime::Result<Imports> {
138
let pre = linker.instantiate_pre(component)?;
139
ImportsPre::new(pre)?.instantiate(store)
140
}
141
/// Convenience wrapper around [`ImportsIndices::new`] and
142
/// [`ImportsIndices::load`].
143
pub fn new(
144
mut store: impl wasmtime::AsContextMut,
145
instance: &wasmtime::component::Instance,
146
) -> wasmtime::Result<Imports> {
147
let indices = ImportsIndices::new(&instance.instance_pre(&store))?;
148
indices.load(&mut store, instance)
149
}
150
/// Convenience wrapper around [`ImportsPre::new`] and
151
/// [`ImportsPre::instantiate_async`].
152
pub async fn instantiate_async<_T>(
153
store: impl wasmtime::AsContextMut<Data = _T>,
154
component: &wasmtime::component::Component,
155
linker: &wasmtime::component::Linker<_T>,
156
) -> wasmtime::Result<Imports>
157
where
158
_T: Send,
159
{
160
let pre = linker.instantiate_pre(component)?;
161
ImportsPre::new(pre)?.instantiate_async(store).await
162
}
163
pub fn add_to_linker<T, D>(
164
linker: &mut wasmtime::component::Linker<T>,
165
host_getter: fn(&mut T) -> D::Data<'_>,
166
) -> wasmtime::Result<()>
167
where
168
D: a::b::interface_with_live_type::HostWithStore
169
+ a::b::interface_with_dead_type::HostWithStore + Send,
170
for<'a> D::Data<
171
'a,
172
>: a::b::interface_with_live_type::Host
173
+ a::b::interface_with_dead_type::Host + Send,
174
T: 'static + Send,
175
{
176
a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?;
177
a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?;
178
Ok(())
179
}
180
}
181
};
182
pub mod a {
183
pub mod b {
184
#[allow(clippy::all)]
185
pub mod interface_with_live_type {
186
#[allow(unused_imports)]
187
use wasmtime::component::__internal::Box;
188
#[derive(wasmtime::component::ComponentType)]
189
#[derive(wasmtime::component::Lift)]
190
#[derive(wasmtime::component::Lower)]
191
#[component(record)]
192
#[derive(Clone, Copy)]
193
pub struct LiveType {
194
#[component(name = "a")]
195
pub a: u32,
196
}
197
impl core::fmt::Debug for LiveType {
198
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
199
f.debug_struct("LiveType").field("a", &self.a).finish()
200
}
201
}
202
const _: () = {
203
assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
204
assert!(
205
4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
206
);
207
};
208
pub trait HostWithStore: wasmtime::component::HasData + Send {}
209
impl<_T: ?Sized> HostWithStore for _T
210
where
211
_T: wasmtime::component::HasData + Send,
212
{}
213
pub trait Host: Send {
214
fn f(&mut self) -> impl ::core::future::Future<Output = LiveType> + Send;
215
}
216
impl<_T: Host + ?Sized + Send> Host for &mut _T {
217
fn f(
218
&mut self,
219
) -> impl ::core::future::Future<Output = LiveType> + Send {
220
async move { Host::f(*self).await }
221
}
222
}
223
pub fn add_to_linker<T, D>(
224
linker: &mut wasmtime::component::Linker<T>,
225
host_getter: fn(&mut T) -> D::Data<'_>,
226
) -> wasmtime::Result<()>
227
where
228
D: HostWithStore,
229
for<'a> D::Data<'a>: Host,
230
T: 'static + Send,
231
{
232
let mut inst = linker.instance("a:b/interface-with-live-type")?;
233
inst.func_wrap_async(
234
"f",
235
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
236
use tracing::Instrument;
237
let span = tracing::span!(
238
tracing::Level::TRACE, "wit-bindgen import", module =
239
"interface-with-live-type", function = "f",
240
);
241
wasmtime::component::__internal::Box::new(
242
async move {
243
tracing::event!(tracing::Level::TRACE, "call");
244
let host = &mut host_getter(caller.data_mut());
245
let r = Host::f(host).await;
246
tracing::event!(
247
tracing::Level::TRACE, result = tracing::field::debug(& r),
248
"return"
249
);
250
Ok((r,))
251
}
252
.instrument(span),
253
)
254
},
255
)?;
256
Ok(())
257
}
258
}
259
#[allow(clippy::all)]
260
pub mod interface_with_dead_type {
261
#[allow(unused_imports)]
262
use wasmtime::component::__internal::Box;
263
pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
264
const _: () = {
265
assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
266
assert!(
267
4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
268
);
269
};
270
#[derive(wasmtime::component::ComponentType)]
271
#[derive(wasmtime::component::Lift)]
272
#[derive(wasmtime::component::Lower)]
273
#[component(record)]
274
#[derive(Clone, Copy)]
275
pub struct DeadType {
276
#[component(name = "a")]
277
pub a: u32,
278
}
279
impl core::fmt::Debug for DeadType {
280
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
281
f.debug_struct("DeadType").field("a", &self.a).finish()
282
}
283
}
284
const _: () = {
285
assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
286
assert!(
287
4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
288
);
289
};
290
#[derive(wasmtime::component::ComponentType)]
291
#[derive(wasmtime::component::Lift)]
292
#[derive(wasmtime::component::Lower)]
293
#[component(variant)]
294
#[derive(Clone, Copy)]
295
pub enum V {
296
#[component(name = "a")]
297
A(LiveType),
298
#[component(name = "b")]
299
B(DeadType),
300
}
301
impl core::fmt::Debug for V {
302
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
303
match self {
304
V::A(e) => f.debug_tuple("V::A").field(e).finish(),
305
V::B(e) => f.debug_tuple("V::B").field(e).finish(),
306
}
307
}
308
}
309
const _: () = {
310
assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
311
assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
312
};
313
pub trait HostWithStore: wasmtime::component::HasData {}
314
impl<_T: ?Sized> HostWithStore for _T
315
where
316
_T: wasmtime::component::HasData,
317
{}
318
pub trait Host {}
319
impl<_T: Host + ?Sized> Host for &mut _T {}
320
pub fn add_to_linker<T, D>(
321
linker: &mut wasmtime::component::Linker<T>,
322
host_getter: fn(&mut T) -> D::Data<'_>,
323
) -> wasmtime::Result<()>
324
where
325
D: HostWithStore,
326
for<'a> D::Data<'a>: Host,
327
T: 'static,
328
{
329
let mut inst = linker.instance("a:b/interface-with-dead-type")?;
330
Ok(())
331
}
332
}
333
}
334
}
335
336