diff --git a/src/ctx.rs b/src/ctx.rs index 66a2e12610..fa084709e3 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -8,6 +8,7 @@ use std::ffi::CString; use std::fs::File; use std::path::{Path, PathBuf}; +/// A builder allowing customizable construction of `WasiCtx` instances. pub struct WasiCtxBuilder { fds: HashMap, preopens: HashMap, @@ -32,6 +33,7 @@ impl WasiCtxBuilder { Ok(builder) } + /// Add arguments to the command-line arguments list. pub fn args>(mut self, args: impl Iterator) -> Result { let args: Result> = args .map(|arg| CString::new(arg.as_ref()).map_err(|_| Error::ENOTCAPABLE)) @@ -40,6 +42,7 @@ impl WasiCtxBuilder { Ok(self) } + /// Add an argument to the command-line arguments list. pub fn arg(mut self, arg: &str) -> Result { self.args .push(CString::new(arg).map_err(|_| Error::ENOTCAPABLE)?); @@ -51,6 +54,7 @@ impl WasiCtxBuilder { self.args(env::args()) } + /// Inherit the stdin, stdout, and stderr streams from the host process. pub fn inherit_stdio(mut self) -> Result { self.fds.insert(0, FdEntry::duplicate_stdin()?); self.fds.insert(1, FdEntry::duplicate_stdout()?); @@ -58,10 +62,12 @@ impl WasiCtxBuilder { Ok(self) } + /// Inherit the environment variables from the host process. pub fn inherit_env(self) -> Result { self.envs(std::env::vars()) } + /// Add an entry to the environment. pub fn env>(mut self, k: S, v: S) -> Result { self.env.insert( CString::new(k.as_ref()).map_err(|_| Error::ENOTCAPABLE)?, @@ -70,6 +76,7 @@ impl WasiCtxBuilder { Ok(self) } + /// Add entries to the environment. pub fn envs, T: Borrow<(S, S)>>( mut self, envs: impl Iterator, @@ -89,11 +96,13 @@ impl WasiCtxBuilder { Ok(self) } + /// Add a preopened directory. pub fn preopened_dir>(mut self, dir: File, guest_path: P) -> Self { self.preopens.insert(guest_path.as_ref().to_owned(), dir); self } + /// Build a `WasiCtx`, consuming this `WasiCtxBuilder`. pub fn build(mut self) -> Result { // startup code starts looking at fd 3 for preopens let mut preopen_fd = 3;