[SOLVED] Put trait bound Debug on generic type only during development


#1

During development, I would like to restrict the usage of my module to those types of payloads which are Debug, so that I can peek into it using println.
As far as I can see, this requires me to add the Debug trait bound to all structs and impls.
Is there an easier way? Ideally, I would later like to remove the code that uses println and change one more line of code to lift the restriction to Debug types.
Thank you!


#2

One common idiom seems to define

pub trait Payload : Debug { }
impl<T:Debug> Payload for T { }

and then to season all structs and impls with T:Payload. The Debug can be later removed in only these two places.
Works fine for me, but I’d be happy about any comment.


#3

You could also make that payload conditional to easily toggle it, perhaps using #[cfg(debug_assertions)] or your own feature flag, and then make a no-op payload otherwise.


#4

You can use specialization to reach Debug impls, even if you don’t have the bound. I’m going to use this for debugging a lot.

Playground example here

That’s the brave new world of non-parametricity.