Skip to content

Commit 17a647a

Browse files
authored
Merge pull request #85 from jrvanwhy/smaller-debug
Replace TockValue's auto-derived Debug implementation with a hand-written implementation optimized for size.
2 parents 08cd0c7 + 8800af2 commit 17a647a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/result.rs

+22-1
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)