Skip to content

Commit e93c6d3

Browse files
committed
enable non-exhaustive on stable
1 parent b91c04a commit e93c6d3

File tree

3 files changed

+65
-52
lines changed

3 files changed

+65
-52
lines changed

partialdebug-derive/src/lib.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use syn::*;
88

99
/// The non-exhaustive version of `PartialDebug`
1010
///
11-
/// Requires the `unstable` feature.
1211
/// Only available for structs with named fields.
13-
#[cfg(feature = "unstable")]
1412
#[proc_macro_derive(NonExhaustivePartialDebug)]
1513
pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
1614
let input = parse_macro_input!(input as ItemStruct);
@@ -27,19 +25,7 @@ pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
2725
}
2826
};
2927

30-
let as_debug_all_fields = fields.iter().map(|field| {
31-
let name = &field.ident;
32-
quote! {
33-
match ::partialdebug::specialization::AsDebug::as_debug(&self. #name) {
34-
::core::option::Option::None => {
35-
__exhaustive = false;
36-
}
37-
::core::option::Option::Some(field) => {
38-
__s.field(stringify!(#name), field);
39-
}
40-
}
41-
}
42-
});
28+
let as_debug_all_fields = fields.iter().map(gen_field_as_debug_non_exhaustive);
4329

4430
let expanded = quote! {
4531
impl #impl_generics ::core::fmt::Debug for #name #ty_generics #where_clause{
@@ -61,6 +47,39 @@ pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
6147
TokenStream::from(expanded)
6248
}
6349

50+
#[cfg(feature = "unstable")]
51+
fn gen_field_as_debug_non_exhaustive(field: &Field) -> TokenStream2 {
52+
let name = &field.ident;
53+
54+
quote! {
55+
match ::partialdebug::specialization::AsDebug::as_debug(&self. #name) {
56+
::core::option::Option::None => {
57+
__exhaustive = false;
58+
}
59+
::core::option::Option::Some(field) => {
60+
__s.field(stringify!(#name), field);
61+
}
62+
}
63+
}
64+
}
65+
66+
#[cfg(not(feature = "unstable"))]
67+
fn gen_field_as_debug_non_exhaustive(field: &Field) -> TokenStream2 {
68+
let name = &field.ident;
69+
let field_type = &field.ty;
70+
71+
quote! {
72+
match ::partialdebug::no_specialization::DebugDetector::<#field_type>::as_debug(&self. #name) {
73+
::core::option::Option::None => {
74+
__exhaustive = false;
75+
}
76+
::core::option::Option::Some(field) => {
77+
__s.field(stringify!(#name), field);
78+
}
79+
}
80+
}
81+
}
82+
6483
/// The placeholder version of `PartialDebug`
6584
#[proc_macro_derive(PlaceholderPartialDebug, attributes(debug_placeholder))]
6685
pub fn derive_placeholder(input: TokenStream) -> TokenStream {
@@ -159,7 +178,7 @@ fn struct_field_conversions<'a>(
159178
}
160179
Some(name) => (quote! {self.#name}, Some(quote! {stringify!(#name),})),
161180
};
162-
gen_field_as_debug(field, placeholder, field_handle, name_arg)
181+
gen_field_as_debug_placeholder(field, placeholder, field_handle, name_arg)
163182
})
164183
}
165184

@@ -175,12 +194,12 @@ fn enum_field_conversions<'a>(
175194
}
176195
Some(name) => (quote! {#name}, Some(quote! {stringify!(#name),})),
177196
};
178-
gen_field_as_debug(field, placeholder, field_handle, name_arg)
197+
gen_field_as_debug_placeholder(field, placeholder, field_handle, name_arg)
179198
})
180199
}
181200

182201
#[cfg(feature = "unstable")]
183-
fn gen_field_as_debug(
202+
fn gen_field_as_debug_placeholder(
184203
field: &Field,
185204
placeholder: &Option<String>,
186205
field_handle: TokenStream2,
@@ -203,7 +222,7 @@ fn gen_field_as_debug(
203222
}
204223

205224
#[cfg(not(feature = "unstable"))]
206-
fn gen_field_as_debug(
225+
fn gen_field_as_debug_placeholder(
207226
field: &Field,
208227
placeholder: &Option<String>,
209228
field_handle: TokenStream2,

src/lib.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Derive Debug for types where not all fields implement Debug.
22
//!
33
//! This crate works on stable and with `no_std`.
4-
//! On nightly the `unstable` feature can be used for specialization-based trait detection and/or `..` formatting.
4+
//! On nightly the `unstable` feature can be used for specialization-based trait detection.
55
//!
66
//! ### Placeholder with Type Info
77
//!
@@ -61,36 +61,30 @@
6161
//! ### Non-Exhaustive
6262
//!
6363
//! Only available for structs with named fields.
64-
65-
#![cfg_attr(
66-
feature = "unstable",
67-
doc = r##"
68-
```
69-
#![feature(debug_non_exhaustive)]
70-
use partialdebug::non_exhaustive::PartialDebug;
71-
# struct DNA;
72-
#
73-
#[derive(PartialDebug)]
74-
struct Dog {
75-
legs: usize,
76-
eyes: usize,
77-
dna: DNA,
78-
}
79-
# impl Dog {
80-
# fn new() -> Dog {
81-
# Dog {
82-
# legs: 4,
83-
# eyes: 2,
84-
# dna: DNA,
85-
# }
86-
# }
87-
# }
88-
#
89-
assert_eq!(format!("{:?}", Dog::new()), "Dog { legs: 4, eyes: 2, .. }");
90-
```
91-
"##
92-
)]
93-
64+
//!
65+
//! ```
66+
//! use partialdebug::non_exhaustive::PartialDebug;
67+
//! # struct DNA;
68+
//! #
69+
//! #[derive(PartialDebug)]
70+
//! struct Dog {
71+
//! legs: usize,
72+
//! eyes: usize,
73+
//! dna: DNA,
74+
//! }
75+
//! # impl Dog {
76+
//! # fn new() -> Dog {
77+
//! # Dog {
78+
//! # legs: 4,
79+
//! # eyes: 2,
80+
//! # dna: DNA,
81+
//! # }
82+
//! # }
83+
//! # }
84+
//! #
85+
//! assert_eq!(format!("{:?}", Dog::new()), "Dog { legs: 4, eyes: 2, .. }");
86+
//! ```
87+
//!
9488
//! ### Caveats
9589
//!
9690
//! Trait detection for generic types requires specialization.
@@ -142,7 +136,8 @@ pub mod placeholder {
142136
}
143137

144138
/// The non exhaustive version of `PartialDebug`
145-
#[cfg(feature = "unstable")]
146139
pub mod non_exhaustive {
140+
#[cfg(not(feature = "unstable"))]
141+
pub use crate::no_specialization::NotDebug as PartialDebug; // needs to be in scope
147142
pub use partialdebug_derive::NonExhaustivePartialDebug as PartialDebug;
148143
}

tests/hygiene.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![no_implicit_prelude]
22
#![no_std]
3-
#![cfg_attr(feature = "unstable", feature(debug_non_exhaustive))]
43
#![allow(dead_code)]
54

65
use ::partialdebug;

0 commit comments

Comments
 (0)