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

Added new Map trait that replaces LockMap #60

Merged
merged 1 commit into from
Dec 4, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = ["quork-proc"]
[dependencies]
cfg-if = "1.0"
lock_api = { version = "0.4", optional = true }
parking_lot = { version = "0.12", optional = true }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add feature flag for parking_lot dependency.

The parking_lot crate is added as an optional dependency but does not have an associated feature flag. To allow users to enable or disable this dependency, consider adding a feature flag.

Apply this change to define a feature for parking_lot:

[features]
parking_lot = ["parking_lot"]

And update conditional compilation in the codebase accordingly.

quork-proc = { version = "0.3", path = "quork-proc", optional = true }
spin = { version = "0.9", optional = true }
thiserror = { version = "1.0" }
Expand Down
5 changes: 5 additions & 0 deletions src/traits/lock.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#![deprecated(
since = "0.8.0",
note = "Use the `map::Map` trait instead. This trait will be removed in the future."
)]

//! Map a Mutex Lock
//!
//! Can theoretically be anything but designed primarily for Mutex Locks
Expand Down
41 changes: 41 additions & 0 deletions src/traits/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Provides a generalized map function
//!
//! This is primarily designed for mapping Mutex types, but can realistically be used for anything in future.

mod mutex;

#[allow(clippy::module_name_repetitions)]
/// Map a mutex lock
pub trait Map<'a, 'g, T: ?Sized, G: 'g> {
/// Maps a value of T to a value of U
fn map<F, U>(&'a self, f: F) -> U
where
F: FnOnce(G) -> U + 'g,
'a: 'g;
}

#[allow(clippy::module_name_repetitions)]
/// Attempts to map a mutex lock
pub trait TryMap<'a, 'g, T: ?Sized, G: 'g, E = G> {
/// Maps a value of T to a value of U
///
/// # Errors
/// - Locking the mutex failed
fn try_map<F, U>(&'a self, f: F) -> Result<U, E>
where
F: FnOnce(G) -> U + 'g,
'a: 'g;
}
Comment on lines +19 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Clarify the default error type E in TryMap trait.

The TryMap trait defines a default type parameter E = G, which may not be immediately clear to users. Since G represents a guard type, using it as an error type could be confusing. Consider defining a more explicit error type or providing additional documentation on the intended usage.

Apply this change to clarify the error type:

- pub trait TryMap<'a, 'g, T: ?Sized, G: 'g, E = G> {
+ pub trait TryMap<'a, 'g, T: ?Sized, G: 'g, E> {

And update implementations accordingly.

Committable suggestion skipped: line range outside the PR's diff.


// impl<'a, 'g, T: ?Sized, G: 'g, S> TryMap<'a, 'g, T, G> for S
// where
// S: Map<'a, 'g, T, G>,
// {
// fn try_map<F, U>(&'a self, f: F) -> Result<U, G>
// where
// F: FnOnce(G) -> U + 'g,
// 'a: 'g,
// {
// Ok(self.map(f))
// }
// }
56 changes: 56 additions & 0 deletions src/traits/map/mutex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#[cfg(feature = "parking_lot")]
impl<'a, 'g, T> super::Map<'a, 'g, T, parking_lot::MutexGuard<'g, T>> for parking_lot::Mutex<T> {
fn map<F, U>(&'a self, f: F) -> U
where
F: FnOnce(parking_lot::MutexGuard<'g, T>) -> U,
'a: 'g,
{
f(self.lock())
}
}

#[cfg(feature = "parking_lot")]
#[derive(Debug, thiserror::Error)]
#[error("Failed to lock the mutex")]
/// Failed to lock the mutex
pub struct LockError;

#[cfg(feature = "parking_lot")]
impl<'a, 'g, T> super::TryMap<'a, 'g, T, parking_lot::MutexGuard<'g, T>, LockError>
for parking_lot::Mutex<T>
{
fn try_map<F, U>(&'a self, f: F) -> Result<U, LockError>
where
F: FnOnce(parking_lot::MutexGuard<'g, T>) -> U,
'a: 'g,
{
match self.try_lock() {
Some(guard) => Ok(f(guard)),
None => Err(LockError),
}
}
}
Comment on lines +22 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Inconsistent error handling between parking_lot::Mutex and std::sync::Mutex.

The try_map method for parking_lot::Mutex returns a custom LockError, whereas the try_map method for std::sync::Mutex returns std::sync::TryLockError. This inconsistency can lead to confusion and complicate error handling for users working with both mutex types. Consider standardizing the error types by either:

  • Returning std::sync::TryLockError in both implementations, or
  • Defining a common error trait or enum to encapsulate different locking errors.


impl<'a, 'g, T>
super::TryMap<
'a,
'g,
T,
std::sync::MutexGuard<'g, T>,
std::sync::TryLockError<std::sync::MutexGuard<'g, T>>,
> for std::sync::Mutex<T>
{
fn try_map<F, U>(
&'a self,
f: F,
) -> Result<U, std::sync::TryLockError<std::sync::MutexGuard<'g, T>>>
where
F: FnOnce(std::sync::MutexGuard<'g, T>) -> U,
'a: 'g,
{
match self.try_lock() {
Ok(guard) => Ok(f(guard)),
Err(e) => Err(e),
}
}
}
2 changes: 2 additions & 0 deletions src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cfg_if::cfg_if! {
pub mod flip;
pub mod lock;
pub mod truncate;
pub mod map;
}
}

Expand All @@ -20,6 +21,7 @@ pub mod prelude {
pub use super::flip::*;
pub use super::lock::*;
pub use super::truncate::*;
pub use super::map::*;
}
}

Expand Down
Loading