Skip to main content

InlineHookMap

Struct InlineHookMap 

Source
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

Source

pub fn new() -> Self

Creates a new empty InlineHookMap.

Source

pub fn hooks<'a>(&'a self) -> Values<'a, fn(), InlineHook<fn()>>

Returns an iterator of the hooks.

Source

pub fn hooks_mut<'a>(&'a mut self) -> ValuesMut<'a, fn(), InlineHook<fn()>>

Returns a mutable iterator of the hooks.

Source

pub fn len(&self) -> usize

Returns the number of hooks in the collection.

Source

pub fn is_empty(&self) -> bool

Returns true if the collection is empty.

Source

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.

Source

pub fn get<F: FnPtr>(&self, target: F) -> Option<&InlineHook<F>>

Get a reference to a specific hook by target function.

Source

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.

Source

pub fn remove<F: FnPtr>(&mut self, target: F) -> Option<InlineHook<F>>

Remove a hook from the collection by target function.

Source

pub fn leak(&mut self)

Leak all hooks, preventing automatic disable() on drop.

Source

pub fn enable<'f1, I1>(&'f1 mut self) -> InlineHookMapEnableBuilder<'f1, I1>
where I1: FnMut(fn(), HRESULT),

Enable all hooks in the collection.

Errors are reported via the on_error callback (if provided). Hooks that fail will remain disabled.

TODO: Transaction

Source

pub fn disable<'f1, I1>(&'f1 mut self) -> InlineHookMapDisableBuilder<'f1, I1>
where I1: FnMut(fn(), HRESULT),

Disable all hooks in the collection.

Errors are reported via the on_error callback (if provided). Hooks that fail will remain enabled.

TODO: Transaction

Trait Implementations§

Source§

impl Default for InlineHookMap

Source§

fn default() -> InlineHookMap

Returns the “default value” for a type. Read more
Source§

impl Drop for InlineHookMap

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> DropFlavorWrapper<T> for T

Source§

type Flavor = MayDrop

The DropFlavor that wraps T into Self
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more