use crate::reentrant::error::Reentered;
use crate::reentrant::reset::Reset;
use crate::token::{with_fresh_token, ExclusiveFamily, ExclusiveToken};
use core::cell::Cell;
#[derive(Debug, Default)]
pub struct ReentrancyCell {
active: Cell<bool>,
}
impl ReentrancyCell {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
active: Cell::new(false),
}
}
#[inline]
#[must_use]
pub fn is_active(&self) -> bool {
self.active.get()
}
#[inline]
pub fn enter<R>(
&self,
f: impl for<'brand> FnOnce(ExclusiveToken<'brand>) -> R,
) -> Result<R, Reentered> {
let _reset = Reset::acquire(&self.active)?;
Ok(with_fresh_token::<ExclusiveFamily, _, _>(f))
}
}