Struct wasmer_wasi::WasiStateBuilder
source · 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 Selfwhere
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
pub fn env<Key, Value>(&mut self, key: Key, value: Value) -> &mut Selfwhere
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
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 Selfwhere
Arg: AsRef<[u8]>,
pub fn arg<Arg>(&mut self, arg: Arg) -> &mut Selfwhere
Arg: AsRef<[u8]>,
Add an argument.
Arguments must not contain the nul (0x0) byte
sourcepub fn envs<I, Key, Value>(&mut self, env_pairs: I) -> &mut Selfwhere
I: IntoIterator<Item = (Key, Value)>,
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
pub fn envs<I, Key, Value>(&mut self, env_pairs: I) -> &mut Selfwhere
I: IntoIterator<Item = (Key, Value)>,
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
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 Selfwhere
I: IntoIterator<Item = Arg>,
Arg: AsRef<[u8]>,
pub fn args<I, Arg>(&mut self, args: I) -> &mut Selfwhere
I: IntoIterator<Item = Arg>,
Arg: AsRef<[u8]>,
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>where
FilePath: AsRef<Path>,
pub fn preopen_dir<FilePath>(
&mut self,
po_dir: FilePath
) -> Result<&mut Self, WasiStateCreationError>where
FilePath: AsRef<Path>,
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>where
I: IntoIterator<Item = FilePath>,
FilePath: AsRef<Path>,
pub fn preopen_dirs<I, FilePath>(
&mut self,
po_dirs: I
) -> Result<&mut Self, WasiStateCreationError>where
I: IntoIterator<Item = FilePath>,
FilePath: AsRef<Path>,
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>where
FilePath: AsRef<Path>,
pub fn map_dir<FilePath>(
&mut self,
alias: &str,
po_dir: FilePath
) -> Result<&mut Self, WasiStateCreationError>where
FilePath: AsRef<Path>,
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>where
I: IntoIterator<Item = (String, FilePath)>,
FilePath: AsRef<Path>,
pub fn map_dirs<I, FilePath>(
&mut self,
mapped_dirs: I
) -> Result<&mut Self, WasiStateCreationError>where
I: IntoIterator<Item = (String, FilePath)>,
FilePath: AsRef<Path>,
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 Selfwhere
R: WasiRuntimeImplementation + Send + Sync + 'static,
pub fn runtime<R>(&mut self, runtime: R) -> &mut Selfwhere
R: WasiRuntimeImplementation + Send + Sync + 'static,
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.