Skip to main content

FnSymbol

Struct FnSymbol 

Source
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>

Source

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.

Source

pub const fn id(self) -> AnvilSymbol

The symbol’s id, sym::<NAME>.

Trait Implementations§

Source§

impl<F: FnPtr> Clone for FnSymbol<F>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: FnPtr> Copy for FnSymbol<F>

Source§

impl<F: FnPtr> Debug for FnSymbol<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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>

§

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>

§

impl<F> UnwindSafe for FnSymbol<F>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.