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
1692 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
#[allow(unused_imports)]
103
use wasmtime::component::__internal::anyhow;
104
impl ImportsIndices {
105
/// Creates a new copy of `ImportsIndices` bindings which can then
106
/// be used to instantiate into a particular store.
107
///
108
/// This method may fail if the component does not have the
109
/// required exports.
110
pub fn new<_T>(
111
_instance_pre: &wasmtime::component::InstancePre<_T>,
112
) -> wasmtime::Result<Self> {
113
let _component = _instance_pre.component();
114
let _instance_type = _instance_pre.instance_type();
115
Ok(ImportsIndices {})
116
}
117
/// Uses the indices stored in `self` to load an instance
118
/// of [`Imports`] from the instance provided.
119
///
120
/// Note that at this time this method will additionally
121
/// perform type-checks of all exports.
122
pub fn load(
123
&self,
124
mut store: impl wasmtime::AsContextMut,
125
instance: &wasmtime::component::Instance,
126
) -> wasmtime::Result<Imports> {
127
let _ = &mut store;
128
let _instance = instance;
129
Ok(Imports {})
130
}
131
}
132
impl Imports {
133
/// Convenience wrapper around [`ImportsPre::new`] and
134
/// [`ImportsPre::instantiate`].
135
pub fn instantiate<_T>(
136
store: impl wasmtime::AsContextMut<Data = _T>,
137
component: &wasmtime::component::Component,
138
linker: &wasmtime::component::Linker<_T>,
139
) -> wasmtime::Result<Imports> {
140
let pre = linker.instantiate_pre(component)?;
141
ImportsPre::new(pre)?.instantiate(store)
142
}
143
/// Convenience wrapper around [`ImportsIndices::new`] and
144
/// [`ImportsIndices::load`].
145
pub fn new(
146
mut store: impl wasmtime::AsContextMut,
147
instance: &wasmtime::component::Instance,
148
) -> wasmtime::Result<Imports> {
149
let indices = ImportsIndices::new(&instance.instance_pre(&store))?;
150
indices.load(&mut store, instance)
151
}
152
/// Convenience wrapper around [`ImportsPre::new`] and
153
/// [`ImportsPre::instantiate_async`].
154
pub async fn instantiate_async<_T>(
155
store: impl wasmtime::AsContextMut<Data = _T>,
156
component: &wasmtime::component::Component,
157
linker: &wasmtime::component::Linker<_T>,
158
) -> wasmtime::Result<Imports>
159
where
160
_T: Send,
161
{
162
let pre = linker.instantiate_pre(component)?;
163
ImportsPre::new(pre)?.instantiate_async(store).await
164
}
165
pub fn add_to_linker<T, D>(
166
linker: &mut wasmtime::component::Linker<T>,
167
host_getter: fn(&mut T) -> D::Data<'_>,
168
) -> wasmtime::Result<()>
169
where
170
D: a::b::interface_with_live_type::HostWithStore
171
+ a::b::interface_with_dead_type::HostWithStore + Send,
172
for<'a> D::Data<
173
'a,
174
>: a::b::interface_with_live_type::Host
175
+ a::b::interface_with_dead_type::Host + Send,
176
T: 'static + Send,
177
{
178
a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?;
179
a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?;
180
Ok(())
181
}
182
}
183
};
184
pub mod a {
185
pub mod b {
186
#[allow(clippy::all)]
187
pub mod interface_with_live_type {
188
#[allow(unused_imports)]
189
use wasmtime::component::__internal::{anyhow, Box};
190
#[derive(wasmtime::component::ComponentType)]
191
#[derive(wasmtime::component::Lift)]
192
#[derive(wasmtime::component::Lower)]
193
#[component(record)]
194
#[derive(Clone, Copy)]
195
pub struct LiveType {
196
#[component(name = "a")]
197
pub a: u32,
198
}
199
impl core::fmt::Debug for LiveType {
200
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
201
f.debug_struct("LiveType").field("a", &self.a).finish()
202
}
203
}
204
const _: () = {
205
assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
206
assert!(
207
4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
208
);
209
};
210
pub trait HostWithStore: wasmtime::component::HasData + Send {}
211
impl<_T: ?Sized> HostWithStore for _T
212
where
213
_T: wasmtime::component::HasData + Send,
214
{}
215
pub trait Host: Send {
216
fn f(&mut self) -> impl ::core::future::Future<Output = LiveType> + Send;
217
}
218
impl<_T: Host + ?Sized + Send> Host for &mut _T {
219
fn f(
220
&mut self,
221
) -> impl ::core::future::Future<Output = LiveType> + Send {
222
async move { Host::f(*self).await }
223
}
224
}
225
pub fn add_to_linker<T, D>(
226
linker: &mut wasmtime::component::Linker<T>,
227
host_getter: fn(&mut T) -> D::Data<'_>,
228
) -> wasmtime::Result<()>
229
where
230
D: HostWithStore,
231
for<'a> D::Data<'a>: Host,
232
T: 'static + Send,
233
{
234
let mut inst = linker.instance("a:b/interface-with-live-type")?;
235
inst.func_wrap_async(
236
"f",
237
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
238
use tracing::Instrument;
239
let span = tracing::span!(
240
tracing::Level::TRACE, "wit-bindgen import", module =
241
"interface-with-live-type", function = "f",
242
);
243
wasmtime::component::__internal::Box::new(
244
async move {
245
tracing::event!(tracing::Level::TRACE, "call");
246
let host = &mut host_getter(caller.data_mut());
247
let r = Host::f(host).await;
248
tracing::event!(
249
tracing::Level::TRACE, result = tracing::field::debug(& r),
250
"return"
251
);
252
Ok((r,))
253
}
254
.instrument(span),
255
)
256
},
257
)?;
258
Ok(())
259
}
260
}
261
#[allow(clippy::all)]
262
pub mod interface_with_dead_type {
263
#[allow(unused_imports)]
264
use wasmtime::component::__internal::{anyhow, Box};
265
pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;
266
const _: () = {
267
assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);
268
assert!(
269
4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
270
);
271
};
272
#[derive(wasmtime::component::ComponentType)]
273
#[derive(wasmtime::component::Lift)]
274
#[derive(wasmtime::component::Lower)]
275
#[component(record)]
276
#[derive(Clone, Copy)]
277
pub struct DeadType {
278
#[component(name = "a")]
279
pub a: u32,
280
}
281
impl core::fmt::Debug for DeadType {
282
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
283
f.debug_struct("DeadType").field("a", &self.a).finish()
284
}
285
}
286
const _: () = {
287
assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);
288
assert!(
289
4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32
290
);
291
};
292
#[derive(wasmtime::component::ComponentType)]
293
#[derive(wasmtime::component::Lift)]
294
#[derive(wasmtime::component::Lower)]
295
#[component(variant)]
296
#[derive(Clone, Copy)]
297
pub enum V {
298
#[component(name = "a")]
299
A(LiveType),
300
#[component(name = "b")]
301
B(DeadType),
302
}
303
impl core::fmt::Debug for V {
304
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
305
match self {
306
V::A(e) => f.debug_tuple("V::A").field(e).finish(),
307
V::B(e) => f.debug_tuple("V::B").field(e).finish(),
308
}
309
}
310
}
311
const _: () = {
312
assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);
313
assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);
314
};
315
pub trait HostWithStore: wasmtime::component::HasData {}
316
impl<_T: ?Sized> HostWithStore for _T
317
where
318
_T: wasmtime::component::HasData,
319
{}
320
pub trait Host {}
321
impl<_T: Host + ?Sized> Host for &mut _T {}
322
pub fn add_to_linker<T, D>(
323
linker: &mut wasmtime::component::Linker<T>,
324
host_getter: fn(&mut T) -> D::Data<'_>,
325
) -> wasmtime::Result<()>
326
where
327
D: HostWithStore,
328
for<'a> D::Data<'a>: Host,
329
T: 'static,
330
{
331
let mut inst = linker.instance("a:b/interface-with-dead-type")?;
332
Ok(())
333
}
334
}
335
}
336
}
337
338