Skip to main content

Hook

Struct Hook 

Source
pub struct Hook<F: 'static> { /* private fields */ }
Expand description

A hook on a function, returned disabled by a generated function’s hook.

F is the function’s generated Signature type. While the guard is alive, this mod can’t hook the function again.

§Dropping

Dropping the guard disables the hook as disable does, and the function can then be hooked again. If disabling fails, the hook stays in place and the function stays reserved. A thread still running the closure is unaffected, as nothing it uses is freed.

§Threads and batches

enable and disable suspend every other thread of the process while patching, so don’t call them from inside the hooked function. Inside an imping_hook::batch they are queued and applied together when the batch ends. The _unsuspended variants skip the suspension and take effect at once, even inside a batch.

Implementations§

Source§

impl<F: Copy + 'static> Hook<F>

Source

pub fn enable(&self) -> Result<()>

Redirects the function to the hook’s closure.

Suspends other threads while patching; see Hook for threads and batches.

§Errors

Error::Hook if patching fails.

Source

pub fn disable(&self) -> Result<()>

Restores the function, so calls skip the closure.

Suspends other threads while patching; see Hook for threads and batches.

§Errors

Error::Hook if patching fails.

Source

pub unsafe fn enable_unsuspended(&self) -> Result<()>

Redirects the function to the hook’s closure without suspending other threads.

Faster than enable; see Hook for threads and batches.

§Errors

Error::Hook if patching fails.

§Safety

No other thread may execute the function’s first instructions while they are patched, or it may run a half-written instruction.

Source

pub unsafe fn disable_unsuspended(&self) -> Result<()>

Restores the function without suspending other threads.

Faster than disable; see Hook for threads and batches.

§Errors

Error::Hook if patching fails.

§Safety

No other thread may execute the function’s first instructions while the original ones are written back.

Source

pub fn is_enabled(&self) -> bool

Returns whether the function is redirected to the closure.

Source

pub fn original(&self) -> F

Returns the original, unhooked function.

It stays valid for the rest of the process, even after the guard is dropped.

Source

pub fn leak(self)

Keeps the hook, in its current state, for the rest of the process.

The function stays reserved, and the mod must never be unloaded.

Trait Implementations§

Source§

impl<F: 'static> Debug for Hook<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: 'static> Drop for Hook<F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<F> !RefUnwindSafe for Hook<F>

§

impl<F> !UnwindSafe for Hook<F>

§

impl<F> Freeze for Hook<F>

§

impl<F> Send for Hook<F>
where F: Sync,

§

impl<F> Sync for Hook<F>
where F: Sync,

§

impl<F> Unpin for Hook<F>

§

impl<F> UnsafeUnpin for Hook<F>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.