pub struct FnSymbol<F: FnPtr> { /* private fields */ }Expand description
A function symbol together with its declared type: what
anvil gen --lang rust-sdk writes as sym::typed::<NAME> for each
function symbol of symbols.toml, from the same signature as
sym::<Name>Fn. Host::hook_symbol takes it
with a hook and a detour of that type, so a detour of another shape does
not compile and the call needs no unsafe.
use std::ffi::c_void;
use anvil::{FnSymbol, FunctionHook, Host};
mod sym {
// What anvil gen --lang rust-sdk writes for `void* (void* session)`.
pub type SpeedObjectFn = unsafe extern "C" fn(session: *mut std::ffi::c_void) -> *mut std::ffi::c_void;
pub const SPEED_OBJECT: u32 = 0;
pub mod typed {
// SAFETY: the type of the signature above.
pub const SPEED_OBJECT: anvil::FnSymbol<super::SpeedObjectFn> =
unsafe { anvil::FnSymbol::new(super::SPEED_OBJECT) };
}
}
static HOOK: FunctionHook<sym::SpeedObjectFn> = FunctionHook::new();
unsafe extern "C" fn detour(session: *mut c_void) -> *mut c_void {
// SAFETY: the game's function, with the game's argument.
HOOK.next().map_or(std::ptr::null_mut(), |next| unsafe { next(session) })
}
fn load(host: &Host) -> anvil::Result<()> {
host.hook_symbol(&HOOK, sym::typed::SPEED_OBJECT, detour)
}A detour of another type is a compile error:
ⓘ
use std::ffi::c_void;
use anvil::{FnSymbol, FunctionHook, Host};
type SpeedObjectFn = unsafe extern "C" fn(session: *mut c_void) -> *mut c_void;
// SAFETY: the type of the symbol's signature.
const SPEED_OBJECT: FnSymbol<SpeedObjectFn> = unsafe { FnSymbol::new(0) };
static HOOK: FunctionHook<SpeedObjectFn> = FunctionHook::new();
unsafe extern "C" fn detour(_speed: i32) -> i32 {
0
}
fn load(host: &Host) -> anvil::Result<()> {
host.hook_symbol(&HOOK, SPEED_OBJECT, detour)
}Implementations§
Source§impl<F: FnPtr> FnSymbol<F>
impl<F: FnPtr> FnSymbol<F>
Sourcepub const unsafe fn new(id: AnvilSymbol) -> Self
pub const unsafe fn new(id: AnvilSymbol) -> Self
The symbol id as a function of type F.
§Safety
id is a function symbol of this plugin’s symbols.toml whose declared
signature is exactly F (as anvil gen translates it): the game calls
a detour of F with the arguments of that signature.
Sourcepub const fn id(self) -> AnvilSymbol
pub const fn id(self) -> AnvilSymbol
The symbol’s id, sym::<NAME>.
Trait Implementations§
Auto Trait Implementations§
impl<F> Forget for FnSymbol<F>
impl<F> Freeze for FnSymbol<F>where
PhantomData<F>: Freeze,
impl<F> Move for FnSymbol<F>
impl<F> RefUnwindSafe for FnSymbol<F>where
PhantomData<F>: RefUnwindSafe,
impl<F> Send for FnSymbol<F>where
PhantomData<F>: Send,
impl<F> Sync for FnSymbol<F>where
PhantomData<F>: Sync,
impl<F> Unpin for FnSymbol<F>where
PhantomData<F>: Unpin,
impl<F> UnsafeUnpin for FnSymbol<F>where
PhantomData<F>: UnsafeUnpin,
impl<F> UnwindSafe for FnSymbol<F>where
PhantomData<F>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more