darfink / detour-rs

A cross-platform detour library written in Rust
Other
389 stars 71 forks source link

Detouring a function with a reference parameter #9

Open whatisaphone opened 5 years ago

whatisaphone commented 5 years ago

Something goes wrong deep in the innards of the macro when you try to detour an extern "Rust" function that takes a reference as a parameter:

static_detours! {
    struct DetourPrintln: fn(&str);
}
error[E0308]: mismatched types
  --> crates\core\src\lib.rs:21:5
   |
21 | /     static_detours! {
22 | |         pub struct DetourPrintln: fn(&str);
23 | |     }
   | |_____^ one type is more general than the other
   |
   = note: expected type `detour::Function`
              found type `detour::Function`
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
darfink commented 5 years ago

That's correct. The Function trait cannot express lifetime semantics of individual arguments and has therefore a 'static requirement (so functions with reference arguments do not implement it).

An alternative is to use the more cumbersome RawDetour.

Perhaps there is a potentially better alternative implementation but I've yet to identify a solution that is type-safe.

whatisaphone commented 5 years ago

I understand. That's an unfortunate limitation. Thanks for the explanation and workaround 👍