From 2cb9f639e2658f15bb232432df3a8f6ef4e61027 Mon Sep 17 00:00:00 2001
From: madonuko <madonuko@outlook.com>
Date: Wed, 10 Jul 2024 15:27:51 +0800
Subject: [PATCH] feat!: `Container.run()` generic return type

---
 src/lib.rs | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 956b344..7364b23 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,5 @@
 use std::{
     collections::BTreeMap,
-    error::Error,
     fs::File,
     os::fd::AsRawFd,
     path::{Path, PathBuf},
@@ -256,10 +255,9 @@ impl Container {
 
     /// Run a function inside the container chroot
     #[inline(always)]
-    pub fn run<F, E>(&mut self, f: F) -> Result<(), E>
+    pub fn run<F, T>(&mut self, f: F) -> std::io::Result<T>
     where
-        F: FnOnce() -> Result<(), E>,
-        E: Error + From<std::io::Error>,
+        F: FnOnce() -> T,
     {
         // Only mount and chroot if we're not already initialized
         if !self._initialized {
@@ -269,15 +267,14 @@ impl Container {
             self.chroot()?;
         }
         tracing::trace!("Running function inside container");
-        f()?;
+        let ret = f();
         if self.chroot {
             self.exit_chroot()?;
         }
         if self._initialized {
             self.umount()?;
         }
-
-        Ok(())
+        Ok(ret)
     }
 
     /// Start mounting files inside the container
@@ -388,10 +385,7 @@ mod tests {
         std::fs::create_dir_all("/tmp/tiffin").unwrap();
         let mut container = Container::new(PathBuf::from("/tmp/tiffin"));
         container
-            .run(|| {
-                std::fs::create_dir_all("/tmp/tiffin/test").unwrap();
-                std::io::Result::Ok(())
-            })
+            .run(|| std::fs::create_dir_all("/tmp/tiffin/test").unwrap())
             .unwrap();
     }
 }