make WasiCtxBuilder be an actual builder, allowing to call .build() at the end of a call chain

This commit is contained in:
Léo Gaspard
2021-01-13 04:51:00 +01:00
parent f3156114c4
commit 2e035be60a

View File

@@ -66,12 +66,12 @@ impl WasiCtxBuilder {
Ok(self.0) Ok(self.0)
} }
pub fn arg(&mut self, arg: &str) -> Result<&mut Self, StringArrayError> { pub fn arg(mut self, arg: &str) -> Result<Self, StringArrayError> {
self.0.args.push(arg.to_owned())?; self.0.args.push(arg.to_owned())?;
Ok(self) Ok(self)
} }
pub fn stdin(&mut self, f: Box<dyn WasiFile>) -> &mut Self { pub fn stdin(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.insert_file( self.0.insert_file(
0, 0,
f, f,
@@ -80,7 +80,7 @@ impl WasiCtxBuilder {
self self
} }
pub fn stdout(&mut self, f: Box<dyn WasiFile>) -> &mut Self { pub fn stdout(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.insert_file( self.0.insert_file(
1, 1,
f, f,
@@ -89,7 +89,7 @@ impl WasiCtxBuilder {
self self
} }
pub fn stderr(&mut self, f: Box<dyn WasiFile>) -> &mut Self { pub fn stderr(mut self, f: Box<dyn WasiFile>) -> Self {
self.0.insert_file( self.0.insert_file(
2, 2,
f, f,
@@ -98,17 +98,17 @@ impl WasiCtxBuilder {
self self
} }
pub fn inherit_stdio(&mut self) -> &mut Self { pub fn inherit_stdio(self) -> Self {
self.stdin(Box::new(crate::stdio::stdin())) self.stdin(Box::new(crate::stdio::stdin()))
.stdout(Box::new(crate::stdio::stdout())) .stdout(Box::new(crate::stdio::stdout()))
.stderr(Box::new(crate::stdio::stderr())) .stderr(Box::new(crate::stdio::stderr()))
} }
pub fn preopened_dir( pub fn preopened_dir(
&mut self, mut self,
dir: Box<dyn WasiDir>, dir: Box<dyn WasiDir>,
path: impl AsRef<Path>, path: impl AsRef<Path>,
) -> Result<&mut Self, Error> { ) -> Result<Self, Error> {
let caps = DirCaps::all(); let caps = DirCaps::all();
let file_caps = FileCaps::all(); let file_caps = FileCaps::all();
self.0.table().push(Box::new(DirEntry::new( self.0.table().push(Box::new(DirEntry::new(
@@ -120,7 +120,7 @@ impl WasiCtxBuilder {
Ok(self) Ok(self)
} }
pub fn random(&mut self, random: Box<dyn RngCore>) -> &mut Self { pub fn random(mut self, random: Box<dyn RngCore>) -> Self {
self.0.random.replace(random); self.0.random.replace(random);
self self
} }