use std::cell::RefCell;
thread_local! {
static COMMAND_POLICY_HOOK_DEPTH: RefCell<usize> = const { RefCell::new(0) };
}
pub(crate) fn enter_command_policy_hook() -> HookDepthGuard {
COMMAND_POLICY_HOOK_DEPTH.with(|depth| *depth.borrow_mut() += 1);
HookDepthGuard
}
pub(crate) struct HookDepthGuard;
impl Drop for HookDepthGuard {
fn drop(&mut self) {
COMMAND_POLICY_HOOK_DEPTH.with(|depth| {
let mut depth = depth.borrow_mut();
*depth = depth.saturating_sub(1);
});
}
}
pub fn command_policy_hook_depth() -> usize {
COMMAND_POLICY_HOOK_DEPTH.with(|depth| *depth.borrow())
}
pub(crate) fn reset_command_policy_hook_depth() {
COMMAND_POLICY_HOOK_DEPTH.with(|depth| *depth.borrow_mut() = 0);
}
pub(crate) fn swap_command_policy_hook_depth(next: usize) -> usize {
COMMAND_POLICY_HOOK_DEPTH.with(|depth| std::mem::replace(&mut *depth.borrow_mut(), next))
}