-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Clarify the default error type The 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.
|
||
|
||
// 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)) | ||
// } | ||
// } |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Inconsistent error handling between The
|
||
|
||
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), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
:And update conditional compilation in the codebase accordingly.