mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
Асинхронный вариант MutexScope.
This commit is contained in:
@@ -15,7 +15,29 @@ where
|
|||||||
fn scope(&self, f: ScopeFn) -> ScopeFnOutput {
|
fn scope(&self, f: ScopeFn) -> ScopeFnOutput {
|
||||||
let mut lock = self.lock().unwrap();
|
let mut lock = self.lock().unwrap();
|
||||||
let inner = lock.deref_mut();
|
let inner = lock.deref_mut();
|
||||||
|
|
||||||
f(inner)
|
f(inner)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait MutexScopeAsync<T> {
|
||||||
|
async fn async_scope<'a, F, FnFut, FnOut>(&'a self, f: F) -> FnOut
|
||||||
|
where
|
||||||
|
FnFut: Future<Output = FnOut>,
|
||||||
|
F: FnOnce(&'a mut T) -> FnFut,
|
||||||
|
T: 'a;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> MutexScopeAsync<T> for Mutex<T> {
|
||||||
|
async fn async_scope<'a, F, FnFut, FnOut>(&'a self, f: F) -> FnOut
|
||||||
|
where
|
||||||
|
FnFut: Future<Output = FnOut>,
|
||||||
|
F: FnOnce(&'a mut T) -> FnFut,
|
||||||
|
T: 'a,
|
||||||
|
{
|
||||||
|
let mut guard = self.lock().unwrap();
|
||||||
|
|
||||||
|
let ptr: &'a mut T = unsafe { &mut *(guard.deref_mut() as *mut _) };
|
||||||
|
f(ptr).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user