Question - any particular reason why Debug
trait isn't derived for the Value
enum?
#2931
-
I was wondering if there's a specific reason - why For example, I was debugging a driver and needed to print-out some bitfields but had to put together my own impl Debug for SCR::BUS_WIDTH::Value {
fn fmt(&self, _f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BUS_WIDTH_1 => {
print!("WIDTH_1");
Ok(())
}
Self::BUS_WIDTH_4 => {
print!("WIDTH_4");
Ok(())
}
Self::BUS_WIDTH_1_4 => {
print!("WIDTH_1_4");
Ok(())
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The main reason to avoid using |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
The main reason to avoid using
#[derive(Debug)]
is that Rust's formatting machinery is pretty heavy and relying on it to print much leads to binaries with a lot of bloat as a result (see https://github.com/tock/tock/blob/master/doc/CodeSize.md#formatting-overhead and https://jamesmunns.com/blog/fmt-unreasonably-expensive/ for discussions of this). Of course, by simply not printing the value in question you avoid the code size overhead, so we could deriveDebug
without hurting code size in the common case -- it just lowers the barrier to more widespread use ofDebug
which can ultimately lead to code bloat.