71
71
//! use tokio::net::TcpListener;
72
72
//! use wasmtime::component::{Component, Linker, ResourceTable};
73
73
//! use wasmtime::{Config, Engine, Result, Store};
74
- //! use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};
74
+ //! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
75
75
//! use wasmtime_wasi_http::bindings::ProxyPre;
76
76
//! use wasmtime_wasi_http::bindings::http::types::Scheme;
77
77
//! use wasmtime_wasi_http::body::HyperOutgoingBody;
193
193
//! http: WasiHttpCtx,
194
194
//! table: ResourceTable,
195
195
//! }
196
- //!
196
+ //! impl IoView for MyClientState {
197
+ //! fn table(&mut self) -> &mut ResourceTable {
198
+ //! &mut self.table
199
+ //! }
200
+ //! }
197
201
//! impl WasiView for MyClientState {
198
202
//! fn ctx(&mut self) -> &mut WasiCtx {
199
203
//! &mut self.wasi
200
204
//! }
201
- //! fn table(&mut self) -> &mut ResourceTable {
202
- //! &mut self.table
203
- //! }
204
205
//! }
205
206
//!
206
207
//! impl WasiHttpView for MyClientState {
207
208
//! fn ctx(&mut self) -> &mut WasiHttpCtx {
208
209
//! &mut self.http
209
210
//! }
210
- //! fn table(&mut self) -> &mut ResourceTable {
211
- //! &mut self.table
212
- //! }
213
211
//! }
214
212
//! ```
215
213
@@ -236,7 +234,7 @@ pub use crate::types::{
236
234
WasiHttpCtx , WasiHttpImpl , WasiHttpView , DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS ,
237
235
DEFAULT_OUTGOING_BODY_CHUNK_SIZE ,
238
236
} ;
239
-
237
+ use wasmtime_wasi :: IoImpl ;
240
238
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
241
239
///
242
240
/// This function will add the `async` variant of all interfaces into the
@@ -251,7 +249,7 @@ pub use crate::types::{
251
249
/// ```
252
250
/// use wasmtime::{Engine, Result, Config};
253
251
/// use wasmtime::component::{ResourceTable, Linker};
254
- /// use wasmtime_wasi::{WasiCtx, WasiView};
252
+ /// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
255
253
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
256
254
///
257
255
/// fn main() -> Result<()> {
@@ -272,25 +270,27 @@ pub use crate::types::{
272
270
/// table: ResourceTable,
273
271
/// }
274
272
///
273
+ /// impl IoView for MyState {
274
+ /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
275
+ /// }
275
276
/// impl WasiHttpView for MyState {
276
277
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
277
- /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
278
278
/// }
279
279
/// impl WasiView for MyState {
280
280
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
281
- /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
282
281
/// }
283
282
/// ```
284
283
pub fn add_to_linker_async < T > ( l : & mut wasmtime:: component:: Linker < T > ) -> anyhow:: Result < ( ) >
285
284
where
286
285
T : WasiHttpView + wasmtime_wasi:: WasiView ,
287
286
{
288
- let closure = type_annotate_wasi :: < T , _ > ( |t| wasmtime_wasi:: WasiImpl ( t) ) ;
287
+ let io_closure = type_annotate_io :: < T , _ > ( |t| wasmtime_wasi:: IoImpl ( t) ) ;
288
+ let closure = type_annotate_wasi :: < T , _ > ( |t| wasmtime_wasi:: WasiImpl ( wasmtime_wasi:: IoImpl ( t) ) ) ;
289
289
wasmtime_wasi:: bindings:: clocks:: wall_clock:: add_to_linker_get_host ( l, closure) ?;
290
290
wasmtime_wasi:: bindings:: clocks:: monotonic_clock:: add_to_linker_get_host ( l, closure) ?;
291
- wasmtime_wasi:: bindings:: io:: poll:: add_to_linker_get_host ( l, closure ) ?;
292
- wasmtime_wasi:: bindings:: io:: error:: add_to_linker_get_host ( l, closure ) ?;
293
- wasmtime_wasi:: bindings:: io:: streams:: add_to_linker_get_host ( l, closure ) ?;
291
+ wasmtime_wasi:: bindings:: io:: poll:: add_to_linker_get_host ( l, io_closure ) ?;
292
+ wasmtime_wasi:: bindings:: io:: error:: add_to_linker_get_host ( l, io_closure ) ?;
293
+ wasmtime_wasi:: bindings:: io:: streams:: add_to_linker_get_host ( l, io_closure ) ?;
294
294
wasmtime_wasi:: bindings:: cli:: stdin:: add_to_linker_get_host ( l, closure) ?;
295
295
wasmtime_wasi:: bindings:: cli:: stdout:: add_to_linker_get_host ( l, closure) ?;
296
296
wasmtime_wasi:: bindings:: cli:: stderr:: add_to_linker_get_host ( l, closure) ?;
@@ -313,6 +313,12 @@ where
313
313
{
314
314
val
315
315
}
316
+ fn type_annotate_io < T , F > ( val : F ) -> F
317
+ where
318
+ F : Fn ( & mut T ) -> wasmtime_wasi:: IoImpl < & mut T > ,
319
+ {
320
+ val
321
+ }
316
322
317
323
/// A slimmed down version of [`add_to_linker_async`] which only adds
318
324
/// `wasi:http` interfaces to the linker.
@@ -325,7 +331,7 @@ pub fn add_only_http_to_linker_async<T>(
325
331
where
326
332
T : WasiHttpView ,
327
333
{
328
- let closure = type_annotate_http :: < T , _ > ( |t| WasiHttpImpl ( t ) ) ;
334
+ let closure = type_annotate_http :: < T , _ > ( |t| WasiHttpImpl ( IoImpl ( t ) ) ) ;
329
335
crate :: bindings:: http:: outgoing_handler:: add_to_linker_get_host ( l, closure) ?;
330
336
crate :: bindings:: http:: types:: add_to_linker_get_host ( l, closure) ?;
331
337
@@ -343,7 +349,7 @@ where
343
349
/// ```
344
350
/// use wasmtime::{Engine, Result, Config};
345
351
/// use wasmtime::component::{ResourceTable, Linker};
346
- /// use wasmtime_wasi::{WasiCtx, WasiView};
352
+ /// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
347
353
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
348
354
///
349
355
/// fn main() -> Result<()> {
@@ -362,27 +368,28 @@ where
362
368
/// http_ctx: WasiHttpCtx,
363
369
/// table: ResourceTable,
364
370
/// }
365
- ///
371
+ /// impl IoView for MyState {
372
+ /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
373
+ /// }
366
374
/// impl WasiHttpView for MyState {
367
375
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
368
- /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
369
376
/// }
370
377
/// impl WasiView for MyState {
371
378
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
372
- /// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
373
379
/// }
374
380
/// ```
375
381
pub fn add_to_linker_sync < T > ( l : & mut wasmtime:: component:: Linker < T > ) -> anyhow:: Result < ( ) >
376
382
where
377
383
T : WasiHttpView + wasmtime_wasi:: WasiView ,
378
384
{
379
- let closure = type_annotate_wasi :: < T , _ > ( |t| wasmtime_wasi:: WasiImpl ( t) ) ;
385
+ let io_closure = type_annotate_io :: < T , _ > ( |t| wasmtime_wasi:: IoImpl ( t) ) ;
386
+ let closure = type_annotate_wasi :: < T , _ > ( |t| wasmtime_wasi:: WasiImpl ( wasmtime_wasi:: IoImpl ( t) ) ) ;
380
387
381
388
wasmtime_wasi:: bindings:: clocks:: wall_clock:: add_to_linker_get_host ( l, closure) ?;
382
389
wasmtime_wasi:: bindings:: clocks:: monotonic_clock:: add_to_linker_get_host ( l, closure) ?;
383
- wasmtime_wasi:: bindings:: sync:: io:: poll:: add_to_linker_get_host ( l, closure ) ?;
384
- wasmtime_wasi:: bindings:: sync:: io:: streams:: add_to_linker_get_host ( l, closure ) ?;
385
- wasmtime_wasi:: bindings:: io:: error:: add_to_linker_get_host ( l, closure ) ?;
390
+ wasmtime_wasi:: bindings:: sync:: io:: poll:: add_to_linker_get_host ( l, io_closure ) ?;
391
+ wasmtime_wasi:: bindings:: sync:: io:: streams:: add_to_linker_get_host ( l, io_closure ) ?;
392
+ wasmtime_wasi:: bindings:: io:: error:: add_to_linker_get_host ( l, io_closure ) ?;
386
393
wasmtime_wasi:: bindings:: cli:: stdin:: add_to_linker_get_host ( l, closure) ?;
387
394
wasmtime_wasi:: bindings:: cli:: stdout:: add_to_linker_get_host ( l, closure) ?;
388
395
wasmtime_wasi:: bindings:: cli:: stderr:: add_to_linker_get_host ( l, closure) ?;
@@ -402,7 +409,7 @@ pub fn add_only_http_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -
402
409
where
403
410
T : WasiHttpView ,
404
411
{
405
- let closure = type_annotate_http :: < T , _ > ( |t| WasiHttpImpl ( t ) ) ;
412
+ let closure = type_annotate_http :: < T , _ > ( |t| WasiHttpImpl ( IoImpl ( t ) ) ) ;
406
413
407
414
crate :: bindings:: http:: outgoing_handler:: add_to_linker_get_host ( l, closure) ?;
408
415
crate :: bindings:: http:: types:: add_to_linker_get_host ( l, closure) ?;
0 commit comments