pub struct WasiStateBuilder { /* private fields */ }
Expand description
Convenient builder API for configuring WASI via WasiState
.
Usage:
let mut state_builder = WasiState::new("wasi-prog-name");
state_builder
.env("ENV_VAR", "ENV_VAL")
.arg("--verbose")
.preopen_dir("src")?
.map_dir("name_wasi_sees", "path/on/host/fs")?
.build();
Implementations§
Source§impl WasiStateBuilder
impl WasiStateBuilder
Sourcepub fn env<Key, Value>(&mut self, key: Key, value: Value) -> &mut Self
pub fn env<Key, Value>(&mut self, key: Key, value: Value) -> &mut Self
Add an environment variable pair.
Both the key and value of an environment variable must not
contain a nul byte (0x0
), and the key must not contain the
=
byte (0x3d
).
Sourcepub fn arg<Arg>(&mut self, arg: Arg) -> &mut Self
pub fn arg<Arg>(&mut self, arg: Arg) -> &mut Self
Add an argument.
Arguments must not contain the nul (0x0) byte
Sourcepub fn envs<I, Key, Value>(&mut self, env_pairs: I) -> &mut Self
pub fn envs<I, Key, Value>(&mut self, env_pairs: I) -> &mut Self
Add multiple environment variable pairs.
Both the key and value of the environment variables must not
contain a nul byte (0x0
), and the key must not contain the
=
byte (0x3d
).
Sourcepub fn args<I, Arg>(&mut self, args: I) -> &mut Self
pub fn args<I, Arg>(&mut self, args: I) -> &mut Self
Add multiple arguments.
Arguments must not contain the nul (0x0) byte
Sourcepub fn preopen_dir<FilePath>(
&mut self,
po_dir: FilePath,
) -> Result<&mut Self, WasiStateCreationError>
pub fn preopen_dir<FilePath>( &mut self, po_dir: FilePath, ) -> Result<&mut Self, WasiStateCreationError>
Preopen a directory
This opens the given directory at the virtual root, /
, and allows
the WASI module to read and write to the given directory.
Sourcepub fn preopen<F>(
&mut self,
inner: F,
) -> Result<&mut Self, WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
pub fn preopen<F>(
&mut self,
inner: F,
) -> Result<&mut Self, WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
Preopen a directory and configure it.
Usage:
WasiState::new("program_name")
.preopen(|p| p.directory("src").read(true).write(true).create(true))?
.preopen(|p| p.directory(".").alias("dot").read(true))?
.build()?;
Sourcepub fn preopen_dirs<I, FilePath>(
&mut self,
po_dirs: I,
) -> Result<&mut Self, WasiStateCreationError>
pub fn preopen_dirs<I, FilePath>( &mut self, po_dirs: I, ) -> Result<&mut Self, WasiStateCreationError>
Preopen a directory.
This opens the given directory at the virtual root, /
, and allows
the WASI module to read and write to the given directory.
Sourcepub fn preopen_vfs_dirs<I>(
&mut self,
po_dirs: I,
) -> Result<&mut Self, WasiStateCreationError>where
I: IntoIterator<Item = String>,
pub fn preopen_vfs_dirs<I>(
&mut self,
po_dirs: I,
) -> Result<&mut Self, WasiStateCreationError>where
I: IntoIterator<Item = String>,
Preopen the given directories from the Virtual FS.
Sourcepub fn map_dir<FilePath>(
&mut self,
alias: &str,
po_dir: FilePath,
) -> Result<&mut Self, WasiStateCreationError>
pub fn map_dir<FilePath>( &mut self, alias: &str, po_dir: FilePath, ) -> Result<&mut Self, WasiStateCreationError>
Preopen a directory with a different name exposed to the WASI.
Sourcepub fn map_dirs<I, FilePath>(
&mut self,
mapped_dirs: I,
) -> Result<&mut Self, WasiStateCreationError>
pub fn map_dirs<I, FilePath>( &mut self, mapped_dirs: I, ) -> Result<&mut Self, WasiStateCreationError>
Preopen directorys with a different names exposed to the WASI.
Sourcepub fn stdout(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
) -> &mut Self
pub fn stdout( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, ) -> &mut Self
Overwrite the default WASI stdout
, if you want to hold on to the
original stdout
use WasiFs::swap_file
after building.
Sourcepub fn stderr(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
) -> &mut Self
pub fn stderr( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, ) -> &mut Self
Overwrite the default WASI stderr
, if you want to hold on to the
original stderr
use WasiFs::swap_file
after building.
Sourcepub fn stdin(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>,
) -> &mut Self
pub fn stdin( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static>, ) -> &mut Self
Overwrite the default WASI stdin
, if you want to hold on to the
original stdin
use WasiFs::swap_file
after building.
Sourcepub fn set_fs(&mut self, fs: Box<dyn FileSystem>) -> &mut Self
pub fn set_fs(&mut self, fs: Box<dyn FileSystem>) -> &mut Self
Sets the FileSystem to be used with this WASI instance.
This is usually used in case a custom wasmer_vfs::FileSystem
is needed.
Sourcepub fn setup_fs(
&mut self,
setup_fs_fn: Box<dyn Fn(&mut WasiInodes, &mut WasiFs) -> Result<(), String> + Send>,
) -> &mut Self
pub fn setup_fs( &mut self, setup_fs_fn: Box<dyn Fn(&mut WasiInodes, &mut WasiFs) -> Result<(), String> + Send>, ) -> &mut Self
Configure the WASI filesystem before running.
Sourcepub fn runtime<R>(&mut self, runtime: R) -> &mut Self
pub fn runtime<R>(&mut self, runtime: R) -> &mut Self
Sets the WASI runtime implementation and overrides the default implementation
Sourcepub fn build(&mut self) -> Result<WasiState, WasiStateCreationError>
pub fn build(&mut self) -> Result<WasiState, WasiStateCreationError>
Consumes the WasiStateBuilder
and produces a WasiState
Returns the error from WasiFs::new
if there’s an error
§Calling build
multiple times
Calling this method multiple times might not produce a determinisic result. This method is changing the builder’s internal state. The values set with the following methods are reset to their defaults:
Ideally, the builder must be refactord to update &mut self
to mut self
for every builder method, but it will break
existing code. It will be addressed in a next major release.
Sourcepub fn finalize(
&mut self,
store: &mut impl AsStoreMut,
) -> Result<WasiFunctionEnv, WasiStateCreationError>
pub fn finalize( &mut self, store: &mut impl AsStoreMut, ) -> Result<WasiFunctionEnv, WasiStateCreationError>
Consumes the WasiStateBuilder
and produces a WasiEnv
Returns the error from WasiFs::new
if there’s an error.
§Calling finalize
multiple times
Calling this method multiple times might not produce a determinisic result. This method is calling Self::build, which is changing the builder’s internal state. See Self::build’s documentation to learn more.