Skip to content

Commit 23f1c77

Browse files
committed
refactor: remove ResourceView
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent 5d7f6cd commit 23f1c77

File tree

9 files changed

+151
-280
lines changed

9 files changed

+151
-280
lines changed

crates/wasi/src/cli.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,6 @@
11
use std::rc::Rc;
22
use std::sync::Arc;
33

4-
use wasmtime::component::ResourceTable;
5-
6-
use crate::ResourceView;
7-
8-
#[repr(transparent)]
9-
pub struct WasiCliImpl<T>(pub T);
10-
11-
impl<T: WasiCliView> WasiCliView for &mut T {
12-
type InputStream = T::InputStream;
13-
type OutputStream = T::OutputStream;
14-
15-
fn cli(&mut self) -> &WasiCliCtx<T::InputStream, T::OutputStream> {
16-
(**self).cli()
17-
}
18-
}
19-
20-
impl<T: WasiCliView> WasiCliView for Box<T> {
21-
type InputStream = T::InputStream;
22-
type OutputStream = T::OutputStream;
23-
24-
fn cli(&mut self) -> &WasiCliCtx<T::InputStream, T::OutputStream> {
25-
(**self).cli()
26-
}
27-
}
28-
29-
impl<T: ResourceView> ResourceView for WasiCliImpl<T> {
30-
fn table(&mut self) -> &mut ResourceTable {
31-
self.0.table()
32-
}
33-
}
34-
35-
impl<T: WasiCliView> WasiCliView for WasiCliImpl<T> {
36-
type InputStream = T::InputStream;
37-
type OutputStream = T::OutputStream;
38-
39-
fn cli(&mut self) -> &WasiCliCtx<T::InputStream, T::OutputStream> {
40-
self.0.cli()
41-
}
42-
}
43-
44-
pub trait WasiCliView: ResourceView + Send {
45-
type InputStream: IsTerminal;
46-
type OutputStream: IsTerminal;
47-
48-
fn cli(&mut self) -> &WasiCliCtx<Self::InputStream, Self::OutputStream>;
49-
}
50-
514
#[derive(Default)]
525
pub struct WasiCliCtx<I, O> {
536
pub environment: Vec<(String, String)>,

crates/wasi/src/lib.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,3 @@ pub use cap_fs_ext::SystemTimeSpec;
4242
pub use cap_rand::RngCore;
4343
#[doc(no_inline)]
4444
pub use wasmtime::component::{ResourceTable, ResourceTableError};
45-
46-
// TODO: Consolidate with `IoView`
47-
pub trait ResourceView {
48-
fn table(&mut self) -> &mut ResourceTable;
49-
}
50-
51-
impl<T: ?Sized + ResourceView> ResourceView for &mut T {
52-
fn table(&mut self) -> &mut ResourceTable {
53-
(**self).table()
54-
}
55-
}
56-
57-
impl<T: ?Sized + ResourceView> ResourceView for Box<T> {
58-
fn table(&mut self) -> &mut ResourceTable {
59-
(**self).table()
60-
}
61-
}

crates/wasi/src/p3/bindings.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
//! done using the `with` option to [`bindgen!`]:
1313
//!
1414
//! ```rust
15-
//! use wasmtime_wasi::ResourceView;
16-
//! use wasmtime_wasi::p3::{WasiCtx, WasiView};
15+
//! use wasmtime_wasi::p3::{WasiCtx, WasiCtxView, WasiView};
1716
//! use wasmtime::{Result, Engine, Config};
1817
//! use wasmtime::component::{Linker, HasSelf, ResourceTable};
1918
//!
@@ -96,12 +95,13 @@
9695
//! }
9796
//! }
9897
//!
99-
//! impl ResourceView for MyState {
100-
//! fn table(&mut self) -> &mut ResourceTable { &mut self.table }
101-
//! }
102-
//!
10398
//! impl WasiView for MyState {
104-
//! fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
99+
//! fn ctx(&mut self) -> WasiCtxView<'_> {
100+
//! WasiCtxView{
101+
//! ctx: &mut self.ctx,
102+
//! table: &mut self.table,
103+
//! }
104+
//! }
105105
//! }
106106
//!
107107
//! fn main() -> Result<()> {
@@ -196,8 +196,7 @@ pub use self::generated::wasi::*;
196196
/// ```no_run
197197
/// use wasmtime::{Engine, Result, Store, Config};
198198
/// use wasmtime::component::{Component, Linker, ResourceTable};
199-
/// use wasmtime_wasi::ResourceView;
200-
/// use wasmtime_wasi::p3::{WasiCtx, WasiView, WasiCtxBuilder};
199+
/// use wasmtime_wasi::p3::{WasiCtx, WasiCtxView, WasiCtxBuilder, WasiView};
201200
/// use wasmtime_wasi::p3::bindings::Command;
202201
///
203202
/// // This example is an example shim of executing a component based on the
@@ -248,12 +247,13 @@ pub use self::generated::wasi::*;
248247
/// table: ResourceTable,
249248
/// }
250249
///
251-
/// impl ResourceView for MyState {
252-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
253-
/// }
254-
///
255250
/// impl WasiView for MyState {
256-
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
251+
/// fn ctx(&mut self) -> WasiCtxView<'_> {
252+
/// WasiCtxView{
253+
/// ctx: &mut self.ctx,
254+
/// table: &mut self.table,
255+
/// }
256+
/// }
257257
/// }
258258
/// ```
259259
///
@@ -270,8 +270,7 @@ pub use self::generated::Command;
270270
/// ```no_run
271271
/// use wasmtime::{Engine, Result, Store, Config};
272272
/// use wasmtime::component::{Linker, Component, ResourceTable};
273-
/// use wasmtime_wasi::ResourceView;
274-
/// use wasmtime_wasi::p3::{WasiCtx, WasiView, WasiCtxBuilder};
273+
/// use wasmtime_wasi::p3::{WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};
275274
/// use wasmtime_wasi::p3::bindings::CommandPre;
276275
///
277276
/// // This example is an example shim of executing a component based on the
@@ -323,12 +322,13 @@ pub use self::generated::Command;
323322
/// table: ResourceTable,
324323
/// }
325324
///
326-
/// impl ResourceView for MyState {
327-
/// fn table(&mut self) -> &mut ResourceTable { &mut self.table }
328-
/// }
329-
///
330325
/// impl WasiView for MyState {
331-
/// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
326+
/// fn ctx(&mut self) -> WasiCtxView<'_> {
327+
/// WasiCtxView{
328+
/// ctx: &mut self.ctx,
329+
/// table: &mut self.table,
330+
/// }
331+
/// }
332332
/// }
333333
/// ```
334334
///

crates/wasi/src/p3/cli/host.rs

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::cli::{IsTerminal, WasiCliImpl, WasiCliView};
1+
use crate::I32Exit;
2+
use crate::cli::IsTerminal;
23
use crate::p3::bindings::cli::{
34
environment, exit, stderr, stdin, stdout, terminal_input, terminal_output, terminal_stderr,
45
terminal_stdin, terminal_stdout,
56
};
6-
use crate::p3::cli::{InputStream, OutputStream, TerminalInput, TerminalOutput, WasiCli};
7-
use crate::{I32Exit, ResourceView as _};
7+
use crate::p3::cli::{TerminalInput, TerminalOutput, WasiCli, WasiCliCtxView};
88
use anyhow::{Context as _, anyhow};
99
use bytes::BytesMut;
1010
use std::io::Cursor;
@@ -78,41 +78,32 @@ where
7878
}
7979
}
8080

