From c188eaa2e16c3778b827f5218191932a8560c682 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 11 Sep 2022 12:11:53 +0200 Subject: Refactor: Pass closure to helper Signed-off-by: Matthias Beyer --- plugins/plugin_fdman/src/plugin.rs | 58 +++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/plugins/plugin_fdman/src/plugin.rs b/plugins/plugin_fdman/src/plugin.rs index 2d2fa008..16fe362c 100644 --- a/plugins/plugin_fdman/src/plugin.rs +++ b/plugins/plugin_fdman/src/plugin.rs @@ -62,18 +62,16 @@ impl Handle for FdManPlugin { message: OpenOptions, sender: tedge_api::address::ReplySenderFor, ) -> Result<(), PluginError> { - async fn inner(guard: Guard, message: OpenOptions) -> OpenOptionsResult { - let file_result = message - .as_std() - .open(message.path()) - .map(|file| crate::file::FileGuard::new(file, guard)) - .map_err(Error::from) - .map_err(OpenOptionsError::from); - OpenOptionsResult::new(message, file_result) - } - HandleWithGuard::new(self, message, sender) - .with_handles(1, inner) + .with_handles(1, move |guard: Guard, message: OpenOptions| async { + let file_result = message + .as_std() + .open(message.path()) + .map(|file| crate::file::FileGuard::new(file, guard)) + .map_err(Error::from) + .map_err(OpenOptionsError::from); + OpenOptionsResult::new(message, file_result) + }) .await } } @@ -85,17 +83,15 @@ impl Handle for FdManPlugin { message: Copy, sender: tedge_api::address::ReplySenderFor, ) -> Result<(), PluginError> { - async fn inner(guard: Guard, message: Copy) -> CopyResult { - let copy_res = tokio::fs::copy(message.src(), message.dst()) - .await - .map_err(Error::from) - .map_err(CopyError::from); - drop(guard); // We can now drop the guard, as the copy process is done - CopyResult::new(message, copy_res) - } - HandleWithGuard::new(self, message, sender) - .with_handles(2, inner) + .with_handles(2, move |guard: Guard, message: Copy| async { + let copy_res = tokio::fs::copy(message.src(), message.dst()) + .await + .map_err(Error::from) + .map_err(CopyError::from); + drop(guard); // We can now drop the guard, as the copy process is done + CopyResult::new(message, copy_res) + }) .await } } @@ -107,17 +103,15 @@ impl Handle for FdManPlugin { message: Rename, sender: tedge_api::address::ReplySenderFor, ) -> Result<(), PluginError> { - async fn inner(guard: Guard, message: Rename) -> RenameResult { - let rename_res = tokio::fs::rename(message.src(), message.dst()) - .await - .map_err(Error::from) - .map_err(RenameError::from); - drop(guard); // We can now drop the guard, as the copy process is done - RenameResult::new(message, rename_res) - } - HandleWithGuard::new(self, message, sender) - .with_handles(2, inner) + .with_handles(2, move |guard: Guard, message: Rename| async { + let rename_res = tokio::fs::rename(message.src(), message.dst()) + .await + .map_err(Error::from) + .map_err(RenameError::from); + drop(guard); // We can now drop the guard, as the copy process is done + RenameResult::new(message, rename_res) + }) .await } } @@ -159,7 +153,7 @@ where async fn with_handles(self, handles_to_aquire: u64, fun: F) -> Result<(), PluginError> where - F: Fn(Guard, M) -> Fut, + F: FnOnce(Guard, M) -> Fut, Fut: std::future::Future, { match self.plugin.aquire_handles(handles_to_aquire) { -- cgit v1.2.3