Skip to content

Commit d568a31

Browse files
committed
Add non-exhaustive Debug output for structs
1 parent 939e375 commit d568a31

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111
- Updated `syn` to v2.
12+
- `Debug` output of structs that skip fields appears `non_exhaustive` now.
1213

1314
### Deprecated
1415
- The `crate` attribute now takes a path instead of a path inside a string

Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ pretty_assertions = "1"
4343
trybuild = { version = "1.0.18", default-features = false }
4444
zeroize_ = { version = "1.5", package = "zeroize", default-features = false }
4545

46-
[[test]]
47-
name = "util"
48-
path = "tests/util.rs"
49-
test = false
50-
5146
[package.metadata.docs.rs]
5247
all-features = true
5348
targets = []

src/trait_/debug.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ impl TraitImpl for Debug {
5050
.iter_field_ident(**trait_)
5151
.map(|field| field.to_string());
5252

53+
let finish = if data.any_skip_trait(**trait_) {
54+
quote! { finish_non_exhaustive }
55+
} else {
56+
quote! { finish }
57+
};
58+
5359
quote! {
5460
#self_pattern => {
5561
let mut __builder = ::core::fmt::Formatter::debug_struct(__f, #debug_name);
5662
#(::core::fmt::DebugStruct::field(&mut __builder, #debug_fields, #self_ident);)*
57-
::core::fmt::DebugStruct::finish(&mut __builder)
63+
::core::fmt::DebugStruct::#finish(&mut __builder)
5864
}
5965
}
6066
}

tests/skip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![allow(clippy::clone_on_copy)]
22

3-
// Necessary for rustfmt.
4-
#[path = "util.rs"]
53
#[macro_use]
64
mod util;
5+
#[path = "skip/debug.rs"]
6+
mod debug;
77
#[path = "skip/field.rs"]
88
mod field;
99
#[path = "skip/field_trait.rs"]

tests/skip/debug.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use derive_where::derive_where;
2+
3+
use crate::util::{AssertDebug, Wrapper};
4+
5+
#[test]
6+
fn all() {
7+
#[derive_where(Debug)]
8+
#[derive_where(skip_inner)]
9+
struct Test<T> {
10+
a: Wrapper<T>,
11+
}
12+
13+
let test_1 = Test { a: 42.into() };
14+
15+
let _ = AssertDebug(&test_1);
16+
17+
assert_eq!(format!("{:?}", test_1), "Test { .. }");
18+
}
19+
20+
#[test]
21+
fn partial() {
22+
#[derive_where(Debug)]
23+
struct Test<T> {
24+
#[derive_where(skip)]
25+
a: Wrapper<T>,
26+
b: Wrapper<T>,
27+
}
28+
29+
let test_1 = Test {
30+
a: 42.into(),
31+
b: 42.into(),
32+
};
33+
34+
let _ = AssertDebug(&test_1);
35+
36+
assert_eq!(format!("{:?}", test_1), "Test { b: 42, .. }");
37+
}
File renamed without changes.

0 commit comments

Comments
 (0)