Struct GenericDetour

Source
pub struct GenericDetour<T: Function> { /* private fields */ }
Expand description

A type-safe detour.

Due to being generated by a macro, the GenericDetour::call method is not exposed in the documentation.
It accepts the same arguments as T, and shares its result type:

/// Calls the original function regardless of whether it's hooked or not.
fn call(&self, T::Arguments) -> T::Output

§Example

use retour::GenericDetour;

fn add5(val: i32) -> i32 {
  val + 5
}

fn add10(val: i32) -> i32 {
  val + 10
}

let mut hook = unsafe { GenericDetour::<fn(i32) -> i32>::new(add5, add10)? };

assert_eq!(add5(5), 10);
assert_eq!(hook.call(5), 10);

unsafe { hook.enable()? };

assert_eq!(add5(5), 15);
assert_eq!(hook.call(5), 10);

unsafe { hook.disable()? };

assert_eq!(add5(5), 10);

Implementations§

Source§

impl<T: Function> GenericDetour<T>

Source

pub unsafe fn new<D>(target: T, detour: D) -> Result<Self>
where T: HookableWith<D>, D: Function,

Create a new hook given a target function and a compatible detour function.

Source

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

Enables the detour.

Source

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

Disables the detour.

Source

pub fn is_enabled(&self) -> bool

Returns whether the detour is enabled or not.

Trait Implementations§

Source§

impl<T: Debug + Function> Debug for GenericDetour<T>

Source§

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

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

impl<T: Function> Send for GenericDetour<T>

Source§

impl<T: Function> Sync for GenericDetour<T>

Auto Trait Implementations§

§

impl<T> !Freeze for GenericDetour<T>

§

impl<T> !RefUnwindSafe for GenericDetour<T>

§

impl<T> Unpin for GenericDetour<T>
where T: Unpin,

§

impl<T> !UnwindSafe for GenericDetour<T>

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

Source§

type Output = T

Should always be Self
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.