Skip to content

Commit

Permalink
Add support for empty enums (#37)
Browse files Browse the repository at this point in the history
* Add support for empty enums

* fix test affected by static_assertions breaking changes
  • Loading branch information
yaahc authored Oct 6, 2021
1 parent 0b90859 commit 2d321d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
.map(|variant| helper.display_with_input(&input.attrs, &variant.attrs))
.collect::<Result<Vec<_>>>()?;

if displays.iter().any(Option::is_some) {
if data.variants.is_empty() {
Ok(quote! {
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
unreachable!("empty enums cannot be instantiated and thus cannot be printed")
}
}
})
} else if displays.iter().any(Option::is_some) {
let arms = data
.variants
.iter()
Expand Down
6 changes: 6 additions & 0 deletions tests/variantless.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use displaydoc::Display;

#[derive(Display)]
enum EmptyInside {}

static_assertions::assert_impl_all!(EmptyInside: core::fmt::Display);

0 comments on commit 2d321d7

Please sign in to comment.