Skip to content

Replace TockValue's auto-derived Debug implementation with a hand-written implementation optimized for size. #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ linked_list_allocator = "0.6.3"

[dev-dependencies]
corepack = { version = "0.4.0", default-features = false, features = ["alloc"] }
serde = { version = "1.0.84", default-features = false, features = ["derive"] }
# We pin the serde version because newer serde versions may not be compatible
# with the nightly toolchain used by libtock-rs.
serde = { version = "=1.0.84", default-features = false, features = ["derive"] }

[profile.dev]
panic = "abort"
Expand Down
23 changes: 22 additions & 1 deletion src/result.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone)]
pub enum TockValue<E> {
Expected(E),
Unexpected(isize),
}

// Size-optimized implementation of Debug.
impl<E: core::fmt::Debug> core::fmt::Debug for TockValue<E> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
match self {
// Printing out the value of E would cause TockResult::unwrap() to
// use a &dyn core::fmt::Debug, which defeats LLVM's
// devirtualization and prevents LTO from removing unused Debug
// implementations. Unfortunately, that generates too much code
// bloat (several kB), so we cannot display the value contained in
// this TockValue.
TockValue::Expected(_) => f.write_str("Expected(...)"),

TockValue::Unexpected(n) => {
f.write_str("Unexpected(")?;
n.fmt(f)?;
f.write_str(")")
}
}
}
}

pub type TockResult<T, E> = Result<T, TockValue<E>>;

pub trait TockResultExt<T, E>: Sized {
Expand Down