Skip to main content

StaticHook

Struct StaticHook 

Source
pub struct StaticHook<F> { /* private fields */ }

Implementations§

Source§

impl<F: Copy> StaticHook<F>

Source

pub const fn new() -> Self

Examples found in repository?
examples/static_hook_basic.rs (line 10)
10static HOOK: StaticHook<fn(i32) -> i32> = StaticHook::new();
Source

pub fn set_before<C: Fn() + Send + Sync + 'static>(&self, cb: C)

Examples found in repository?
examples/static_hook_basic.rs (line 34)
29fn main() -> dobby_rs_framework::Result<()> {
30    common::init_example_logging();
31
32    // EN: Set optional callbacks (not required).
33    // CN: 设置可选回调(非必须)。
34    HOOK.set_before(|| log::info!("before"));
35    HOOK.set_after(|| log::info!("after"));
36
37    unsafe {
38        HOOK.install(
39            common::target_add as fn(i32) -> i32,
40            detour_add as fn(i32) -> i32,
41        )?;
42    }
43
44    let v = common::target_add(1);
45    println!("target_add(1) = {v}");
46
47    unsafe {
48        HOOK.uninstall()?;
49    }
50    Ok(())
51}
Source

pub fn set_after<C: Fn() + Send + Sync + 'static>(&self, cb: C)

Examples found in repository?
examples/static_hook_basic.rs (line 35)
29fn main() -> dobby_rs_framework::Result<()> {
30    common::init_example_logging();
31
32    // EN: Set optional callbacks (not required).
33    // CN: 设置可选回调(非必须)。
34    HOOK.set_before(|| log::info!("before"));
35    HOOK.set_after(|| log::info!("after"));
36
37    unsafe {
38        HOOK.install(
39            common::target_add as fn(i32) -> i32,
40            detour_add as fn(i32) -> i32,
41        )?;
42    }
43
44    let v = common::target_add(1);
45    println!("target_add(1) = {v}");
46
47    unsafe {
48        HOOK.uninstall()?;
49    }
50    Ok(())
51}
Source

pub fn call_before(&self)

Examples found in repository?
examples/static_hook_basic.rs (line 16)
13fn detour_add(x: i32) -> i32 {
14    // EN: Optional: run user callbacks.
15    // CN: 可选:执行用户的 before/after 回调。
16    HOOK.call_before();
17
18    // EN: Call the original implementation with modified arguments.
19    // CN: 修改参数后调用原函数。
20    let out = (HOOK.original())(x + 100);
21
22    HOOK.call_after();
23
24    // EN: Post-process return value.
25    // CN: 对返回值做二次处理。
26    out + 10
27}
Source

pub fn call_after(&self)

Examples found in repository?
examples/static_hook_basic.rs (line 22)
13fn detour_add(x: i32) -> i32 {
14    // EN: Optional: run user callbacks.
15    // CN: 可选:执行用户的 before/after 回调。
16    HOOK.call_before();
17
18    // EN: Call the original implementation with modified arguments.
19    // CN: 修改参数后调用原函数。
20    let out = (HOOK.original())(x + 100);
21
22    HOOK.call_after();
23
24    // EN: Post-process return value.
25    // CN: 对返回值做二次处理。
26    out + 10
27}
Source

pub unsafe fn install(&self, target: F, detour: F) -> Result<()>

Examples found in repository?
examples/static_hook_basic.rs (lines 38-41)
29fn main() -> dobby_rs_framework::Result<()> {
30    common::init_example_logging();
31
32    // EN: Set optional callbacks (not required).
33    // CN: 设置可选回调(非必须)。
34    HOOK.set_before(|| log::info!("before"));
35    HOOK.set_after(|| log::info!("after"));
36
37    unsafe {
38        HOOK.install(
39            common::target_add as fn(i32) -> i32,
40            detour_add as fn(i32) -> i32,
41        )?;
42    }
43
44    let v = common::target_add(1);
45    println!("target_add(1) = {v}");
46
47    unsafe {
48        HOOK.uninstall()?;
49    }
50    Ok(())
51}
Source

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

Examples found in repository?
examples/static_hook_basic.rs (line 48)
29fn main() -> dobby_rs_framework::Result<()> {
30    common::init_example_logging();
31
32    // EN: Set optional callbacks (not required).
33    // CN: 设置可选回调(非必须)。
34    HOOK.set_before(|| log::info!("before"));
35    HOOK.set_after(|| log::info!("after"));
36
37    unsafe {
38        HOOK.install(
39            common::target_add as fn(i32) -> i32,
40            detour_add as fn(i32) -> i32,
41        )?;
42    }
43
44    let v = common::target_add(1);
45    println!("target_add(1) = {v}");
46
47    unsafe {
48        HOOK.uninstall()?;
49    }
50    Ok(())
51}
Source

pub fn original(&self) -> F

Examples found in repository?
examples/static_hook_basic.rs (line 20)
13fn detour_add(x: i32) -> i32 {
14    // EN: Optional: run user callbacks.
15    // CN: 可选:执行用户的 before/after 回调。
16    HOOK.call_before();
17
18    // EN: Call the original implementation with modified arguments.
19    // CN: 修改参数后调用原函数。
20    let out = (HOOK.original())(x + 100);
21
22    HOOK.call_after();
23
24    // EN: Post-process return value.
25    // CN: 对返回值做二次处理。
26    out + 10
27}

Trait Implementations§

Source§

impl<F: Copy> Default for StaticHook<F>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<F> !Freeze for StaticHook<F>

§

impl<F> RefUnwindSafe for StaticHook<F>
where F: RefUnwindSafe,

§

impl<F> Send for StaticHook<F>
where F: Send,

§

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

§

impl<F> Unpin for StaticHook<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for StaticHook<F>

§

impl<F> UnwindSafe for StaticHook<F>
where F: UnwindSafe,

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.