fmt::Debug not implemented for "system", "stdcall", "...", function pointer


I used a macro to simplify C struct FFI declaration. Everything is ok but not when the struct contains a function pointer when I try to print it with debug trait.
Having the function pointer to be extern "stdcall" cause the following error:
But if I removed the calling convention, no more error:

I found an open issue for this: that redirect to this one

This is very annoying when you do a lot of C FFI and I can't find any rfc for this. Does anybody is up-to-date concerning the progress?

Thank you

I don't know anything about the progress, but one approach to avoid the issue is to define a type like the one below.

#[derive(Copy, Clone)]
pub struct ImplDebug<F>(pub F);
impl<F: Default> Default for ImplDebug<F> {
    fn default() -> Self { ImplDebug(Default::default()) }
impl<F> fmt::Debug for ImplDebug<F> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl<F> Deref for ImplDebug<F> {
    type Target = F;
    fn deref(&self) -> &F {
impl<F> DerefMut for ImplDebug<F> {
    fn deref_mut(&mut self) -> &mut F {
        &mut self.0


The code in your example would then have the following debug representation:

MyStruct { fct_ptr: extern "stdcall" fn(*mut core::ffi::c_void, u32, u64, u64) -> u64 }

This type uses #[repr(transparent)] to ensure the memory representation is exactly the same as the inner type, making it valid for C-ffi in exactly the same places as the inner type is. Additionally it implements Deref to make it easier to work with, as references to ImplDebug<F> automatically coerce to references to F.


A big thank you! This is a smart and convenient approach.
I hope issues will be fixed soon!

1 Like