diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-10 10:53:58 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-09-10 12:13:33 +0200 |
commit | e3270d1dd4033160777d5b6f2eadf7ab2ff5f5ba (patch) | |
tree | 5799e9584a06241bce04d1a1577a675fe50c02aa | |
parent | 9b74aa41d802b27e69e742c0306b633ad0ff3f89 (diff) |
Refactor API to use a Guard internally
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | plugins/plugin_fdman/src/file.rs | 27 | ||||
-rw-r--r-- | plugins/plugin_fdman/src/guard.rs | 20 | ||||
-rw-r--r-- | plugins/plugin_fdman/src/lib.rs | 5 | ||||
-rw-r--r-- | plugins/plugin_fdman/src/message/open_options.rs | 51 | ||||
-rw-r--r-- | plugins/plugin_fdman/src/plugin.rs | 7 |
5 files changed, 62 insertions, 48 deletions
diff --git a/plugins/plugin_fdman/src/file.rs b/plugins/plugin_fdman/src/file.rs new file mode 100644 index 00000000..012e35a3 --- /dev/null +++ b/plugins/plugin_fdman/src/file.rs @@ -0,0 +1,27 @@ +use crate::guard::Guard; + +#[derive(Debug)] +pub struct FileGuard { + file: std::fs::File, + guard: Guard, +} + +impl FileGuard { + pub(crate) fn new(file: std::fs::File, guard: Guard) -> Self { + Self { file, guard } + } +} + +impl std::ops::Deref for FileGuard { + type Target = std::fs::File; + + fn deref(&self) -> &Self::Target { + &self.file + } +} + +impl std::ops::DerefMut for FileGuard { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.file + } +} diff --git a/plugins/plugin_fdman/src/guard.rs b/plugins/plugin_fdman/src/guard.rs new file mode 100644 index 00000000..a464ef32 --- /dev/null +++ b/plugins/plugin_fdman/src/guard.rs @@ -0,0 +1,20 @@ +use std::sync::{atomic::AtomicU64, Arc}; + +#[derive(Debug)] +pub struct Guard { + tokens: u64, + handle_ref: Arc<AtomicU64>, +} + +impl Guard { + pub fn new(tokens: u64, handle_ref: Arc<AtomicU64>) -> Self { + Self { tokens, handle_ref } + } +} + +impl Drop for Guard { + fn drop(&mut self) { + self.handle_ref + .fetch_sub(self.tokens, std::sync::atomic::Ordering::SeqCst); + } +} diff --git a/plugins/plugin_fdman/src/lib.rs b/plugins/plugin_fdman/src/lib.rs index 71e12f2a..e7526604 100644 --- a/plugins/plugin_fdman/src/lib.rs +++ b/plugins/plugin_fdman/src/lib.rs @@ -1,9 +1,12 @@ mod builder; mod config; mod error; -pub mod message; +mod guard; mod plugin; +pub mod file; +pub mod message; + pub use crate::builder::FdManPluginBuilder; pub use crate::error::Error; pub use crate::plugin::FdManPlugin; diff --git a/plugins/plugin_fdman/src/message/open_options.rs b/plugins/plugin_fdman/src/message/open_options.rs index c8913052..39cc926e 100644 --- a/plugins/plugin_fdman/src/message/open_options.rs +++ b/plugins/plugin_fdman/src/message/open_options.rs @@ -1,10 +1,9 @@ -use std::{ - path::PathBuf, - sync::{atomic::AtomicU64, Arc}, -}; +use std::path::PathBuf; use tedge_api::Message; +use crate::file::FileGuard; + #[derive(Clone, Debug, bevy_reflect::TypeUuid)] #[uuid = "cf82a6ed-3fc1-4011-bbf0-c79fc6fdf781"] pub struct OpenOptions { @@ -81,42 +80,6 @@ impl tedge_api::message::AcceptsReplies for OpenOptions { type Reply = OpenOptionsResult; } -#[derive(Debug)] -pub struct OpenOptionsFile { - file: std::fs::File, - currently_held_count: Arc<AtomicU64>, -} - -impl OpenOptionsFile { - pub(crate) fn new(file: std::fs::File, currently_held_count: Arc<AtomicU64>) -> Self { - Self { - file, - currently_held_count, - } - } -} - -impl Drop for OpenOptionsFile { - fn drop(&mut self) { - self.currently_held_count - .fetch_sub(1, std::sync::atomic::Ordering::SeqCst); - } -} - -impl std::ops::Deref for OpenOptionsFile { - type Target = std::fs::File; - - fn deref(&self) -> &Self::Target { - &self.file - } -} - -impl std::ops::DerefMut for OpenOptionsFile { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.file - } -} - #[derive(Debug, thiserror::Error)] pub enum OpenOptionsError { #[error("FdMan Plugin error")] @@ -127,13 +90,13 @@ pub enum OpenOptionsError { #[uuid = "e259bed9-14b9-4edf-ab7b-e87d454b823f"] pub struct OpenOptionsResult { open_options: OpenOptions, - result: Result<OpenOptionsFile, OpenOptionsError>, + result: Result<FileGuard, OpenOptionsError>, } impl OpenOptionsResult { pub(crate) fn new( open_options: OpenOptions, - result: Result<OpenOptionsFile, OpenOptionsError>, + result: Result<FileGuard, OpenOptionsError>, ) -> Self { Self { open_options, @@ -145,11 +108,11 @@ impl OpenOptionsResult { &self.open_options } - pub fn result(&self) -> &Result<OpenOptionsFile, OpenOptionsError> { + pub fn result(&self) -> &Result<FileGuard, OpenOptionsError> { &self.result } - pub fn into_result(self) -> Result<OpenOptionsFile, OpenOptionsError> { + pub fn into_result(self) -> Result<FileGuard, OpenOptionsError> { self.result } } diff --git a/plugins/plugin_fdman/src/plugin.rs b/plugins/plugin_fdman/src/plugin.rs index de36855c..0c959162 100644 --- a/plugins/plugin_fdman/src/plugin.rs +++ b/plugins/plugin_fdman/src/plugin.rs @@ -9,6 +9,7 @@ use tedge_api::{ use crate::{ error::Error, + guard::Guard, message::{OpenOptions, OpenOptionsError, OpenOptionsResult}, }; @@ -74,16 +75,16 @@ impl Handle<OpenOptions> for FdManPlugin { Err(OpenOptionsError::from(err)), )) .map_err(|_| Error::SendingReply) - .map_err(PluginError::from) + .map_err(PluginError::from); } let file_result = message .as_std() .open(message.path()) .map(|file| { - crate::message::OpenOptionsFile::new( + crate::file::FileGuard::new( file, - self.currently_held_count.clone(), + Guard::new(aquired_handles, self.currently_held_count.clone()), ) }) .map_err(Error::from) |