Crate fn_ptr

Crate fn_ptr 

Source
Expand description

fn-ptr is a small utility crate that provides a FnPtr trait, implemented for all function pointer types:

  • fn(T) -> U
  • unsafe fn(T) -> U
  • extern "C" fn(T)
  • unsafe extern "sysv64" fn() -> i32

The trait provides associated types and constants to introspect function pointer types at compile time.

§Features

§1. Function Pointer Metadata

Every function pointer automatically implements FnPtr as well as a bunch of other related traits. With these you can inspect the type of function pointers at compile time:

use fn_ptr::{FnPtr, Abi};

type F = extern "C" fn(i32, i32) -> i32;

assert_eq!(<F as FnPtr>::ARITY, 2);
assert_eq!(<F as FnPtr>::IS_SAFE, true);
assert_eq!(<F as FnPtr>::IS_EXTERN, true);
assert_eq!(<F as FnPtr>::ABI, Abi::C { unwind: false });

There are also some const helper functons to do so ergonomically.

const A: usize = fn_ptr::arity::<F>();         // 2
const SAFE: bool = fn_ptr::is_safe::<F>();     // true
const EXT: bool = fn_ptr::is_extern::<F>();    // true
const ABI: Abi = fn_ptr::abi::<F>();           // Abi::C

§2. Toggle Function Pointer Safety

You can toggle the safety of a function pointer at the type level:

use fn_ptr::{make_safe, make_unsafe};

type U = unsafe extern "C" fn(i32);
type S = make_safe!(U);       // extern "C" fn(i32)

type S2 = extern "C" fn(i32);
type U2 = make_unsafe!(S2);   // unsafe extern "C" fn(i32)

Or at the instance level:

let safe_add: fn(i32, i32) -> i32 = |a, b| {a + b};
let unsafe_add: unsafe fn(i32, i32) -> i32 = safe_add.as_unsafe();
let safe_add2: fn(i32, i32) -> i32 = unsafe { unsafe_add.as_safe() };

§3. Changing ABIs

You can also change the ABI of a function pointer at the type level:

use fn_ptr::{with_abi, Abi};

type F = extern "C" fn(i32) -> i32;

type G = with_abi!("sysv64", F);
type H = with_abi!("C", extern "system" fn());

Or at the instance level:

use fn_ptr::{FnPtr, abi};
let rust_add: fn(i32, i32) -> i32 = |a, b| {a + b};
// Safety: not actually safe!
let c_add: extern "C" fn(i32, i32) -> i32 = unsafe { rust_add.with_abi::<abi!("C")>() };

Note that this does not change the underlying ABI and should be used with caution.

§How It Works

To implement the traits for all function pointer types, there is a large macro. For the conversion macros the crate relies on two traits: WithAbi and WithSafety that can also be used directly:

use fn_ptr::{FnPtr, WithAbi, WithSafety, marker::{SysV64, Unsafe}};

type F = extern "C" fn(i32);
type G = <F as WithAbi<SysV64>>::F;
type U = <F as WithSafety<Unsafe>>::F;

§License

Licensed under the MIT license, see LICENSE for details.

Re-exports§

pub use abi::Abi;

Modules§

abi
Module containing the Abi abstraction.
marker
Module containing all marker types and traits.
prelude
Prelude for this crate.

Macros§

abi
Macro to convert an ABI string to the corrsponding Abi marker type.
arity
Macro to convert an integral number to the corresponding Arity marker type.
make_safe
Convert a function-pointer type to the safe variant of the same signature. Arguments, return type, and ABI are preserved.
make_unsafe
Convert a function-pointer type to the unsafe variant of the same signature. Arguments, return type, and ABI are preserved.
safety
Macro to convert a safety token (safe or unsafe) or a boolean literal to the corrsponding Safety marker type.
with_abi
Construct a function-pointer type identical to the given one but using the specified ABI.
with_safety
Construct a function-pointer type identical to the given one but using the specified safety.

Structs§

OpaqueFn
A struct representing an opaque function.

Traits§

AsSafe
Helper trait to compute the safe version of a function pointer type while preserving arity, ABI, and signature.
AsUnsafe
Helper trait to compute the unsafe version of a function pointer type while preserving arity, ABI, and signature.
Convertible
Helper trait that simplifies generic bounds when converting between funciton pointer types.
FnPtr
Marker trait for all function pointers.
HasAbi
Marker trait implemented for function pointers of a specific ABI.
HasSafety
Marker trait denoting the safety of a function pointer type.
SafeFnPtr
Marker trait for all callable safe function pointer types (fn / extern fn).
StaticFnPtr
Marker trait for all static function pointer types. The return type and all parameter types have to be 'static.
UnsafeFnPtr
Marker trait for all callable unsafe function pointer types (unsafe fn / unsafe extern fn).
WithAbi
Helper trait to change the ABI of a function pointer type while preserving arity, safety, and signature.
WithSafety
Helper trait to change the safety of a function pointer type while preserving arity, ABI, and signature.

Functions§

abi
Returns the ABI of the function pointer.
arity
Returns the number of arguments of a function pointer type.
is_extern
Returns true if the function pointer uses an extern ABI.
is_safe
Returns true for safe function pointers (fn).
is_unsafe
Returns true for unsafe function pointers (unsafe fn).

Type Aliases§

UntypedFnPtr
Type alias for a raw untyped function pointer.