Skip to content

Commit e7f43b8

Browse files
authored
wasmtime-wasi: Split a new IoView trait off of WasiView (bytecodealliance#10016)
* split IoView trait off of WasiView * move wasi-http over to split views * config and keyvalue: no changes to libraries, just tests where WasiView used * wasmtime-cli: fixes for IoView/WasiView split * move rest of wasi-io impl to be on IoImpl * wasi-http: linker with wasi IoImpl * wasi-nn tests: IoView impl
1 parent 112d1a6 commit e7f43b8

File tree

33 files changed

+309
-250
lines changed

33 files changed

+309
-250
lines changed

crates/wasi-config/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! component::{Linker, ResourceTable},
1818
//! Config, Engine, Result, Store,
1919
//! };
20-
//! use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};
20+
//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
2121
//! use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
2222
//!
2323
//! #[tokio::main]
@@ -53,8 +53,10 @@
5353
//! wasi_config_vars: WasiConfigVariables,
5454
//! }
5555
//!
56-
//! impl WasiView for Ctx {
56+
//! impl IoView for Ctx {
5757
//! fn table(&mut self) -> &mut ResourceTable { &mut self.table }
58+
//! }
59+
//! impl WasiView for Ctx {
5860
//! fn ctx(&mut self) -> &mut WasiCtx { &mut self.wasi_ctx }
5961
//! }
6062
//! ```

crates/wasi-config/tests/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use wasmtime::{
44
component::{Component, Linker, ResourceTable},
55
Store,
66
};
7-
use wasmtime_wasi::{add_to_linker_async, bindings::Command, WasiCtx, WasiCtxBuilder, WasiView};
7+
use wasmtime_wasi::{
8+
add_to_linker_async, bindings::Command, IoView, WasiCtx, WasiCtxBuilder, WasiView,
9+
};
810
use wasmtime_wasi_config::{WasiConfig, WasiConfigVariables};
911

1012
struct Ctx {
@@ -13,11 +15,12 @@ struct Ctx {
1315
wasi_config_vars: WasiConfigVariables,
1416
}
1517

16-
impl WasiView for Ctx {
18+
impl IoView for Ctx {
1719
fn table(&mut self) -> &mut ResourceTable {
1820
&mut self.table
1921
}
20-
22+
}
23+
impl WasiView for Ctx {
2124
fn ctx(&mut self) -> &mut WasiCtx {
2225
&mut self.wasi_ctx
2326
}

crates/wasi-http/src/http_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use bytes::Bytes;
1414
use http_body_util::{BodyExt, Empty};
1515
use hyper::Method;
1616
use wasmtime::component::Resource;
17+
use wasmtime_wasi::IoView;
1718

1819
impl<T> outgoing_handler::Host for WasiHttpImpl<T>
1920
where

crates/wasi-http/src/lib.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
//! use tokio::net::TcpListener;
7272
//! use wasmtime::component::{Component, Linker, ResourceTable};
7373
//! use wasmtime::{Config, Engine, Result, Store};
74-
//! use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiView};
74+
//! use wasmtime_wasi::{IoView, WasiCtx, WasiCtxBuilder, WasiView};
7575
//! use wasmtime_wasi_http::bindings::ProxyPre;
7676
//! use wasmtime_wasi_http::bindings::http::types::Scheme;
7777
//! use wasmtime_wasi_http::body::HyperOutgoingBody;
@@ -193,23 +193,21 @@
193193
//! http: WasiHttpCtx,
194194
//! table: ResourceTable,
195195
//! }
196-
//!
196+
//! impl IoView for MyClientState {
197+
//! fn table(&mut self) -> &mut ResourceTable {
198+
//! &mut self.table
199+
//! }
200+
//! }
197201
//! impl WasiView for MyClientState {
198202
//! fn ctx(&mut self) -> &mut WasiCtx {
199203
//! &mut self.wasi
200204
//! }
201-
//! fn table(&mut self) -> &mut ResourceTable {
202-
//! &mut self.table
203-
//! }
204205
//! }
205206
//!
206207
//! impl WasiHttpView for MyClientState {
207208
//! fn ctx(&mut self) -> &mut WasiHttpCtx {
208209
//! &mut self.http
209210
//! }
210-
//! fn table(&mut self) -> &mut ResourceTable {
211-
//! &mut self.table
212-
//! }
213211
//! }
214212
//! ```
215213
@@ -236,7 +234,7 @@ pub use crate::types::{
236234
WasiHttpCtx, WasiHttpImpl, WasiHttpView, DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,
237235
DEFAULT_OUTGOING_BODY_CHUNK_SIZE,
238236
};
239-
237+
use wasmtime_wasi::IoImpl;
240238
/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].
241239
///
242240
/// This function will add the `async` variant of all interfaces into the
@@ -251,7 +249,7 @@ pub use crate::types::{
251249
/// ```
252250
/// use wasmtime::{Engine, Result, Config};
253251
/// use wasmtime::component::{ResourceTable, Linker};
254-
/// use wasmtime_wasi::{WasiCtx, WasiView};
252+
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
255253
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
256254
///
257255
/// fn main() -> Result<()> {
@@ -272,25 +270,27 @@ pub use crate::types::{
272270
/// table: ResourceTable,
273271
/// }
274272
///
273+
/// impl IoView for MyState {
274+
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
275+
/// }
275276
/// impl WasiHttpView for MyState {
276277
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
277-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
278278
/// }
279279
/// impl WasiView for MyState {
280280
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
281-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
282281
/// }
283282
/// ```
284283
pub fn add_to_linker_async<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
285284
where
286285
T: WasiHttpView + wasmtime_wasi::WasiView,
287286
{
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)));
289289
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
290290
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)?;
294294
wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
295295
wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
296296
wasmtime_wasi::bindings::cli::stderr::add_to_linker_get_host(l, closure)?;
@@ -313,6 +313,12 @@ where
313313
{
314314
val
315315
}
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+
}
316322

317323
/// A slimmed down version of [`add_to_linker_async`] which only adds
318324
/// `wasi:http` interfaces to the linker.
@@ -325,7 +331,7 @@ pub fn add_only_http_to_linker_async<T>(
325331
where
326332
T: WasiHttpView,
327333
{
328-
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(t));
334+
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(IoImpl(t)));
329335
crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
330336
crate::bindings::http::types::add_to_linker_get_host(l, closure)?;
331337

@@ -343,7 +349,7 @@ where
343349
/// ```
344350
/// use wasmtime::{Engine, Result, Config};
345351
/// use wasmtime::component::{ResourceTable, Linker};
346-
/// use wasmtime_wasi::{WasiCtx, WasiView};
352+
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView};
347353
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
348354
///
349355
/// fn main() -> Result<()> {
@@ -362,27 +368,28 @@ where
362368
/// http_ctx: WasiHttpCtx,
363369
/// table: ResourceTable,
364370
/// }
365-
///
371+
/// impl IoView for MyState {
372+
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
373+
/// }
366374
/// impl WasiHttpView for MyState {
367375
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
368-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
369376
/// }
370377
/// impl WasiView for MyState {
371378
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
372-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
373379
/// }
374380
/// ```
375381
pub fn add_to_linker_sync<T>(l: &mut wasmtime::component::Linker<T>) -> anyhow::Result<()>
376382
where
377383
T: WasiHttpView + wasmtime_wasi::WasiView,
378384
{
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)));
380387

