pub struct InlineHookMap { /* private fields */ }Available on crate features
inline and std only.Expand description
A type-erased map of inline hooks indexed by target function pointer.
A collection of inline hooks that can be enabled/disabled together.
If you just want to store hooks of the same function type, just use
HashMap<F, InlineHook<F>> instead of InlineHookMap.
§Examples
// cargo add ib-hook --features inline
use ib_hook::inline::{InlineHook, InlineHookMap};
type MyFn = extern "system" fn(u32) -> u32;
extern "system" fn original1(x: u32) -> u32 { x + 1 }
extern "system" fn original2(x: u32) -> u32 { x + 2 }
extern "system" fn hooked1(x: u32) -> u32 { x + 0o721 }
extern "system" fn hooked2(x: u32) -> u32 { x + 0o722 }
// Create a collection of hooks
let mut hooks = InlineHookMap::new();
hooks.insert::<MyFn>(original1, hooked1);
// Insert and enable a hook
hooks.insert::<MyFn>(original2, hooked2).enable().unwrap();
// Enable all hooks at once
hooks.enable().on_error(|target, e| eprintln!("Target {target:?} failed: {e:?}"));
// Verify hooks are enabled
assert_eq!(original1(0x100), 721); // redirected to hooked1
assert_eq!(original2(0x100), 722); // redirected to hooked2
// Disable all hooks at once
hooks.disable().on_error(|target, e| eprintln!("Target {target:?} failed: {e:?}"));
// Verify hooks are disabled
assert_eq!(original1(0x100), 0x101); // back to original
assert_eq!(original2(0x100), 0x102); // back to original
// Access individual hooks by target function
if let Some(hook) = hooks.get::<MyFn>(original1) {
println!("Hook is enabled: {}", hook.is_enabled());
}Implementations§
Source§impl InlineHookMap
impl InlineHookMap
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty InlineHookMap.
Sourcepub fn hooks<'a>(&'a self) -> Values<'a, fn(), InlineHook<fn()>>
pub fn hooks<'a>(&'a self) -> Values<'a, fn(), InlineHook<fn()>>
Returns an iterator of the hooks.
Sourcepub fn hooks_mut<'a>(&'a mut self) -> ValuesMut<'a, fn(), InlineHook<fn()>>
pub fn hooks_mut<'a>(&'a mut self) -> ValuesMut<'a, fn(), InlineHook<fn()>>
Returns a mutable iterator of the hooks.
Sourcepub fn insert<'a, F: FnPtr>(
&'a mut self,
target: F,
detour: F,
) -> &'a mut InlineHook<F>
pub fn insert<'a, F: FnPtr>( &'a mut self, target: F, detour: F, ) -> &'a mut InlineHook<F>
Add a new hook to the collection.
The hook is created but not enabled. Use enable() to enable it.
Sourcepub fn get<F: FnPtr>(&self, target: F) -> Option<&InlineHook<F>>
pub fn get<F: FnPtr>(&self, target: F) -> Option<&InlineHook<F>>
Get a reference to a specific hook by target function.
Sourcepub fn get_mut<F: FnPtr>(&mut self, target: F) -> Option<&mut InlineHook<F>>
pub fn get_mut<F: FnPtr>(&mut self, target: F) -> Option<&mut InlineHook<F>>
Get a mutable reference to a specific hook by target function.
Sourcepub fn remove<F: FnPtr>(&mut self, target: F) -> Option<InlineHook<F>>
pub fn remove<F: FnPtr>(&mut self, target: F) -> Option<InlineHook<F>>
Remove a hook from the collection by target function.
Trait Implementations§
Source§impl Default for InlineHookMap
impl Default for InlineHookMap
Source§fn default() -> InlineHookMap
fn default() -> InlineHookMap
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for InlineHookMap
impl RefUnwindSafe for InlineHookMap
impl Send for InlineHookMap
impl Sync for InlineHookMap
impl Unpin for InlineHookMap
impl UnsafeUnpin for InlineHookMap
impl UnwindSafe for InlineHookMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more