use crate::*;
impl HookContext {
pub fn from_inner(inner: *mut HookContextInner) -> Self {
HookContext { inner }
}
pub(crate) fn get_inner(&self) -> &'static mut HookContextInner {
unsafe { &mut *self.inner }
}
pub fn reset_hook_index(&mut self) {
self.get_inner().set_hook_index(0);
}
pub fn set_arm_changed(&mut self, changed: bool) {
let inner: &mut HookContextInner = self.get_inner();
if inner.get_arm_changed() != changed {
let cleanups: Vec<Box<dyn FnOnce()>> = take(inner.get_mut_cleanups());
for cleanup in cleanups {
cleanup();
}
inner.get_mut_hooks().clear();
inner.set_arm_changed(changed);
}
self.reset_hook_index();
}
}
impl Clone for HookContext {
fn clone(&self) -> Self {
*self
}
}
impl Copy for HookContext {}
impl Default for HookContext {
fn default() -> Self {
let boxed: Box<HookContextInner> = Box::default();
HookContext::from_inner(Box::leak(boxed) as *mut HookContextInner)
}
}
impl HookContextInner {
pub const fn new() -> Self {
HookContextInner {
hooks: Vec::new(),
arm_changed: false,
hook_index: 0_usize,
cleanups: Vec::new(),
}
}
}
impl Default for HookContextInner {
fn default() -> Self {
Self::new()
}
}
impl HookContextCell {
pub const fn get_inner(&self) -> &'static mut HookContextInner {
unsafe { &mut *self.0.get() }
}
}
unsafe impl Sync for HookContextCell {}