diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-11 12:09:24 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-11 12:09:24 +0200 |
commit | 96bfee68c0b2205b1fad7b10cd3236c80334dfde (patch) | |
tree | 24f27d1a5d762a897f15aa19659de06a4b5ec539 | |
parent | f9efed7066867ff21b4a23682ccc24ed33d86d23 (diff) |
Refactor helper fn to helper type
This reduces the type parameters that need to be specified when calling
the helper.
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | plugins/plugin_fdman/src/plugin.rs | 87 |
1 files changed, 55 insertions, 32 deletions
diff --git a/plugins/plugin_fdman/src/plugin.rs b/plugins/plugin_fdman/src/plugin.rs index dc22e92d..2d2fa008 100644 --- a/plugins/plugin_fdman/src/plugin.rs +++ b/plugins/plugin_fdman/src/plugin.rs @@ -72,15 +72,9 @@ impl Handle<OpenOptions> for FdManPlugin { OpenOptionsResult::new(message, file_result) } - with_aquired_handles::< - 1, - OpenOptionsResult, - crate::file::FileGuard, - OpenOptionsError, - OpenOptions, - _, - >(self, message, sender, inner) - .await + HandleWithGuard::new(self, message, sender) + .with_handles(1, inner) + .await } } @@ -100,7 +94,8 @@ impl Handle<Copy> for FdManPlugin { CopyResult::new(message, copy_res) } - with_aquired_handles::<2, CopyResult, u64, CopyError, Copy, _>(self, message, sender, inner) + HandleWithGuard::new(self, message, sender) + .with_handles(2, inner) .await } } @@ -121,39 +116,67 @@ impl Handle<Rename> for FdManPlugin { RenameResult::new(message, rename_res) } - with_aquired_handles::<2, RenameResult, (), RenameError, Rename, _>( - self, message, sender, inner, - ) - .await + HandleWithGuard::new(self, message, sender) + .with_handles(2, inner) + .await } } -async fn with_aquired_handles<const N: u64, Res, RT, Err, M, Fut>( - this: &FdManPlugin, +struct HandleWithGuard<'a, M, Res, RT, Err> +where + M: tedge_api::Message + tedge_api::message::AcceptsReplies<Reply = Res>, + Res: tedge_api::Message + MessageResult<M, RT, Err>, + Err: From<Error> + std::error::Error, +{ + plugin: &'a FdManPlugin, message: M, sender: tedge_api::address::ReplySenderFor<M>, - inner: impl Fn(Guard, M) -> Fut, -) -> Result<(), PluginError> + _pd_res: std::marker::PhantomData<Res>, + _pd_rt: std::marker::PhantomData<RT>, + _pd_err: std::marker::PhantomData<Err>, +} + +impl<'a, M, Res, RT, Err> HandleWithGuard<'a, M, Res, RT, Err> where - Err: From<Error> + std::error::Error, M: tedge_api::Message + tedge_api::message::AcceptsReplies<Reply = Res>, Res: tedge_api::Message + MessageResult<M, RT, Err>, - Fut: std::future::Future<Output = Res>, + Err: From<Error> + std::error::Error, { - let handles_to_aquire = N; - match this.aquire_handles(handles_to_aquire) { - Ok(guard) => { - let res: Res = inner(guard, message).await; + fn new( + plugin: &'a FdManPlugin, + message: M, + sender: tedge_api::address::ReplySenderFor<M>, + ) -> Self { + Self { + plugin, + message, + sender, + _pd_res: std::marker::PhantomData, + _pd_rt: std::marker::PhantomData, + _pd_err: std::marker::PhantomData, + } + } - sender - .reply(res) + async fn with_handles<F, Fut>(self, handles_to_aquire: u64, fun: F) -> Result<(), PluginError> + where + F: Fn(Guard, M) -> Fut, + Fut: std::future::Future<Output = Res>, + { + match self.plugin.aquire_handles(handles_to_aquire) { + Ok(guard) => { + let res: Res = fun(guard, self.message).await; + + self.sender + .reply(res) + .map_err(|_| Error::SendingReply) + .map_err(PluginError::from) + } + Err(err @ Error::InsufficientHandles { .. }) => self + .sender + .reply(Res::new(self.message, Err(Err::from(err)))) .map_err(|_| Error::SendingReply) - .map_err(PluginError::from) + .map_err(PluginError::from), + Err(other) => Err(PluginError::from(other)), } - Err(err @ Error::InsufficientHandles { .. }) => sender - .reply(Res::new(message, Err(Err::from(err)))) - .map_err(|_| Error::SendingReply) - .map_err(PluginError::from), - Err(other) => Err(PluginError::from(other)), } } |