xref: /wasmtime-44.0.1/crates/wasi/src/view.rs (revision bc4582c3)
1 use crate::WasiCtx;
2 use wasmtime::component::ResourceTable;
3 
4 /// A trait which provides access to the [`WasiCtx`] inside the embedder's `T`
5 /// of [`Store<T>`][`Store`].
6 ///
7 /// This crate's WASI Host implementations depend on the contents of
8 /// [`WasiCtx`]. The `T` type [`Store<T>`][`Store`] is defined in each
9 /// embedding of Wasmtime. These implementations are connected to the
10 /// [`Linker<T>`][`Linker`] by [`add_to_linker_async`](crate::p2::add_to_linker_async)
11 /// functions.
12 ///
13 /// # Example
14 ///
15 /// ```
16 /// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
17 /// use wasmtime::component::ResourceTable;
18 ///
19 /// struct MyState {
20 ///     ctx: WasiCtx,
21 ///     table: ResourceTable,
22 /// }
23 ///
24 /// impl WasiView for MyState {
25 ///     fn ctx(&mut self) -> WasiCtxView<'_> {
26 ///         WasiCtxView{
27 ///             ctx: &mut self.ctx,
28 ///             table: &mut self.table,
29 ///         }
30 ///     }
31 /// }
32 /// ```
33 /// [`Store`]: wasmtime::Store
34 /// [`Linker`]: wasmtime::component::Linker
35 ///
36 pub trait WasiView: Send {
37     /// Yields mutable access to the [`WasiCtx`] configuration used for this
38     /// context.
ctx(&mut self) -> WasiCtxView<'_>39     fn ctx(&mut self) -> WasiCtxView<'_>;
40 }
41 
42 /// Structure returned from [`WasiView::ctx`] which provides access to WASI
43 /// state for host functions to be implemented with.
44 pub struct WasiCtxView<'a> {
45     /// The [`WasiCtx`], or configuration, of the guest.
46     pub ctx: &'a mut WasiCtx,
47     /// Resources, such as files/streams, that the guest is using.
48     pub table: &'a mut ResourceTable,
49 }
50 
51 impl<T: WasiView> crate::cli::WasiCliView for T {
cli(&mut self) -> crate::cli::WasiCliCtxView<'_>52     fn cli(&mut self) -> crate::cli::WasiCliCtxView<'_> {
53         let WasiCtxView { ctx, table } = self.ctx();
54         crate::cli::WasiCliCtxView {
55             ctx: &mut ctx.cli,
56             table,
57         }
58     }
59 }
60 
61 impl<T: WasiView> crate::clocks::WasiClocksView for T {
clocks(&mut self) -> crate::clocks::WasiClocksCtxView<'_>62     fn clocks(&mut self) -> crate::clocks::WasiClocksCtxView<'_> {
63         let WasiCtxView { ctx, table } = self.ctx();
64         crate::clocks::WasiClocksCtxView {
65             ctx: &mut ctx.clocks,
66             table,
67         }
68     }
69 }
70 
71 impl<T: WasiView> crate::filesystem::WasiFilesystemView for T {
filesystem(&mut self) -> crate::filesystem::WasiFilesystemCtxView<'_>72     fn filesystem(&mut self) -> crate::filesystem::WasiFilesystemCtxView<'_> {
73         let WasiCtxView { ctx, table } = self.ctx();
74         crate::filesystem::WasiFilesystemCtxView {
75             ctx: &mut ctx.filesystem,
76             table,
77         }
78     }
79 }
80 
81 impl<T: WasiView> crate::random::WasiRandomView for T {
random(&mut self) -> &mut crate::random::WasiRandomCtx82     fn random(&mut self) -> &mut crate::random::WasiRandomCtx {
83         &mut self.ctx().ctx.random
84     }
85 }
86 
87 impl<T: WasiView> crate::sockets::WasiSocketsView for T {
sockets(&mut self) -> crate::sockets::WasiSocketsCtxView<'_>88     fn sockets(&mut self) -> crate::sockets::WasiSocketsCtxView<'_> {
89         let WasiCtxView { ctx, table } = self.ctx();
90         crate::sockets::WasiSocketsCtxView {
91             ctx: &mut ctx.sockets,
92             table,
93         }
94     }
95 }
96