detour-rs
detour-rs copied to clipboard
Detouring a function with a reference parameter
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)
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.
I understand. That's an unfortunate limitation. Thanks for the explanation and workaround 👍