Smarter than `&&self.field` in derived debugs?

This thread on URLO made me think: Why it is not allowed to cast &T to &dyn Trait, where T: Trait + ?Sized? - help - The Rust Programming Language Forum

Today, because derive(Debug) doesn't know which fields are unsized, it's stuck using &&self.foo to get a &dyn Debug. But for most fields that's a waste, and &self.foo would have been sufficient.

Is there a good way we could make a construct of some sort -- language or library API -- to do this?

Anyone clever enough to make autoref-based pseudo-specialization work based on the T: Sized bound somehow?

5 Likes

I have an unfinished branch (GitHub - GKFX/rust at derive-debug-static-table) which makes derive(Debug) use a static table of data about the type for better performance and smaller code than the current approach. When constructing that table, it's possible to emit e.g.

if crate::mem::size_of::<&[u8]>() != crate::mem::size_of::<&()>()

which (I think) selects the right option without autoref magic. Built-in macros are quite fiddly, so I haven't finished it yet. NB derive(Debug) on repr(packed) is unsound on the linked branch.