diff --git a/README.md b/README.md index 3803de6..668abb0 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The templated type `T` for `Synchronizer` can be any Rust struct implementing sp To use `mmap-sync`, add it to your `Cargo.toml` under `[dependencies]`: ```toml [dependencies] -mmap-sync = "1" +mmap-sync = "2.0.0" ``` Then, import `mmap-sync` in your Rust program: ```rust diff --git a/benches/synchronizer.rs b/benches/synchronizer.rs index c0f2a1a..26c28a3 100644 --- a/benches/synchronizer.rs +++ b/benches/synchronizer.rs @@ -30,11 +30,11 @@ fn build_mock_data() -> (HelloWorld, AlignedVec) { (data, bytes) } -fn derive_shm_path(subpath : &str) -> String { - const EV_NAME : &str = "MMAPSYNC_BM_ROOTDIR"; - const DEFAULT_ROOT : &str = "/dev/shm"; // respect original functionality +fn derive_shm_path(subpath: &str) -> String { + const EV_NAME: &str = "MMAPSYNC_BM_ROOTDIR"; + const DEFAULT_ROOT: &str = "/dev/shm"; // respect original functionality - let selected_root : String = match env::var(EV_NAME) { + let selected_root: String = match env::var(EV_NAME) { Ok(val) => { let requested_root = val.trim(); @@ -54,10 +54,8 @@ fn derive_shm_path(subpath : &str) -> String { DEFAULT_ROOT.into() } } - }, - Err(_e) => { - DEFAULT_ROOT.into() } + Err(_e) => DEFAULT_ROOT.into(), }; format!("{selected_root}/{subpath}") diff --git a/src/guard.rs b/src/guard.rs index daa18d8..bd34d29 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -17,7 +17,6 @@ use std::ops::Deref; use crate::instance::InstanceVersion; use crate::state::State; -use crate::synchronizer::SynchronizerError; /// An RAII implementation of a “scoped read lock” of a `State` pub(crate) struct ReadGuard<'a> { @@ -27,16 +26,13 @@ pub(crate) struct ReadGuard<'a> { impl<'a> ReadGuard<'a> { /// Creates new `ReadGuard` with specified parameters - pub(crate) fn new( - state: &'a mut State, - version: InstanceVersion, - ) -> Result { + pub(crate) fn new(state: &'a mut State, version: InstanceVersion) -> Self { state.rlock(version); - Ok(ReadGuard { version, state }) + ReadGuard { version, state } } } -impl<'a> Drop for ReadGuard<'a> { +impl Drop for ReadGuard<'_> { /// Unlocks stored `version` when `ReadGuard` goes out of scope fn drop(&mut self) { self.state.runlock(self.version); @@ -52,9 +48,9 @@ pub struct ReadResult<'a, T: Archive> { impl<'a, T: Archive> ReadResult<'a, T> { /// Creates new `ReadResult` with specified parameters - pub(crate) fn new(_guard: ReadGuard<'a>, entity: &'a Archived, switched: bool) -> Self { + pub(crate) fn new(guard: ReadGuard<'a>, entity: &'a Archived, switched: bool) -> Self { ReadResult { - _guard, + _guard: guard, entity, switched, } @@ -66,7 +62,7 @@ impl<'a, T: Archive> ReadResult<'a, T> { } } -impl<'a, T: Archive> Deref for ReadResult<'a, T> { +impl Deref for ReadResult<'_, T> { type Target = Archived; /// Dereferences stored `entity` for easier access diff --git a/src/locks.rs b/src/locks.rs index 88e2ae1..4d9f584 100644 --- a/src/locks.rs +++ b/src/locks.rs @@ -77,11 +77,11 @@ impl<'a> WriteLockStrategySealed<'a> for LockDisabled { } } -impl<'a> WriteLockStrategy<'a> for LockDisabled {} +impl WriteLockStrategy<'_> for LockDisabled {} pub struct DisabledGuard<'a>(&'a mut MmapMut); -impl<'a> Deref for DisabledGuard<'a> { +impl Deref for DisabledGuard<'_> { type Target = MmapMut; fn deref(&self) -> &Self::Target { @@ -89,7 +89,7 @@ impl<'a> Deref for DisabledGuard<'a> { } } -impl<'a> DerefMut for DisabledGuard<'a> { +impl DerefMut for DisabledGuard<'_> { fn deref_mut(&mut self) -> &mut Self::Target { &mut *self.0 } @@ -145,14 +145,14 @@ impl<'a> WriteLockStrategySealed<'a> for SingleWriter { } #[cfg(unix)] -impl<'a> WriteLockStrategy<'a> for SingleWriter {} +impl WriteLockStrategy<'_> for SingleWriter {} /// A simple guard which does not release the lock upon being dropped. #[cfg(unix)] pub struct SingleWriterGuard<'a>(&'a mut MmapMut); #[cfg(unix)] -impl<'a> Deref for SingleWriterGuard<'a> { +impl Deref for SingleWriterGuard<'_> { type Target = MmapMut; fn deref(&self) -> &Self::Target { @@ -161,7 +161,7 @@ impl<'a> Deref for SingleWriterGuard<'a> { } #[cfg(unix)] -impl<'a> DerefMut for SingleWriterGuard<'a> { +impl DerefMut for SingleWriterGuard<'_> { fn deref_mut(&mut self) -> &mut Self::Target { &mut *self.0 } diff --git a/src/state.rs b/src/state.rs index 4084893..b4de164 100644 --- a/src/state.rs +++ b/src/state.rs @@ -72,9 +72,8 @@ impl State { num_readers.store(0, Ordering::SeqCst); reset = true; break; - } else { - thread::sleep(sleep_duration); } + thread::sleep(sleep_duration); } (next_idx, reset) @@ -162,7 +161,7 @@ impl<'a, WL: WriteLockStrategy<'a>> StateContainer { let mut need_init = false; // Reset state file size to match exactly `STATE_SIZE` - if state_file.metadata().map_err(FailedStateRead)?.len() as usize != STATE_SIZE { + if state_file.metadata().map_err(FailedStateRead)?.len() != STATE_SIZE as u64 { state_file .set_len(STATE_SIZE as u64) .map_err(FailedStateRead)?; @@ -175,9 +174,9 @@ impl<'a, WL: WriteLockStrategy<'a>> StateContainer { let new_state = State::default(); unsafe { mmap.as_mut_ptr() - .copy_from((&new_state as *const State) as *const u8, STATE_SIZE) - }; - }; + .copy_from((&new_state as *const State) as *const u8, STATE_SIZE); + } + } self.mmap = Some(WL::new(mmap, state_file)); Ok(()) diff --git a/src/synchronizer.rs b/src/synchronizer.rs index 2cf87d2..179fba9 100644 --- a/src/synchronizer.rs +++ b/src/synchronizer.rs @@ -219,7 +219,7 @@ where pub unsafe fn read( &'a mut self, check_bytes: bool, - ) -> Result, SynchronizerError> + ) -> Result, SynchronizerError> where T: Archive, T::Archived: for<'b> CheckBytes>, @@ -231,7 +231,7 @@ where let version = state.version()?; // create and lock state guard for reading - let guard = ReadGuard::new(state, version)?; + let guard = ReadGuard::new(state, version); // fetch data for current version from mapped memory let (data, switched) = self.data_container.data(version)?;