dbg_pls/impls/std/
fnptr.rs

1use crate::{DebugPls, Formatter};
2
3macro_rules! fnptr_impls_safety_abi {
4    ($FnTy: ty, $($Arg: ident),*) => {
5        impl<Ret, $($Arg),*> DebugPls for $FnTy {
6            fn fmt(&self, f: Formatter<'_>) {
7                // HACK: The intermediate cast as usize is required for AVR
8                // so that the address space of the source function pointer
9                // is preserved in the final function pointer.
10                //
11                // https://github.com/avr-rust/rust/issues/143
12                DebugPls::fmt(&(*self as usize as *const ()), f)
13            }
14        }
15    }
16}
17
18macro_rules! peel {
19    ($name:ident, $($other:ident,)*) => (fnptr_impls_args! { $($other),* })
20}
21
22macro_rules! fnptr_impls_args {
23    ($($Arg: ident),+) => {
24        fnptr_impls_safety_abi! { extern "Rust" fn($($Arg),+) -> Ret, $($Arg),+ }
25        fnptr_impls_safety_abi! { extern "C" fn($($Arg),+) -> Ret, $($Arg),+ }
26        fnptr_impls_safety_abi! { extern "C" fn($($Arg),+ , ...) -> Ret, $($Arg),+ }
27        fnptr_impls_safety_abi! { unsafe extern "Rust" fn($($Arg),+) -> Ret, $($Arg),+ }
28        fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),+) -> Ret, $($Arg),+ }
29        fnptr_impls_safety_abi! { unsafe extern "C" fn($($Arg),+ , ...) -> Ret, $($Arg),+ }
30
31        peel! { $($Arg,)+ }
32    };
33    () => {
34        // No variadic functions with 0 parameters
35        fnptr_impls_safety_abi! { extern "Rust" fn() -> Ret, }
36        fnptr_impls_safety_abi! { extern "C" fn() -> Ret, }
37        fnptr_impls_safety_abi! { unsafe extern "Rust" fn() -> Ret, }
38        fnptr_impls_safety_abi! { unsafe extern "C" fn() -> Ret, }
39    };
40}
41
42fnptr_impls_args! { A, B, C, D, E, F, G, H, I, J, K, L }