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:
@@ -19,3 +19,25 @@ where
|
||||
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