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:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=591d6647da50f72991cc7456852233d2
But if I removed the calling convention, no more error:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f7a41eb9144b8c2393ff6d52c6c48b34

I found an open issue for this: https://github.com/rust-lang/rust/issues/40710 that redirect to this one https://github.com/rust-lang/rust/issues/46989.

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