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