fn-ptr
This is a utility crate for introspecting and rewriting function pointer types at compile time.
It implements FnPtr for all function-pointer types:
fn(T) -> Uunsafe fn(T) -> Uextern "C-unwind" fn(T)unsafe extern "sysv64" fn() -> i32
Function pointer metadata
FnPtr exposes metadata as associated types/consts.
use ;
type F = extern "C" fn ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Ergonomic const functions are provided as well:
const A: usize = ;
const SAFE: bool = ;
const EXT: bool = ;
const abi: AbiValue = ;
Rewriting function-pointer types
The crate provides type-level rewriting via traits:
- abi:
WithAbi/with_abi! - Safety:
WithSafety/with_safety!(make_safe!,make_unsafe!) - Output:
WithOutput/with_output! - Args:
WithArgs/with_args!
Type-level transformations
The macros compute a new function-pointer type while preserving everything else.
use ;
type F = extern "C" fn ;
type F1 = with_abi!; // extern "sysv64" fn(i32) -> i32
type F2 = with_safety!; // unsafe extern "C" fn(i32) -> i32
type F3 = with_output!; // extern "C" fn(i32) -> u64
type F4 = with_args!; // extern "C" fn(u8, u16) -> i32
Value-level casts
The same transformations exist at the value level via methods on FnPtr.
These casts are only transmuting and do not the actual underlying function.
use ;
let f: fn = ;
// safety
let u: unsafe fn = f.as_unsafe;
let f2: fn = unsafe ;
// abi
let c: extern "C" fn = unsafe ;
// output
let out: fn = unsafe ;
// args
let args: fn = unsafe ;
# assert_eq!;
# assert_eq!;
# assert_eq!;
# assert_eq!;
How it works
Implementations are generated by a large macro. The rewrite macros are thin wrappers
over the traits WithAbi, WithSafety, WithOutput, WithArgs (and the corresponding *Impl helper traits).
License
Licensed under the MIT license, see LICENSE for details.