-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turned Mutex as generics in CachingLocal
- Loading branch information
Showing
5 changed files
with
194 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub(crate) mod date; | |
pub mod multi; | ||
pub mod locked; | ||
pub mod redis; | ||
pub mod utils; |
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,46 @@ | ||
use async_trait::async_trait; | ||
use std::fmt::Debug; | ||
use tokio::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; | ||
|
||
#[async_trait] | ||
pub trait MutexLike: Send + Sync + Debug { | ||
type T: Send; | ||
fn new(value: Self::T) -> Self; | ||
|
||
async fn lock(&self) -> MutexGuard<'_, Self::T>; | ||
} | ||
|
||
#[async_trait] | ||
impl<T: Send + Debug> MutexLike for Mutex<T> { | ||
type T = T; | ||
|
||
fn new(value: Self::T) -> Self { | ||
Self::new(value) | ||
} | ||
|
||
async fn lock(&self) -> MutexGuard<'_, Self::T> { | ||
Mutex::lock(self).await | ||
} | ||
} | ||
|
||
#[async_trait] | ||
pub trait Locker: Send + Sync { | ||
fn new() -> Self; | ||
async fn read(&self) -> RwLockReadGuard<'_, ()>; | ||
async fn write(&self) -> RwLockWriteGuard<'_, ()>; | ||
} | ||
|
||
#[async_trait] | ||
impl Locker for RwLock<()> { | ||
fn new() -> Self { | ||
Self::new(()) | ||
} | ||
|
||
async fn read(&self) -> RwLockReadGuard<'_, ()> { | ||
RwLock::read(self).await | ||
} | ||
|
||
async fn write(&self) -> RwLockWriteGuard<'_, ()> { | ||
RwLock::write(self).await | ||
} | ||
} |
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 @@ | ||
pub mod lock; |