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

Hello,

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: Standard traits not derived for function pointers with different calling conventions · Issue #40710 · rust-lang/rust · GitHub that redirect to this one Function pointer docs may need updating · Issue #46989 · rust-lang/rust · GitHub.

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.

#[repr(transparent)]
#[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 {
        f.write_str(std::any::type_name::<F>())
    }
}
impl<F> Deref for ImplDebug<F> {
    type Target = F;
    fn deref(&self) -> &F {
        &self.0
    }
}
impl<F> DerefMut for ImplDebug<F> {
    fn deref_mut(&mut self) -> &mut F {
        &mut self.0
    }
}

playground

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.

2 Likes

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

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.