hook_macro
Procedural macro for generating runtime hook systems with before/on/after callbacks.
Features
- 🔧 Automatically generates
before,on, andafterhook chains - 🌐 Supports FFI export for C/C++ interop
- 🛡️ Reentrancy protection to prevent infinite loops
- 🎨 Customizable naming with
prefix,suffix, andmanglingoptions - 💾 Configurable fallback return value when hooks interrupt execution
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick Start
use register_hook;
Examples
With Fallback
use register_hook;
Custom Naming
use register_hook;
// Rename the original implementation
// Or use custom prefix
// Or fully control the name
Generated API
For a function foo with one parameter, the macro generates:
| Function | Description |
|---|---|
register_before_foo(hook) |
Register a before hook |
register_on_foo(hook) |
Register an on hook |
register_after_foo(hook) |
Register an after hook |
clear_foo_hooks() |
Clear all hooks |
Hook Types
With parameter T:
type BeforeFooHook = unsafe extern "C" fn ;
type OnFooHook = unsafe extern "C" fn ;
type AfterFooHook = unsafe extern "C" fn ;
Without parameters:
type BeforeFooHook = unsafe extern "C" fn ;
type OnFooHook = unsafe extern "C" fn ;
type AfterFooHook = unsafe extern "C" fn ;
Return true from a hook to interrupt the chain and stop execution.
Attribute Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
fallback |
string | Default::default() |
Return value when hooks interrupt execution |
prefix |
string | "" |
Prefix for the original implementation name |
suffix |
string | "_impl" |
Suffix for the original implementation name |
mangling |
string | None | Full custom name for the original implementation |
Limitations
- Functions can have at most one parameter
- Parameter type must implement
Clone - Return type must implement
Default(unlessfallbackis specified)
License
This project is licensed under the LICENSE.