From e0f7fbba2ebb4dcac5f06dd242b7da7c66e9cd2c Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Sat, 27 Jan 2024 01:28:13 +0100 Subject: [PATCH] [#96] ProcessState state method is const --- iceoryx2-bb/posix/src/process_state.rs | 80 +++++++++++-------- .../posix/tests/process_state_tests.rs | 12 +-- iceoryx2-bb/system-types/src/path.rs | 1 + 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/iceoryx2-bb/posix/src/process_state.rs b/iceoryx2-bb/posix/src/process_state.rs index f4c805387..3cdcebf83 100644 --- a/iceoryx2-bb/posix/src/process_state.rs +++ b/iceoryx2-bb/posix/src/process_state.rs @@ -77,6 +77,8 @@ //! } //! ``` +use std::fmt::Debug; + pub use iceoryx2_bb_container::semantic_string::SemanticString; use iceoryx2_bb_elementary::enum_gen; use iceoryx2_bb_log::{debug, error, fail, fatal_panic, trace, warn}; @@ -415,12 +417,22 @@ impl ProcessGuard { /// ProcessState::DoesNotExist => (), /// } /// ``` -#[derive(Debug)] pub struct ProcessMonitor { - file: Option, + file: core::cell::Cell>, path: FilePath, } +impl Debug for ProcessMonitor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "ProcessMonitor {{ file = {:?}, path = {:?}}}", + unsafe { &*self.file.as_ptr() }, + self.path + ) + } +} + impl ProcessMonitor { /// Creates a new [`ProcessMonitor`] that can obtain the state of the process that will be /// monitored. @@ -435,8 +447,8 @@ impl ProcessMonitor { /// let mut monitor = ProcessMonitor::new(&process_state_path).expect(""); /// ``` pub fn new(path: &FilePath) -> Result { - let mut new_self = Self { - file: None, + let new_self = Self { + file: core::cell::Cell::new(None), path: *path, }; @@ -477,9 +489,9 @@ impl ProcessMonitor { /// ProcessState::DoesNotExist => (), /// } /// ``` - pub fn state(&mut self) -> Result { + pub fn state(&self) -> Result { let msg = "Unable to acquire ProcessState"; - match self.file { + match unsafe { &*self.file.as_ptr() } { Some(_) => self.read_state_from_file(), None => match File::does_exist(&self.path) { Ok(true) => { @@ -496,8 +508,8 @@ impl ProcessMonitor { } } - fn read_state_from_file(&mut self) -> Result { - let file = match self.file { + fn read_state_from_file(&self) -> Result { + let file = match unsafe { &*self.file.as_ptr() } { Some(ref f) => f, None => return Ok(ProcessState::InInitialization), }; @@ -526,12 +538,12 @@ impl ProcessMonitor { Ok(true) => match file.permission() { Ok(INIT_PERMISSION) => Ok(ProcessState::InInitialization), Err(_) | Ok(_) => { - self.file = None; + self.file.set(None); Ok(ProcessState::Dead) } }, Ok(false) => { - self.file = None; + self.file.set(None); Ok(ProcessState::DoesNotExist) } Err(v) => { @@ -542,39 +554,41 @@ impl ProcessMonitor { } } - fn open_file(&mut self) -> Result<(), ProcessMonitorCreateError> { + fn open_file(&self) -> Result<(), ProcessMonitorCreateError> { let origin = "ProcessMonitor::new()"; let msg = format!( "Unable to open ProcessMonitor with the file \"{}\"", self.path ); - self.file = match FileBuilder::new(&self.path).open_existing(AccessMode::Read) { - Ok(f) => Some(f), - Err(FileOpenError::FileDoesNotExist) => None, - Err(FileOpenError::IsDirectory) => { - fail!(from origin, with ProcessMonitorCreateError::IsDirectory, + self.file.set( + match FileBuilder::new(&self.path).open_existing(AccessMode::Read) { + Ok(f) => Some(f), + Err(FileOpenError::FileDoesNotExist) => None, + Err(FileOpenError::IsDirectory) => { + fail!(from origin, with ProcessMonitorCreateError::IsDirectory, "{} since the path is a directory.", msg); - } - Err(FileOpenError::InsufficientPermissions) => { - if FileBuilder::new(&self.path) - .open_existing(AccessMode::Write) - .is_ok() - { - None - } else { - fail!(from origin, with ProcessMonitorCreateError::InsufficientPermissions, + } + Err(FileOpenError::InsufficientPermissions) => { + if FileBuilder::new(&self.path) + .open_existing(AccessMode::Write) + .is_ok() + { + None + } else { + fail!(from origin, with ProcessMonitorCreateError::InsufficientPermissions, "{} due to insufficient permissions.", msg); + } } - } - Err(FileOpenError::Interrupt) => { - fail!(from origin, with ProcessMonitorCreateError::Interrupt, + Err(FileOpenError::Interrupt) => { + fail!(from origin, with ProcessMonitorCreateError::Interrupt, "{} since an interrupt signal was received.", msg); - } - Err(v) => { - fail!(from origin, with ProcessMonitorCreateError::UnknownError, + } + Err(v) => { + fail!(from origin, with ProcessMonitorCreateError::UnknownError, "{} since an unknown failure occurred ({:?}).", msg, v); - } - }; + } + }, + ); Ok(()) } diff --git a/iceoryx2-bb/posix/tests/process_state_tests.rs b/iceoryx2-bb/posix/tests/process_state_tests.rs index 126cd0062..5db85add9 100644 --- a/iceoryx2-bb/posix/tests/process_state_tests.rs +++ b/iceoryx2-bb/posix/tests/process_state_tests.rs @@ -94,7 +94,7 @@ pub fn process_state_watcher_detects_alive_state_from_existing_process() { let path = generate_file_path(); let guard = ProcessGuard::new(&path).unwrap(); - let mut watcher = ProcessMonitor::new(&path).unwrap(); + let watcher = ProcessMonitor::new(&path).unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::Alive); drop(guard); @@ -109,7 +109,7 @@ pub fn process_state_watcher_detects_dead_state() { .creation_mode(CreationMode::PurgeAndCreate) .create() .unwrap(); - let mut watcher = ProcessMonitor::new(&path).unwrap(); + let watcher = ProcessMonitor::new(&path).unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::Dead); file.remove_self().unwrap(); @@ -120,7 +120,7 @@ pub fn process_state_watcher_detects_dead_state() { pub fn process_state_watcher_detects_non_existing_state() { let path = generate_file_path(); - let mut watcher = ProcessMonitor::new(&path).unwrap(); + let watcher = ProcessMonitor::new(&path).unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::DoesNotExist); } @@ -128,7 +128,7 @@ pub fn process_state_watcher_detects_non_existing_state() { pub fn process_state_watcher_transitions_work_starting_from_non_existing_process() { let path = generate_file_path(); - let mut watcher = ProcessMonitor::new(&path).unwrap(); + let watcher = ProcessMonitor::new(&path).unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::DoesNotExist); let file = FileBuilder::new(&path) .creation_mode(CreationMode::PurgeAndCreate) @@ -148,7 +148,7 @@ pub fn process_state_watcher_transitions_work_starting_from_existing_process() { .create() .unwrap(); - let mut watcher = ProcessMonitor::new(&path).unwrap(); + let watcher = ProcessMonitor::new(&path).unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::Dead); file.remove_self().unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::DoesNotExist); @@ -172,7 +172,7 @@ pub fn process_state_watcher_detects_initialized_state() { .create() .unwrap(); - let mut watcher = ProcessMonitor::new(&path).unwrap(); + let watcher = ProcessMonitor::new(&path).unwrap(); assert_that!(watcher.state().unwrap(), eq ProcessState::InInitialization); file.set_permission(Permission::OWNER_ALL).unwrap(); file.remove_self().unwrap(); diff --git a/iceoryx2-bb/system-types/src/path.rs b/iceoryx2-bb/system-types/src/path.rs index bf7fbfe77..95c2b755d 100644 --- a/iceoryx2-bb/system-types/src/path.rs +++ b/iceoryx2-bb/system-types/src/path.rs @@ -29,6 +29,7 @@ pub use iceoryx2_bb_container::semantic_string::SemanticString; +use core::hash::{Hash, Hasher}; use iceoryx2_bb_container::byte_string::FixedSizeByteString; use iceoryx2_bb_container::semantic_string;