Skip to content

Commit 7b30711

Browse files
authoredDec 26, 2024··
Style Changes (#29)
* style: run cargo fmt on benchmarks * style: make use of lifetime elision, fixing new clippy lint * fix: change cast order to be infallible on 32-bit targets * style: address pedantic lints for variable names and syntax * style: simplify return type for infallible constructor * docs: update version number in dependency example
1 parent b9f1bdf commit 7b30711

File tree

6 files changed

+25
-32
lines changed

6 files changed

+25
-32
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The templated type `T` for `Synchronizer` can be any Rust struct implementing sp
5050
To use `mmap-sync`, add it to your `Cargo.toml` under `[dependencies]`:
5151
```toml
5252
[dependencies]
53-
mmap-sync = "1"
53+
mmap-sync = "2.0.0"
5454
```
5555
Then, import `mmap-sync` in your Rust program:
5656
```rust

‎benches/synchronizer.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ fn build_mock_data() -> (HelloWorld, AlignedVec) {
3030
(data, bytes)
3131
}
3232

33-
fn derive_shm_path(subpath : &str) -> String {
34-
const EV_NAME : &str = "MMAPSYNC_BM_ROOTDIR";
35-
const DEFAULT_ROOT : &str = "/dev/shm"; // respect original functionality
33+
fn derive_shm_path(subpath: &str) -> String {
34+
const EV_NAME: &str = "MMAPSYNC_BM_ROOTDIR";
35+
const DEFAULT_ROOT: &str = "/dev/shm"; // respect original functionality
3636

37-
let selected_root : String = match env::var(EV_NAME) {
37+
let selected_root: String = match env::var(EV_NAME) {
3838
Ok(val) => {
3939
let requested_root = val.trim();
4040

@@ -54,10 +54,8 @@ fn derive_shm_path(subpath : &str) -> String {
5454
DEFAULT_ROOT.into()
5555
}
5656
}
57-
},
58-
Err(_e) => {
59-
DEFAULT_ROOT.into()
6057
}
58+
Err(_e) => DEFAULT_ROOT.into(),
6159
};
6260

6361
format!("{selected_root}/{subpath}")

‎src/guard.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::ops::Deref;
1717

1818
use crate::instance::InstanceVersion;
1919
use crate::state::State;
20-
use crate::synchronizer::SynchronizerError;
2120

2221
/// An RAII implementation of a “scoped read lock” of a `State`
2322
pub(crate) struct ReadGuard<'a> {
@@ -27,16 +26,13 @@ pub(crate) struct ReadGuard<'a> {
2726

2827
impl<'a> ReadGuard<'a> {
2928
/// Creates new `ReadGuard` with specified parameters
30-
pub(crate) fn new(
31-
state: &'a mut State,
32-
version: InstanceVersion,
33-
) -> Result<Self, SynchronizerError> {
29+
pub(crate) fn new(state: &'a mut State, version: InstanceVersion) -> Self {
3430
state.rlock(version);
35-
Ok(ReadGuard { version, state })
31+
ReadGuard { version, state }
3632
}
3733
}
3834

39-
impl<'a> Drop for ReadGuard<'a> {
35+
impl Drop for ReadGuard<'_> {
4036
/// Unlocks stored `version` when `ReadGuard` goes out of scope
4137
fn drop(&mut self) {
4238
self.state.runlock(self.version);
@@ -52,9 +48,9 @@ pub struct ReadResult<'a, T: Archive> {
5248

5349
impl<'a, T: Archive> ReadResult<'a, T> {
5450
/// Creates new `ReadResult` with specified parameters
55-
pub(crate) fn new(_guard: ReadGuard<'a>, entity: &'a Archived<T>, switched: bool) -> Self {
51+
pub(crate) fn new(guard: ReadGuard<'a>, entity: &'a Archived<T>, switched: bool) -> Self {
5652
ReadResult {
57-
_guard,
53+
_guard: guard,
5854
entity,
5955
switched,
6056
}
@@ -66,7 +62,7 @@ impl<'a, T: Archive> ReadResult<'a, T> {
6662
}
6763
}
6864

69-
impl<'a, T: Archive> Deref for ReadResult<'a, T> {
65+
impl<T: Archive> Deref for ReadResult<'_, T> {
7066
type Target = Archived<T>;
7167

7268
/// Dereferences stored `entity` for easier access

‎src/locks.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ impl<'a> WriteLockStrategySealed<'a> for LockDisabled {
7777
}
7878
}
7979

80-
impl<'a> WriteLockStrategy<'a> for LockDisabled {}
80+
impl WriteLockStrategy<'_> for LockDisabled {}
8181

8282
pub struct DisabledGuard<'a>(&'a mut MmapMut);
8383

84-
impl<'a> Deref for DisabledGuard<'a> {
84+
impl Deref for DisabledGuard<'_> {
8585
type Target = MmapMut;
8686

8787
fn deref(&self) -> &Self::Target {
8888
&*self.0
8989
}
9090
}
9191

92-
impl<'a> DerefMut for DisabledGuard<'a> {
92+
impl DerefMut for DisabledGuard<'_> {
9393
fn deref_mut(&mut self) -> &mut Self::Target {
9494
&mut *self.0
9595
}
@@ -145,14 +145,14 @@ impl<'a> WriteLockStrategySealed<'a> for SingleWriter {
145145
}
146146

147147
#[cfg(unix)]
148-
impl<'a> WriteLockStrategy<'a> for SingleWriter {}
148+
impl WriteLockStrategy<'_> for SingleWriter {}
149149

150150
/// A simple guard which does not release the lock upon being dropped.
151151
#[cfg(unix)]
152152
pub struct SingleWriterGuard<'a>(&'a mut MmapMut);
153153

154154
#[cfg(unix)]
155-
impl<'a> Deref for SingleWriterGuard<'a> {
155+
impl Deref for SingleWriterGuard<'_> {
156156
type Target = MmapMut;
157157

158158
fn deref(&self) -> &Self::Target {
@@ -161,7 +161,7 @@ impl<'a> Deref for SingleWriterGuard<'a> {
161161
}
162162

163163
#[cfg(unix)]
164-
impl<'a> DerefMut for SingleWriterGuard<'a> {
164+
impl DerefMut for SingleWriterGuard<'_> {
165165
fn deref_mut(&mut self) -> &mut Self::Target {
166166
&mut *self.0
167167
}

‎src/state.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ impl State {
7272
num_readers.store(0, Ordering::SeqCst);
7373
reset = true;
7474
break;
75-
} else {
76-
thread::sleep(sleep_duration);
7775
}
76+
thread::sleep(sleep_duration);
7877
}
7978

8079
(next_idx, reset)
@@ -162,7 +161,7 @@ impl<'a, WL: WriteLockStrategy<'a>> StateContainer<WL> {
162161

163162
let mut need_init = false;
164163
// Reset state file size to match exactly `STATE_SIZE`
165-
if state_file.metadata().map_err(FailedStateRead)?.len() as usize != STATE_SIZE {
164+
if state_file.metadata().map_err(FailedStateRead)?.len() != STATE_SIZE as u64 {
166165
state_file
167166
.set_len(STATE_SIZE as u64)
168167
.map_err(FailedStateRead)?;
@@ -175,9 +174,9 @@ impl<'a, WL: WriteLockStrategy<'a>> StateContainer<WL> {
175174
let new_state = State::default();
176175
unsafe {
177176
mmap.as_mut_ptr()
178-
.copy_from((&new_state as *const State) as *const u8, STATE_SIZE)
179-
};
180-
};
177+
.copy_from((&new_state as *const State) as *const u8, STATE_SIZE);
178+
}
179+
}
181180

182181
self.mmap = Some(WL::new(mmap, state_file));
183182
Ok(())

‎src/synchronizer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ where
219219
pub unsafe fn read<T>(
220220
&'a mut self,
221221
check_bytes: bool,
222-
) -> Result<ReadResult<T>, SynchronizerError>
222+
) -> Result<ReadResult<'a, T>, SynchronizerError>
223223
where
224224
T: Archive,
225225
T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
@@ -231,7 +231,7 @@ where
231231
let version = state.version()?;
232232

233233
// create and lock state guard for reading
234-
let guard = ReadGuard::new(state, version)?;
234+
let guard = ReadGuard::new(state, version);
235235

236236
// fetch data for current version from mapped memory
237237
let (data, switched) = self.data_container.data(version)?;

0 commit comments

Comments
 (0)
Please sign in to comment.