Struct wasmer_wasix::WasiEnvBuilder
source · pub struct WasiEnvBuilder { /* private fields */ }
Expand description
Builder API for configuring a WasiEnv
environment needed to run WASI modules.
Usage:
let mut state_builder = WasiEnv::builder("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_init()?;
Implementations§
source§impl WasiEnvBuilder
impl WasiEnvBuilder
sourcepub fn new(program_name: impl Into<String>) -> Self
pub fn new(program_name: impl Into<String>) -> Self
Creates an empty WasiEnvBuilder
.
sourcepub fn env<Key, Value>(self, key: Key, value: Value) -> Selfwhere
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
pub fn env<Key, Value>(self, key: Key, value: Value) -> 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 add_env<Key, Value>(&mut self, key: Key, value: Value)where
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
pub fn add_env<Key, Value>(&mut self, key: Key, value: Value)where 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 envs<I, Key, Value>(self, env_pairs: I) -> Selfwhere
I: IntoIterator<Item = (Key, Value)>,
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
pub fn envs<I, Key, Value>(self, env_pairs: I) -> 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 add_envs<I, Key, Value>(&mut self, env_pairs: I)where
I: IntoIterator<Item = (Key, Value)>,
Key: AsRef<[u8]>,
Value: AsRef<[u8]>,
pub fn add_envs<I, Key, Value>(&mut self, env_pairs: I)where 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 get_env(&self) -> &[(String, Vec<u8>)]
pub fn get_env(&self) -> &[(String, Vec<u8>)]
Get a reference to the configured environment variables.
sourcepub fn get_env_mut(&mut self) -> &mut Vec<(String, Vec<u8>)>
pub fn get_env_mut(&mut self) -> &mut Vec<(String, Vec<u8>)>
Get a mutable reference to the configured environment variables.
sourcepub fn arg<V>(self, arg: V) -> Selfwhere
V: AsRef<[u8]>,
pub fn arg<V>(self, arg: V) -> Selfwhere V: AsRef<[u8]>,
Add an argument.
Arguments must not contain the nul (0x0) byte
sourcepub fn add_arg<V>(&mut self, arg: V)where
V: AsRef<[u8]>,
pub fn add_arg<V>(&mut self, arg: V)where V: AsRef<[u8]>,
Add an argument.
Arguments must not contain the nul (0x0) byte.
sourcepub fn args<I, Arg>(self, args: I) -> Selfwhere
I: IntoIterator<Item = Arg>,
Arg: AsRef<[u8]>,
pub fn args<I, Arg>(self, args: I) -> Selfwhere I: IntoIterator<Item = Arg>, Arg: AsRef<[u8]>,
Add multiple arguments.
Arguments must not contain the nul (0x0) byte
sourcepub fn add_args<I, Arg>(&mut self, args: I)where
I: IntoIterator<Item = Arg>,
Arg: AsRef<[u8]>,
pub fn add_args<I, Arg>(&mut self, args: I)where I: IntoIterator<Item = Arg>, Arg: AsRef<[u8]>,
Add multiple arguments.
Arguments must not contain the nul (0x0) byte
sourcepub fn get_args_mut(&mut self) -> &mut Vec<String>
pub fn get_args_mut(&mut self) -> &mut Vec<String>
Get a mutable reference to the configured arguments.
sourcepub fn use_webc(self, pkg: BinaryPackage) -> Self
pub fn use_webc(self, pkg: BinaryPackage) -> Self
Adds a container this module inherits from.
This will make all of the container’s files and commands available to the resulting WASI instance.
sourcepub fn add_webc(&mut self, pkg: BinaryPackage) -> &mut Self
pub fn add_webc(&mut self, pkg: BinaryPackage) -> &mut Self
Adds a container this module inherits from.
This will make all of the container’s files and commands available to the resulting WASI instance.
sourcepub fn uses<I>(self, uses: I) -> Selfwhere
I: IntoIterator<Item = BinaryPackage>,
pub fn uses<I>(self, uses: I) -> Selfwhere I: IntoIterator<Item = BinaryPackage>,
Adds a list of other containers this module inherits from.
This will make all of the container’s files and commands available to the resulting WASI instance.
sourcepub fn map_command<Name, Target>(self, name: Name, target: Target) -> Selfwhere
Name: AsRef<str>,
Target: AsRef<str>,
pub fn map_command<Name, Target>(self, name: Name, target: Target) -> Selfwhere Name: AsRef<str>, Target: AsRef<str>,
Map an atom to a local binary
sourcepub fn map_commands<I, Name, Target>(self, map_commands: I) -> Selfwhere
I: IntoIterator<Item = (Name, Target)>,
Name: AsRef<str>,
Target: AsRef<str>,
pub fn map_commands<I, Name, Target>(self, map_commands: I) -> Selfwhere I: IntoIterator<Item = (Name, Target)>, Name: AsRef<str>, Target: AsRef<str>,
Maps a series of atoms to the local binaries
sourcepub fn preopen_dir<P>(self, po_dir: P) -> Result<Self, WasiStateCreationError>where
P: AsRef<Path>,
pub fn preopen_dir<P>(self, po_dir: P) -> Result<Self, WasiStateCreationError>where P: 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 add_preopen_dir<P>(
&mut self,
po_dir: P
) -> Result<(), WasiStateCreationError>where
P: AsRef<Path>,
pub fn add_preopen_dir<P>( &mut self, po_dir: P ) -> Result<(), WasiStateCreationError>where P: AsRef<Path>,
Adds a 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_dirs<I, P>(self, dirs: I) -> Result<Self, WasiStateCreationError>where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
pub fn preopen_dirs<I, P>(self, dirs: I) -> Result<Self, WasiStateCreationError>where I: IntoIterator<Item = P>, P: AsRef<Path>,
Preopen multiple directories.
This opens the given directories at the virtual root, /
, and allows
the WASI module to read and write to the given directory.
sourcepub fn preopen_build<F>(self, inner: F) -> Result<Self, WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
pub fn preopen_build<F>(self, inner: F) -> Result<Self, WasiStateCreationError>where F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
Preopen a directory and configure it.
Usage:
WasiEnv::builder("program_name")
.preopen_build(|p| p.directory("src").read(true).write(true).create(true))?
.preopen_build(|p| p.directory(".").alias("dot").read(true))?
.build_init()?;
sourcepub fn add_preopen_build<F>(
&mut self,
inner: F
) -> Result<(), WasiStateCreationError>where
F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
pub fn add_preopen_build<F>( &mut self, inner: F ) -> Result<(), WasiStateCreationError>where F: Fn(&mut PreopenDirBuilder) -> &mut PreopenDirBuilder,
Preopen a directory and configure it.
Usage:
WasiEnv::builder("program_name")
.preopen_build(|p| p.directory("src").read(true).write(true).create(true))?
.preopen_build(|p| p.directory(".").alias("dot").read(true))?
.build_init()?;
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<P>(
self,
alias: &str,
po_dir: P
) -> Result<Self, WasiStateCreationError>where
P: AsRef<Path>,
pub fn map_dir<P>( self, alias: &str, po_dir: P ) -> Result<Self, WasiStateCreationError>where P: AsRef<Path>,
Preopen a directory with a different name exposed to the WASI.
sourcepub fn add_map_dir<P>(
&mut self,
alias: &str,
po_dir: P
) -> Result<(), WasiStateCreationError>where
P: AsRef<Path>,
pub fn add_map_dir<P>( &mut self, alias: &str, po_dir: P ) -> Result<(), WasiStateCreationError>where P: AsRef<Path>,
Preopen a directory with a different name exposed to the WASI.
sourcepub fn map_dirs<I, P>(
self,
mapped_dirs: I
) -> Result<Self, WasiStateCreationError>where
I: IntoIterator<Item = (String, P)>,
P: AsRef<Path>,
pub fn map_dirs<I, P>( self, mapped_dirs: I ) -> Result<Self, WasiStateCreationError>where I: IntoIterator<Item = (String, P)>, P: AsRef<Path>,
Preopen directorys with a different names exposed to the WASI.
sourcepub fn stdout(
self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>
) -> Self
pub fn stdout( self, new_file: Box<dyn VirtualFile + Send + Sync + 'static> ) -> Self
Overwrite the default WASI stdout
, if you want to hold on to the
original stdout
use WasiFs::swap_file
after building.
sourcepub fn set_stdout(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>
)
pub fn set_stdout( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static> )
Overwrite the default WASI stdout
, if you want to hold on to the
original stdout
use WasiFs::swap_file
after building.
sourcepub fn stderr(
self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>
) -> Self
pub fn stderr( self, new_file: Box<dyn VirtualFile + Send + Sync + 'static> ) -> Self
Overwrite the default WASI stderr
, if you want to hold on to the
original stderr
use WasiFs::swap_file
after building.
sourcepub fn set_stderr(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>
)
pub fn set_stderr( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static> )
Overwrite the default WASI stderr
, if you want to hold on to the
original stderr
use WasiFs::swap_file
after building.
sourcepub fn stdin(
self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>
) -> Self
pub fn stdin( self, new_file: Box<dyn VirtualFile + Send + Sync + 'static> ) -> 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_stdin(
&mut self,
new_file: Box<dyn VirtualFile + Send + Sync + 'static>
)
pub fn set_stdin( &mut self, new_file: Box<dyn VirtualFile + Send + Sync + 'static> )
Overwrite the default WASI stdin
, if you want to hold on to the
original stdin
use WasiFs::swap_file
after building.
sourcepub fn fs(self, fs: Box<dyn FileSystem + Send + Sync>) -> Self
pub fn fs(self, fs: Box<dyn FileSystem + Send + Sync>) -> Self
Sets the FileSystem to be used with this WASI instance.
This is usually used in case a custom virtual_fs::FileSystem
is needed.
pub fn set_fs(&mut self, fs: Box<dyn FileSystem + Send + Sync>)
sourcepub fn sandbox_fs(self, fs: TmpFileSystem) -> Self
pub fn sandbox_fs(self, fs: TmpFileSystem) -> Self
Sets a new sandbox FileSystem to be used with this WASI instance.
This is usually used in case a custom virtual_fs::FileSystem
is needed.
sourcepub fn setup_fs(
self,
setup_fs_fn: Box<dyn Fn(&WasiInodes, &mut WasiFs) -> Result<(), String> + Send>
) -> Self
pub fn setup_fs( self, setup_fs_fn: Box<dyn Fn(&WasiInodes, &mut WasiFs) -> Result<(), String> + Send> ) -> Self
Configure the WASI filesystem before running.
sourcepub fn runtime(self, runtime: Arc<dyn Runtime + Send + Sync>) -> Self
pub fn runtime(self, runtime: Arc<dyn Runtime + Send + Sync>) -> Self
Sets the WASI runtime implementation and overrides the default implementation
pub fn set_runtime(&mut self, runtime: Arc<dyn Runtime + Send + Sync>)
pub fn capabilities(self, capabilities: Capabilities) -> Self
pub fn capabilities_mut(&mut self) -> &mut Capabilities
pub fn set_capabilities(&mut self, capabilities: Capabilities)
sourcepub fn build_init(self) -> Result<WasiEnvInit, WasiStateCreationError>
pub fn build_init(self) -> Result<WasiEnvInit, WasiStateCreationError>
Consumes the WasiEnvBuilder
and produces a WasiEnvInit
, which
can be used to construct a new WasiEnv
.
Returns the error from WasiFs::new
if there’s an error
NOTE: You should prefer to not work directly with WasiEnvInit
.
Use WasiEnvBuilder::run
or WasiEnvBuilder::run_with_store
instead
to ensure proper invokation of WASI modules.
pub fn build(self) -> Result<WasiEnv, WasiRuntimeError>
sourcepub fn instantiate(
self,
module: Module,
store: &mut impl AsStoreMut
) -> Result<(Instance, WasiFunctionEnv), WasiRuntimeError>
pub fn instantiate( self, module: Module, store: &mut impl AsStoreMut ) -> Result<(Instance, WasiFunctionEnv), WasiRuntimeError>
Consumes the WasiEnvBuilder
and produces a WasiEnvInit
, which
can be used to construct a new WasiEnv
.
Returns the error from WasiFs::new
if there’s an error
pub fn run(self, module: Module) -> Result<(), WasiRuntimeError>
pub fn run_with_store( self, module: Module, store: &mut Store ) -> Result<(), WasiRuntimeError>
sourcepub fn run_with_store_async(
self,
module: Module,
store: Store
) -> Result<(), WasiRuntimeError>
pub fn run_with_store_async( self, module: Module, store: Store ) -> Result<(), WasiRuntimeError>
Start the WASI executable with async threads enabled.