Skip to content

Commit 8d8a0d2

Browse files
authored
Merge pull request #1 from madonuko/w/typed-run
feat!: `Container.run()` generic return type
2 parents 314cb3b + 2cb9f63 commit 8d8a0d2

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/lib.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
collections::BTreeMap,
3-
error::Error,
43
fs::File,
54
os::fd::AsRawFd,
65
path::{Path, PathBuf},
@@ -256,10 +255,9 @@ impl Container {
256255

257256
/// Run a function inside the container chroot
258257
#[inline(always)]
259-
pub fn run<F, E>(&mut self, f: F) -> Result<(), E>
258+
pub fn run<F, T>(&mut self, f: F) -> std::io::Result<T>
260259
where
261-
F: FnOnce() -> Result<(), E>,
262-
E: Error + From<std::io::Error>,
260+
F: FnOnce() -> T,
263261
{
264262
// Only mount and chroot if we're not already initialized
265263
if !self._initialized {
@@ -269,15 +267,14 @@ impl Container {
269267
self.chroot()?;
270268
}
271269
tracing::trace!("Running function inside container");
272-
f()?;
270+
let ret = f();
273271
if self.chroot {
274272
self.exit_chroot()?;
275273
}
276274
if self._initialized {
277275
self.umount()?;
278276
}
279-
280-
Ok(())
277+
Ok(ret)
281278
}
282279

283280
/// Start mounting files inside the container
@@ -388,10 +385,7 @@ mod tests {
388385
std::fs::create_dir_all("/tmp/tiffin").unwrap();
389386
let mut container = Container::new(PathBuf::from("/tmp/tiffin"));
390387
container
391-
.run(|| {
392-
std::fs::create_dir_all("/tmp/tiffin/test").unwrap();
393-
std::io::Result::Ok(())
394-
})
388+
.run(|| std::fs::create_dir_all("/tmp/tiffin/test").unwrap())
395389
.unwrap();
396390
}
397391
}

0 commit comments

Comments
 (0)