use crate::*;
impl From<usize> for &'static mut HookContextInner {
#[inline(always)]
fn from(address: usize) -> Self {
unsafe { &mut *(address as *mut HookContextInner) }
}
}
impl From<usize> for &'static HookContextInner {
#[inline(always)]
fn from(address: usize) -> Self {
unsafe { &*(address as *const HookContextInner) }
}
}
impl From<HookContext> for usize {
#[inline(always)]
fn from(context: HookContext) -> Self {
context.inner as usize
}
}
impl From<usize> for HookContext {
#[inline(always)]
fn from(address: usize) -> Self {
HookContext {
inner: address as *mut HookContextInner,
}
}
}
impl HookContext {
#[allow(clippy::mut_from_ref)]
pub fn leak_mut(&self) -> &'static mut HookContextInner {
let address: usize = (*self).into();
address.into()
}
pub fn reset_hook_index(&mut self) {
self.leak_mut().set_hook_index(0);
}
pub fn set_arm_changed(&mut self, changed: bool) {
let inner: &mut HookContextInner = self.leak_mut();
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();
let ptr: *mut HookContextInner = Box::leak(boxed) as *mut HookContextInner;
let address: usize = ptr as usize;
address.into()
}
}
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 {}