81-
impl<T> terminal_input::Host for WasiCliImpl<T> where T: WasiCliView {}
82-
impl<T> terminal_output::Host for WasiCliImpl<T> where T: WasiCliView {}
81+
impl terminal_input::Host for WasiCliCtxView<'_> {}
82+
impl terminal_output::Host for WasiCliCtxView<'_> {}
8383

84-
impl<T> terminal_input::HostTerminalInput for WasiCliImpl<T>
85-
where
86-
T: WasiCliView,
87-
{
84+
impl terminal_input::HostTerminalInput for WasiCliCtxView<'_> {
8885
fn drop(&mut self, rep: Resource<TerminalInput>) -> wasmtime::Result<()> {
89-
self.table()
86+
self.table
9087
.delete(rep)
9188
.context("failed to delete terminal input resource from table")?;
9289
Ok(())
9390
}
9491
}
9592

96-
impl<T> terminal_output::HostTerminalOutput for WasiCliImpl<T>
97-
where
98-
T: WasiCliView,
99-
{
93+
impl terminal_output::HostTerminalOutput for WasiCliCtxView<'_> {
10094
fn drop(&mut self, rep: Resource<TerminalOutput>) -> wasmtime::Result<()> {
101-
self.table()
95+
self.table
10296
.delete(rep)
10397
.context("failed to delete terminal output resource from table")?;
10498
Ok(())
10599
}
106100
}
107101

