use crate::WasiCtx;1use wasmtime::component::ResourceTable;23/// A trait which provides access to the [`WasiCtx`] inside the embedder's `T`4/// of [`Store<T>`][`Store`].5///6/// This crate's WASI Host implementations depend on the contents of7/// [`WasiCtx`]. The `T` type [`Store<T>`][`Store`] is defined in each8/// embedding of Wasmtime. These implementations are connected to the9/// [`Linker<T>`][`Linker`] by [`add_to_linker`](crate::p2::add_to_linker)10/// functions.11///12/// # Example13///14/// ```15/// use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};16/// use wasmtime::component::ResourceTable;17///18/// struct MyState {19/// ctx: WasiCtx,20/// table: ResourceTable,21/// }22///23/// impl WasiView for MyState {24/// fn ctx(&mut self) -> WasiCtxView<'_> {25/// WasiCtxView{26/// ctx: &mut self.ctx,27/// table: &mut self.table,28/// }29/// }30/// }31/// ```32/// [`Store`]: wasmtime::Store33/// [`Linker`]: wasmtime::component::Linker34///35pub trait WasiView: Send {36/// Yields mutable access to the [`WasiCtx`] configuration used for this37/// context.38fn ctx(&mut self) -> WasiCtxView<'_>;39}4041/// Structure returned from [`WasiView::ctx`] which provides accesss to WASI42/// state for host functions to be implemented with.43pub struct WasiCtxView<'a> {44/// The [`WasiCtx`], or configuration, of the guest.45pub ctx: &'a mut WasiCtx,46/// Resources, such as files/streams, that the guest is using.47pub table: &'a mut ResourceTable,48}4950impl<T: WasiView> crate::cli::WasiCliView for T {51fn cli(&mut self) -> crate::cli::WasiCliCtxView<'_> {52let WasiCtxView { ctx, table } = self.ctx();53crate::cli::WasiCliCtxView {54ctx: &mut ctx.cli,55table,56}57}58}5960impl<T: WasiView> crate::clocks::WasiClocksView for T {61fn clocks(&mut self) -> crate::clocks::WasiClocksCtxView<'_> {62let WasiCtxView { ctx, table } = self.ctx();63crate::clocks::WasiClocksCtxView {64ctx: &mut ctx.clocks,65table,66}67}68}6970impl<T: WasiView> crate::filesystem::WasiFilesystemView for T {71fn filesystem(&mut self) -> crate::filesystem::WasiFilesystemCtxView<'_> {72let WasiCtxView { ctx, table } = self.ctx();73crate::filesystem::WasiFilesystemCtxView {74ctx: &mut ctx.filesystem,75table,76}77}78}7980impl<T: WasiView> crate::random::WasiRandomView for T {81fn random(&mut self) -> &mut crate::random::WasiRandomCtx {82&mut self.ctx().ctx.random83}84}8586impl<T: WasiView> crate::sockets::WasiSocketsView for T {87fn sockets(&mut self) -> crate::sockets::WasiSocketsCtxView<'_> {88let WasiCtxView { ctx, table } = self.ctx();89crate::sockets::WasiSocketsCtxView {90ctx: &mut ctx.sockets,91table,92}93}94}959697