Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style Changes #29

Merged
merged 6 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions benches/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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}")
Expand Down
16 changes: 6 additions & 10 deletions src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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<Self, SynchronizerError> {
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);
Expand All @@ -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<T>, switched: bool) -> Self {
pub(crate) fn new(guard: ReadGuard<'a>, entity: &'a Archived<T>, switched: bool) -> Self {
ReadResult {
_guard,
_guard: guard,
entity,
switched,
}
Expand All @@ -66,7 +62,7 @@ impl<'a, T: Archive> ReadResult<'a, T> {
}
}

impl<'a, T: Archive> Deref for ReadResult<'a, T> {
impl<T: Archive> Deref for ReadResult<'_, T> {
type Target = Archived<T>;

/// Dereferences stored `entity` for easier access
Expand Down
12 changes: 6 additions & 6 deletions src/locks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ 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 {
&*self.0
}
}

impl<'a> DerefMut for DisabledGuard<'a> {
impl DerefMut for DisabledGuard<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self.0
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
11 changes: 5 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -162,7 +161,7 @@ impl<'a, WL: WriteLockStrategy<'a>> StateContainer<WL> {

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)?;
Expand All @@ -175,9 +174,9 @@ impl<'a, WL: WriteLockStrategy<'a>> StateContainer<WL> {
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(())
Expand Down
4 changes: 2 additions & 2 deletions src/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ where
pub unsafe fn read<T>(
&'a mut self,
check_bytes: bool,
) -> Result<ReadResult<T>, SynchronizerError>
) -> Result<ReadResult<'a, T>, SynchronizerError>
where
T: Archive,
T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
Expand All @@ -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)?;
Expand Down
Loading