108-
impl<T> terminal_stdin::Host for WasiCliImpl<T>
109-
where
110-
T: WasiCliView,
111-
{
102+
impl terminal_stdin::Host for WasiCliCtxView<'_> {
112103
fn get_terminal_stdin(&mut self) -> wasmtime::Result<Option<Resource<TerminalInput>>> {
113-
if self.cli().stdin.is_terminal() {
104+
if self.ctx.stdin.is_terminal() {
114105
let fd = self
115-
.table()
106+
.table
116107
.push(TerminalInput)
117108
.context("failed to push terminal stdin resource to table")?;
118109
Ok(Some(fd))
@@ -122,14 +113,11 @@ where
122113
}
123114
}
124115

125-
impl<T> terminal_stdout::Host for WasiCliImpl<T>
126-
where
127-
T: WasiCliView,
128-
{
116+
impl terminal_stdout::Host for WasiCliCtxView<'_> {
129117
fn get_terminal_stdout(&mut self) -> wasmtime::Result<Option<Resource<TerminalOutput>>> {
130-
if self.cli().stdout.is_terminal() {
118+
if self.ctx.stdout.is_terminal() {
131119
let fd = self
132-
.table()
120+
.table
133121
.push(TerminalOutput)
134122
.context("failed to push terminal stdout resource to table")?;
135123
Ok(Some(fd))
@@ -139,14 +127,11 @@ where
139127
}
140128
}
141129

142-
impl<T> terminal_stderr::Host for WasiCliImpl<T>
143-
where
144-
T: WasiCliView,
145-
{
130+
impl terminal_stderr::Host for WasiCliCtxView<'_> {
146131
fn get_terminal_stderr(&mut self) -> wasmtime::Result<Option<Resource<TerminalOutput>>> {
147-
if self.cli().stderr.is_terminal() {
132+
if self.ctx.stderr.is_terminal() {
148133
let fd = self
149-
.table()
134+
.table
150135
.push(TerminalOutput)
151136
.context("failed to push terminal stderr resource to table")?;
152137
Ok(Some(fd))
@@ -156,18 +141,14 @@ where
156141
}
157142
}
158143

159-
impl<T> stdin::HostConcurrent for WasiCli<T>
160-
where
161-
T: WasiCliView + 'static,
162-
T::InputStream: InputStream,
163-
{
144+
impl stdin::HostConcurrent for WasiCli {
164145
async fn get_stdin<U>(store: &Accessor<U, Self>) -> wasmtime::Result<HostStream<u8>> {
165146
store.with(|mut view| {
166147
let instance = view.instance();
167148
let (tx, rx) = instance
168149
.stream::<_, _, BytesMut>(&mut view)
169150
.context("failed to create stream")?;
170-
let stdin = view.get().cli().stdin.reader();
151+
let stdin = view.get().ctx.stdin.reader();
171152
view.spawn(InputTask {
172153
rx: Box::into_pin(stdin),
173154
tx,
@@ -177,20 +158,16 @@ where
177158
}
178159
}
179160

180-
impl<T> stdin::Host for WasiCliImpl<T> where T: WasiCliView {}
161+
impl stdin::Host for WasiCliCtxView<'_> {}
181162

182-
impl<T> stdout::HostConcurrent for WasiCli<T>
183-
where
184-
T: WasiCliView + 'static,
185-
T::OutputStream: OutputStream,
186-
{
163+
impl stdout::HostConcurrent for WasiCli {
187164
async fn set_stdout<U>(
188165
store: &Accessor<U, Self>,
189166
data: HostStream<u8>,
190167
) -> wasmtime::Result<()> {
191168
store.with(|mut view| {
192169
let stdout = data.into_reader(&mut view);
193-
let tx = view.get().cli().stdout.writer();
170+
let tx = view.get().ctx.stdout.writer();
194171
view.spawn(OutputTask {
195172
rx: stdout,
196173
tx: Box::into_pin(tx),
@@ -200,20 +177,16 @@ where
200177
}
201178
}
202179

203-
impl<T> stdout::Host for WasiCliImpl<T> where T: WasiCliView {}
180+
impl stdout::Host for WasiCliCtxView<'_> {}
204181

205-
impl<T> stderr::HostConcurrent for WasiCli<T>
206-
where
207-
T: WasiCliView + 'static,
208-
T::OutputStream: OutputStream,
209-
{
182+
impl stderr::HostConcurrent for WasiCli {
210183
async fn set_stderr<U>(
211184
store: &Accessor<U, Self>,
212185
data: HostStream<u8>,
213186
) -> wasmtime::Result<()> {
214187
store.with(|mut view| {
215188
let stderr = data.into_reader(&mut view);
216-
let tx = view.get().cli().stderr.writer();
189+
let tx = view.get().ctx.stderr.writer();
217190
view.spawn(OutputTask {
218191
rx: stderr,
219192
tx: Box::into_pin(tx),
@@ -223,29 +196,23 @@ where
223196
}
224197
}
225198

226-
impl<T> stderr::Host for WasiCliImpl<T> where T: WasiCliView {}
199+
impl stderr::Host for WasiCliCtxView<'_> {}
227200

228-
impl<T> environment::Host for WasiCliImpl<T>
229-
where
230-
T: WasiCliView,
231-
{
201+
impl environment::Host for WasiCliCtxView<'_> {
232202
fn get_environment(&mut self) -> wasmtime::Result<Vec<(String, String)>> {
233-
Ok(self.cli().environment.clone())
203+
Ok(self.ctx.environment.clone())
234204
}
235205

236206
fn get_arguments(&mut self) -> wasmtime::Result<Vec<String>> {
237-
Ok(self.cli().arguments.clone())
207+
Ok(self.ctx.arguments.clone())
238208
}
239209

240210
fn initial_cwd(&mut self) -> wasmtime::Result<Option<String>> {
241-
Ok(self.cli().initial_cwd.clone())
211+
Ok(self.ctx.initial_cwd.clone())
242212
}
243213
}
244214

245-
impl<T> exit::Host for WasiCliImpl<T>
246-
where
247-
T: WasiCliView,
248-
{
215+
impl exit::Host for WasiCliCtxView<'_> {
249216
fn exit(&mut self, status: Result<(), ()>) -> wasmtime::Result<()> {
250217
let status = match status {
251218
Ok(()) => 0,

0 commit comments

Comments
 (0)