mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Использование функции для осуществления операций в базе данных вместо ручного блокирования мьютекса.
This commit is contained in:
21
src/utility/mutex.rs
Normal file
21
src/utility/mutex.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use std::ops::DerefMut;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub trait MutexScope<T, ScopeFn, ScopeFnOutput>
|
||||
where
|
||||
ScopeFn: FnOnce(&mut T) -> ScopeFnOutput,
|
||||
{
|
||||
fn scope(&self, f: ScopeFn) -> ScopeFnOutput;
|
||||
}
|
||||
|
||||
impl<T, ScopeFn, ScopeFnOutput> MutexScope<T, ScopeFn, ScopeFnOutput> for Mutex<T>
|
||||
where
|
||||
ScopeFn: FnOnce(&mut T) -> ScopeFnOutput,
|
||||
{
|
||||
fn scope(&self, f: ScopeFn) -> ScopeFnOutput {
|
||||
let mut lock = self.lock().unwrap();
|
||||
let inner = lock.deref_mut();
|
||||
|
||||
f(inner)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user