381388
wasmtime_wasi::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?;
382389
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)?;
386393
wasmtime_wasi::bindings::cli::stdin::add_to_linker_get_host(l, closure)?;
387394
wasmtime_wasi::bindings::cli::stdout::add_to_linker_get_host(l, closure)?;
388395
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>) -
402409
where
403410
T: WasiHttpView,
404411
{
405-
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(t));
412+
let closure = type_annotate_http::<T, _>(|t| WasiHttpImpl(IoImpl(t)));
406413

407414
crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?;
408415
crate::bindings::http::types::add_to_linker_get_host(l, closure)?;

crates/wasi-http/src/types.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::time::Duration;
1818
use tokio::net::TcpStream;
1919
use tokio::time::timeout;
2020
use wasmtime::component::{Resource, ResourceTable};
21-
use wasmtime_wasi::{runtime::AbortOnDropJoinHandle, Subscribe};
21+
use wasmtime_wasi::{runtime::AbortOnDropJoinHandle, IoImpl, IoView, Subscribe};
2222

2323
/// Capture the state necessary for use in the wasi-http API implementation.
2424
#[derive(Debug)]
@@ -39,7 +39,7 @@ impl WasiHttpCtx {
3939
///
4040
/// ```
4141
/// use wasmtime::component::ResourceTable;
42-
/// use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};
42+
/// use wasmtime_wasi::{IoView, WasiCtx, WasiView, WasiCtxBuilder};
4343
/// use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
4444
///
4545
/// struct MyState {
@@ -48,14 +48,15 @@ impl WasiHttpCtx {
4848
/// table: ResourceTable,
4949
/// }
5050
///
51+
/// impl IoView for MyState {
52+
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
53+
/// }
5154
/// impl WasiHttpView for MyState {
5255
/// fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http_ctx }
53-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
5456
/// }
5557
///
5658
/// impl WasiView for MyState {
5759
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
58-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
5960
/// }
6061
///
6162
/// impl MyState {
@@ -73,13 +74,10 @@ impl WasiHttpCtx {
7374
/// }
7475
/// }
7576
/// ```
76-
pub trait WasiHttpView: Send {
77+
pub trait WasiHttpView: IoView {
7778
/// Returns a mutable reference to the WASI HTTP context.
7879
fn ctx(&mut self) -> &mut WasiHttpCtx;
7980

80-
/// Returns a mutable reference to the WASI HTTP resource table.
81-
fn table(&mut self) -> &mut ResourceTable;
82-
8381
/// Create a new incoming request resource.
8482
fn new_incoming_request<B>(
8583
&mut self,
@@ -150,10 +148,6 @@ impl<T: ?Sized + WasiHttpView> WasiHttpView for &mut T {
150148
T::ctx(self)
151149
}
152150

153-
fn table(&mut self) -> &mut ResourceTable {
154-
T::table(self)
155-
}
156-
157151
fn new_response_outparam(
158152
&mut self,
159153
result: tokio::sync::oneshot::Sender<
@@ -189,10 +183,6 @@ impl<T: ?Sized + WasiHttpView> WasiHttpView for Box<T> {
189183
T::ctx(self)
190184
}
191185

192-
fn table(&mut self) -> &mut ResourceTable {
193-
T::table(self)
194-
}
195-
196186
fn new_response_outparam(
197187
&mut self,
198188
result: tokio::sync::oneshot::Sender<
@@ -236,15 +226,16 @@ impl<T: ?Sized + WasiHttpView> WasiHttpView for Box<T> {
236226
/// [`add_to_linker_sync`](crate::add_to_linker_sync)
237227
/// and doesn't need to be manually configured.
238228
#[repr(transparent)]
239-
pub struct WasiHttpImpl<T>(pub T);
229+
pub struct WasiHttpImpl<T>(pub IoImpl<T>);
240230

231+
impl<T: IoView> IoView for WasiHttpImpl<T> {
232+
fn table(&mut self) -> &mut ResourceTable {
233+
T::table(&mut self.0 .0)
234+
}
235+
}
241236
impl<T: WasiHttpView> WasiHttpView for WasiHttpImpl<T> {
242237
fn ctx(&mut self) -> &mut WasiHttpCtx {
243-
self.0.ctx()
244-
}
245-
246-
fn table(&mut self) -> &mut ResourceTable {
247-
self.0.table()
238+
self.0 .0.ctx()
248239
}
249240

250241
fn new_response_outparam(
@@ -253,27 +244,27 @@ impl<T: WasiHttpView> WasiHttpView for WasiHttpImpl<T> {
253244
Result<hyper::Response<HyperOutgoingBody>, types::ErrorCode>,
254245
>,
255246
) -> wasmtime::Result<Resource<HostResponseOutparam>> {
256-
self.0.new_response_outparam(result)
247+
self.0 .0.new_response_outparam(result)
257248
}
258249

259250
fn send_request(
260251
&mut self,
261252
request: hyper::Request<HyperOutgoingBody>,
262253
config: OutgoingRequestConfig,
263254
) -> crate::HttpResult<HostFutureIncomingResponse> {
264-
self.0.send_request(request, config)
255+
self.0 .0.send_request(request, config)
265256
}
266257

267258
fn is_forbidden_header(&mut self, name: &HeaderName) -> bool {
268-
self.0.is_forbidden_header(name)
259+
self.0 .0.is_forbidden_header(name)
269260
}
270261

271262
fn outgoing_body_buffer_chunks(&mut self) -> usize {
272-
self.0.outgoing_body_buffer_chunks()
263+
self.0 .0.outgoing_body_buffer_chunks()
273264
}
274265

275266
fn outgoing_body_chunk_size(&mut self) -> usize {
276-
self.0.outgoing_body_chunk_size()
267+
self.0 .0.outgoing_body_chunk_size()
277268
}
278269
}
279270

crates/wasi-http/src/types_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::str::FromStr;
1616
use wasmtime::component::{Resource, ResourceTable};
1717
use wasmtime_wasi::{
1818
bindings::io::streams::{InputStream, OutputStream},
19-
Pollable, ResourceTableError,
19+
IoView, Pollable, ResourceTableError,
2020
};
2121

2222
impl<T> crate::bindings::http::types::Host for WasiHttpImpl<T>

crates/wasi-http/tests/all/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use wasmtime::{
1111
component::{Component, Linker, ResourceTable},
1212
Config, Engine, Store,
1313
};
14-
use wasmtime_wasi::{self, pipe::MemoryOutputPipe, WasiCtx, WasiCtxBuilder, WasiView};
14+
use wasmtime_wasi::{self, pipe::MemoryOutputPipe, IoView, WasiCtx, WasiCtxBuilder, WasiView};
1515
use wasmtime_wasi_http::{
1616
bindings::http::types::{ErrorCode, Scheme},
1717
body::HyperOutgoingBody,
@@ -38,10 +38,12 @@ struct Ctx {
3838
rejected_authority: Option<String>,
3939
}
4040

41-
impl WasiView for Ctx {
41+
impl IoView for Ctx {
4242
fn table(&mut self) -> &mut ResourceTable {
4343
&mut self.table
4444
}
45+
}
46+
impl WasiView for Ctx {
4547
fn ctx(&mut self) -> &mut WasiCtx {
4648
&mut self.wasi
4749
}
@@ -52,10 +54,6 @@ impl WasiHttpView for Ctx {
5254
&mut self.http
5355
}
5456

55-
fn table(&mut self) -> &mut ResourceTable {
56-
&mut self.table
57-
}
58-
5957
fn send_request(
6058
&mut self,
6159
request: hyper::Request<HyperOutgoingBody>,

0 commit comments

Comments
 (0)