Struct RawDetour

Source
pub struct RawDetour(/* private fields */);
Expand description

A raw detour.

§Example

use retour::RawDetour;
use std::mem;

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

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

let mut hook = unsafe { RawDetour::new(add5 as *const (), add10 as *const ())? };

assert_eq!(add5(5), 10);
assert_eq!(hook.is_enabled(), false);

unsafe { hook.enable()? };
assert!(hook.is_enabled());

let original: fn(i32) -> i32 = unsafe { mem::transmute(hook.trampoline()) };

assert_eq!(add5(5), 15);
assert_eq!(original(5), 10);

unsafe { hook.disable()? };
assert_eq!(add5(5), 10);

Implementations§

Source§

impl RawDetour

Source

pub unsafe fn new(target: *const (), detour: *const ()) -> Result<Self>

Constructs a new inline detour patcher.

The hook is disabled by default. Even when this function is succesful, there is no guaranteee that the detour function will actually get called when the target function gets called. An invocation of the target function might for example get inlined in which case it is impossible to hook at runtime.

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.

Source

pub fn trampoline(&self) -> &()

Returns a reference to the generated trampoline.

Trait Implementations§

Source§

impl Debug for RawDetour

Source§

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

Formats the value using the given formatter. 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> 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.