use super::*;
#[derive(Debug, Default)]
pub(super) struct GuardWithDeferredDrop<'m> {
guard: Option<MutexGuard<'m, State>>,
deferred_drop: DeferredDrop,
}
pub(super) type DeferredDrop = Vec<drop_reentrancy::ProtectedArc<dyn IsParticipant>>;
impl<'m> GuardWithDeferredDrop<'m> {
pub(super) fn new(guard: MutexGuard<'m, State>) -> Self {
GuardWithDeferredDrop {
guard: Some(guard),
deferred_drop: vec![],
}
}
pub(super) fn deref_mut_both(&mut self) -> (&mut State, &mut DeferredDrop) {
(
self.guard.as_mut().expect("deref_mut after drop"),
&mut self.deferred_drop,
)
}
}
impl std::ops::Deref for GuardWithDeferredDrop<'_> {
type Target = State;
fn deref(&self) -> &State {
self.guard.as_ref().expect("deref after drop")
}
}
impl std::ops::DerefMut for GuardWithDeferredDrop<'_> {
fn deref_mut(&mut self) -> &mut State {
self.deref_mut_both().0
}
}
impl Drop for GuardWithDeferredDrop<'_> {
fn drop(&mut self) {
let guard = self.guard.take().expect("dropping twice!");
drop::<MutexGuard<_>>(guard);
for p in self.deferred_drop.drain(..) {
p.promise_dropping_is_ok();
}
}
}