You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`fn-ptr` is a small utility crate that provides a `FnPtr` trait, implemented for all function pointer types:
5
+
`fn-ptr` is a small utility crate that provides a [`FnPtr`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.FnPtr.html) trait, implemented for all function pointer types:
6
6
-`fn(T) -> U`
7
7
-`unsafe fn(T) -> U`
8
8
-`extern "C" fn(T)`
@@ -35,7 +35,7 @@ pub trait FnPtr {
35
35
36
36
1. Function-pointer metadata
37
37
38
-
Every function pointer automatically implements [`FnPtr`]. Depending on the type, it may also implement [`SafeFnPtr`], [`UnsafeFnPtr`], and [`HasAbi<Abi>`].
38
+
Every function pointer automatically implements [`FnPtr`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.FnPtr.html). Depending on the type, it may also implement [`SafeFnPtr`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.SafeFnPtr.html), [`UnsafeFnPtr`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.UnsafeFnPtr.html), and [`HasAbi<Abi>`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.HasAbi.html).
39
39
40
40
```rust
41
41
usefn_ptr::{FnPtr, Abi};
@@ -53,10 +53,11 @@ Const helper functions are also provided:
53
53
constA:usize=fn_ptr::arity::<F>(); // 2
54
54
constSAFE:bool=fn_ptr::is_safe::<F>(); // true
55
55
constEXT:bool=fn_ptr::is_extern::<F>(); // true
56
-
constABI:Abi=fn_ptr::abi::<F>(); // Abi::C
56
+
constABI:Abi=fn_ptr::abi::<F>(); // Abi::C
57
57
```
58
58
59
59
2. Changing ABIs at the type level
60
+
60
61
You can change the ABI of a function pointer type using macros:
61
62
```rust
62
63
usefn_ptr::{with_abi, Abi};
@@ -68,6 +69,7 @@ type H = with_abi!("C", extern "system" fn());
68
69
```
69
70
70
71
3. Toggle function pointer safety
72
+
71
73
Macros are provided to make function pointers safe or unsafe:
72
74
```rust
73
75
usefn_ptr::{make_safe, make_unsafe};
@@ -80,7 +82,7 @@ type U2 = make_unsafe!(S2); // unsafe extern "C" fn(i32)
80
82
```
81
83
82
84
## How it works under the hood
83
-
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:
85
+
All macros rely on type-level traits [`WithAbi`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.WithAbi.html) and [`WithSafety`](https://docs.rs/fn-ptr/latest/fn_ptr/trait.WithSafety.html). Each trait exposes an associated type representing the transformed function pointer. You can use these traits directly for const generics or explicit type transformations:
0 commit comments