1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use super::*;

impl<T> Format for core::ptr::NonNull<T> {
    fn format(&self, fmt: Formatter) {
        crate::write!(fmt, "{}", self.as_ptr())
    }
}

#[cfg(c_variadic)]
macro_rules! fnptr_format_cvariadic {
    ($($Arg:ident),+) => {
        impl<Ret, $($Arg),*> Format for extern "C" fn($($Arg),* , ...) -> Ret {
            fn format(&self, fmt: Formatter) {
                crate::write!(fmt, "{}", (*self as usize) as *const ())
            }
        }
        impl<Ret, $($Arg),*> Format for unsafe extern "C" fn($($Arg),* , ...) -> Ret {
            fn format(&self, fmt: Formatter) {
                crate::write!(fmt, "{}", (*self as usize) as *const ())
            }
        }
    };
    () => {
        // C variadics require at least one other argument
    };
}

macro_rules! fnptr_format_args {
    ($($Arg:ident),*) => {
        impl<Ret, $($Arg),*> Format for extern "Rust" fn($($Arg),*) -> Ret {
            fn format(&self, fmt: Formatter) {
                crate::write!(fmt, "{}", (*self as usize) as *const ())
            }
        }
        impl<Ret, $($Arg),*> Format for extern "C" fn($($Arg),*) -> Ret {
            fn format(&self, fmt: Formatter) {
                crate::write!(fmt, "{}", (*self as usize) as *const ())
            }
        }
        impl<Ret, $($Arg),*> Format for unsafe extern "Rust" fn($($Arg),*) -> Ret {
            fn format(&self, fmt: Formatter) {
                crate::write!(fmt, "{}", (*self as usize) as *const ())
            }
        }
        impl<Ret, $($Arg),*> Format for unsafe extern "C" fn($($Arg),*) -> Ret {
            fn format(&self, fmt: Formatter) {
                crate::write!(fmt, "{}", (*self as usize) as *const ())
            }
        }
        #[cfg(c_variadic)]
        fnptr_format_cvariadic!{ $($Arg),* }
    };
}

// core::ptr has fnptr impls up to 12 arguments
// https://doc.rust-lang.org/src/core/ptr/mod.rs.html#1994
fnptr_format_args! {}
fnptr_format_args! { A }
fnptr_format_args! { A, B }
fnptr_format_args! { A, B, C }
fnptr_format_args! { A, B, C, D }
fnptr_format_args! { A, B, C, D, E }
fnptr_format_args! { A, B, C, D, E, F }
fnptr_format_args! { A, B, C, D, E, F, G }
fnptr_format_args! { A, B, C, D, E, F, G, H }
fnptr_format_args! { A, B, C, D, E, F, G, H, I }
fnptr_format_args! { A, B, C, D, E, F, G, H, I, J }
fnptr_format_args! { A, B, C, D, E, F, G, H, I, J, K }
fnptr_format_args! { A, B, C, D, E, F, G, H, I, J, K, L }