Skip to content

Commit

Permalink
Add a feature to disable extra debug info (bincode-org#595)
Browse files Browse the repository at this point in the history
This allows
- More optimization (smallest final binary size)
- No internal code structure leak (no enum name leak).
  • Loading branch information
capatoles committed Oct 20, 2022
1 parent a51d499 commit 2cf20b3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ default = ["std", "derive"]
std = ["alloc", "serde?/std"]
alloc = ["serde?/alloc"]
derive = ["bincode_derive"]
no-extra-debug = ["bincode_derive/no-extra-debug"]

[dependencies]
bincode_derive = { path = "derive", version = "2.0.0-rc.2", optional = true }
Expand Down
3 changes: 3 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ keywords = ["binary", "encode", "decode", "serialize", "deserialize"]
license = "MIT"
description = "Implementation of #[derive(Encode, Decode)] for bincode"

[features]
no-extra-debug = []

[lib]
proc-macro = true

Expand Down
10 changes: 9 additions & 1 deletion derive/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@ impl DeriveEnum {

variant_inner.ident_str("type_name");
variant_inner.punct(':');
variant_inner.lit_str(enum_name);
// we add the enum name in the generated error
if cfg!(feature = "no-extra-debug") {
// if the feature is "no-extra-debug" we set an empty string rather than leaking the enum name
// this is also useful for optimization
variant_inner.lit_str("");
} else {
// by default, we add the enum name to the generated error
variant_inner.lit_str(enum_name);
}
variant_inner.punct(',');

variant_inner.ident_str("allowed");
Expand Down

0 comments on commit 2cf20b3

Please sign in to comment.