From e4883341f12556277dddffd7ca7aede3b71e5483 Mon Sep 17 00:00:00 2001 From: Johnathan Van Why Date: Thu, 8 Aug 2019 17:28:32 -0700 Subject: [PATCH 1/2] Pin serde to a specific version, to keep compatibility with libtock-rs' toolchain. The latest version of serde requires a newer toolchain. This change allows the examples to keep building, un-breaking the Travil build. --- Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a2b98e5e..e49e6991 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" From 8800af2ec0608ed71699e4199d110896082bfa93 Mon Sep 17 00:00:00 2001 From: Johnathan Van Why Date: Thu, 8 Aug 2019 16:43:21 -0700 Subject: [PATCH 2/2] 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(). I'm blocking serde updates so that the CI passes. --- src/result.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/result.rs b/src/result.rs index cfc809ca..e00f3f09 100644 --- a/src/result.rs +++ b/src/result.rs @@ -1,9 +1,30 @@ -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone)] pub enum TockValue { Expected(E), Unexpected(isize), } +// Size-optimized implementation of Debug. +impl core::fmt::Debug for TockValue { + 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 = Result>; pub trait TockResultExt: Sized {