Skip to content

Commit ed38fc0

Browse files
committed
Replace TockValue's auto-derived Debug implementation with a hand-written implementation optimized for size.
For Google's h1b_tests application, this saves 9440 bytes in .text. The caveat is that invoking Debug on a TockValue will not format the value contained in the TockValue::Expected().
1 parent 6db291d commit ed38fc0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/result.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
#[derive(Copy, Clone, Debug)]
1+
#[derive(Copy, Clone)]
22
pub enum TockValue<E> {
33
Expected(E),
44
Unexpected(isize),
55
}
66

7+
// Size-optimized implementation of Debug.
8+
impl<E: core::fmt::Debug> core::fmt::Debug for TockValue<E> {
9+
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
10+
match self {
11+
// Printing out the value of E would cause TockResult::unwrap() to
12+
// use a &dyn core::fmt::Debug, which defeats LLVM's
13+
// devirtualization and prevents LTO from removing unused Debug
14+
// implementations. Unfortunately, that generates too much code
15+
// bloat (several kB), so we cannot display the value contained in
16+
// this TockValue.
17+
TockValue::Expected(_) => f.write_str("Expected(...)"),
18+
19+
TockValue::Unexpected(n) => {
20+
f.write_str("Unexpected(")?;
21+
n.fmt(f)?;
22+
f.write_str(")")
23+
},
24+
}
25+
}
26+
}
27+
728
pub type TockResult<T, E> = Result<T, TockValue<E>>;
829

930
pub trait TockResultExt<T, E>: Sized {

0 commit comments

Comments
 (0)