fn-ptr
fn-ptr is a small utility crate that provides a FnPtr trait, implemented for all function pointer types:
fn(T) -> Uunsafe fn(T) -> Uextern "C" fn(T)unsafe extern "sysv64" fn() -> i32
The trait provides associated types and constants to introspect function pointer types at compile time.
Features
- Function-pointer metadata
Every function pointer automatically implements [FnPtr]. Depending on the type, it may also implement [SafeFnPtr], [UnsafeFnPtr], and [HasAbi].
use ;
type F = extern "C" fn ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Const helper functions are also provided:
const A: usize = ; // 2
const SAFE: bool = ; // true
const EXT: bool = ; // true
const ABI: Abi = ; // Abi::C
- Changing ABIs at the type level You can change the ABI of a function pointer type using macros:
use ;
type F = extern "C" fn ;
type G = with_abi!;
type H = with_abi!;
- Toggle function pointer safety Macros are provided to make function pointers safe or unsafe:
use ;
type U = unsafe extern "C" fn;
type S = make_safe!; // extern "C" fn(i32)
type S2 = extern "C" fn;
type U2 = make_unsafe!; // unsafe extern "C" fn(i32)
How it works under the hood
All macros rely on type-level traits [WithAbi] and [WithSafety]. Each trait exposes an associated type representing the transformed function pointer. You can use these traits directly for const generics or explicit type transformations:
use ;
type F = extern "C" fn;
type G = , F>>F;
type U = , F>>F;
License
Licensed under the MIT license, see LICENSE for details.