-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new map trait that replaces LockMap
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
// 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)) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} | ||
|
||
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters