Skip to content

Commit 2dc0e46

Browse files
committed
Better error messages and docs mentioning limitations
1 parent b77c6c6 commit 2dc0e46

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

partialdebug-derive/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use proc_macro::TokenStream;
22

3-
use proc_macro2::TokenStream as TokenStream2;
3+
use proc_macro2::{Span, TokenStream as TokenStream2};
44
use quote::{quote, ToTokens};
55
use syn::parse::{Parse, ParseStream};
66
use syn::spanned::Spanned;
77
use syn::*;
88

99
/// The non exhaustive version of `PartialDebug`
10+
///
11+
/// Requires the `debug_non_exhaustive` feature.
12+
/// Only available for structs with named fields.
1013
#[proc_macro_derive(NonExhaustivePartialDebug)]
1114
pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
1215
let input = parse_macro_input!(input as ItemStruct);
@@ -15,8 +18,12 @@ pub fn derive_non_exhaustive(input: TokenStream) -> TokenStream {
1518

1619
let fields = match input.fields {
1720
Fields::Named(FieldsNamed { named, .. }) => named,
18-
Fields::Unnamed(_) => unimplemented!(),
19-
Fields::Unit => unimplemented!(),
21+
Fields::Unit => punctuated::Punctuated::new(),
22+
Fields::Unnamed(_) => {
23+
return Error::new(Span::call_site(), "non_exhaustive currently is only available on structs with named fields. See https://github.com/rust-lang/rust/issues/67364")
24+
.to_compile_error()
25+
.into();
26+
}
2027
};
2128

2229
let as_debug_all_fields = fields.iter().map(|field| {
@@ -74,7 +81,14 @@ pub fn derive_placeholder(input: TokenStream) -> TokenStream {
7481
struct_field_conversions(&fields, &placeholder),
7582
),
7683
Data::Enum(data_enum) => gen_enum_debug(&data_enum, &name, &placeholder),
77-
Data::Union(_) => unimplemented!(),
84+
Data::Union(_) => {
85+
return Error::new(
86+
Span::call_site(),
87+
"PartialDebug can not be derived for unions",
88+
)
89+
.to_compile_error()
90+
.into();
91+
}
7892
};
7993

8094
let expanded = quote! {

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
//! Derive Debug for types where not all fields implement Debug
1+
//! Derive Debug for types where not all fields implement Debug.
2+
//!
3+
//! This relies on specialization and thus requires nightly.
24
//!
35
//! ### Non Exhaustive
46
//!
7+
//! Requires the `debug_non_exhaustive` feature.
8+
//! Only available for structs with named fields.
9+
//!
510
//! ```
611
//! #![feature(debug_non_exhaustive)]
712
//! use partialdebug::non_exhaustive::PartialDebug;

0 commit comments

Comments
 (0)