Skip to content

Commit 52b4f44

Browse files
daxpeddaModProg
authored andcommitted
Address review
1 parent 023309d commit 52b4f44

File tree

2 files changed

+27
-68
lines changed

2 files changed

+27
-68
lines changed

src/item.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use proc_macro2::TokenStream;
44
use quote::{quote, ToTokens};
55
use syn::{
6-
punctuated::Punctuated, spanned::Spanned, Attribute, Fields, Ident, Meta, Result, Token,
7-
Variant,
6+
punctuated::Punctuated, spanned::Spanned, Attribute, Ident, Meta, Result, Token, Variant,
87
};
98

109
use crate::{Data, Error, Incomparable, Trait};
@@ -138,11 +137,7 @@ impl Discriminant {
138137
}
139138
}
140139

141-
let is_unit = variants.iter().all(|variant| match &variant.fields {
142-
Fields::Named(fields) => fields.named.is_empty(),
143-
Fields::Unnamed(fields) => fields.unnamed.is_empty(),
144-
Fields::Unit => true,
145-
});
140+
let is_unit = variants.iter().all(|variant| variant.fields.is_empty());
146141

147142
Ok(if let Some(repr) = has_repr {
148143
if is_unit {

src/trait_/common_ord.rs

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -101,69 +101,22 @@ pub fn build_ord_signature(item: &Item, trait_: &DeriveTrait, body: &TokenStream
101101
_ => unreachable!("unsupported trait in `prepare_ord`"),
102102
};
103103

104-
// Nightly or unsafe (default) implementation.
105-
#[cfg(any(feature = "nightly", not(feature = "safe")))]
106-
{
107-
// Nightly implementation.
108-
#[cfg(feature = "nightly")]
109-
quote! {
110-
#incomparable
111-
112-
let __self_disc = ::core::intrinsics::discriminant_value(self);
113-
let __other_disc = ::core::intrinsics::discriminant_value(__other);
114-
115-
if __self_disc == __other_disc {
116-
#body_equal
117-
} else {
118-
#path::#method(&__self_disc, &__other_disc)
119-
}
120-
}
121-
// Unsafe (default) implementation.
122-
#[cfg(not(feature = "nightly"))]
123-
{
124-
let body_else = match discriminant {
125-
Discriminant::Single => unreachable!(
126-
"we should only generate this code with multiple variants"
127-
),
128-
Discriminant::UnitDefault => quote! {
129-
#path::#method(
130-
self as isize,
131-
__other as isize,
132-
)
133-
},
134-
Discriminant::Default => {
135-
build_recursive_order(trait_, variants, &incomparable)
136-
}
137-
Discriminant::UnitRepr(repr) => quote! {
138-
#path::#method(
139-
self as #repr,
140-
__other as #repr,
141-
)
142-
},
143-
Discriminant::Repr(repr) => quote! {
144-
#path::#method(
145-
unsafe { *<*const _>::from(self).cast::<#repr>() },
146-
unsafe { *<*const _>::from(__other).cast::<#repr>() },
147-
)
148-
},
149-
};
150-
151-
quote! {
152-
#incomparable
104+
// Nightly implementation.
105+
#[cfg(feature = "nightly")]
106+
quote! {
107+
#incomparable
153108

154-
let __self_disc = ::core::mem::discriminant(self);
155-
let __other_disc = ::core::mem::discriminant(__other);
109+
let __self_disc = ::core::intrinsics::discriminant_value(self);
110+
let __other_disc = ::core::intrinsics::discriminant_value(__other);
156111

157-
if __self_disc == __other_disc {
158-
#body_equal
159-
} else {
160-
#body_else
161-
}
162-
}
112+
if __self_disc == __other_disc {
113+
#body_equal
114+
} else {
115+
#path::#method(&__self_disc, &__other_disc)
163116
}
164117
}
165-
// Safe implementation when not on nightly.
166-
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
118+
119+
#[cfg(not(feature = "nightly"))]
167120
{
168121
let body_else = match discriminant {
169122
Discriminant::Single => {
@@ -175,15 +128,26 @@ pub fn build_ord_signature(item: &Item, trait_: &DeriveTrait, body: &TokenStream
175128
__other as isize,
176129
)
177130
},
131+
Discriminant::Default => {
132+
build_recursive_order(trait_, variants, &incomparable)
133+
}
178134
Discriminant::UnitRepr(repr) => quote! {
179135
#path::#method(
180136
self as #repr,
181137
__other as #repr,
182138
)
183139
},
184-
Discriminant::Default | Discriminant::Repr(_) => {
185-
build_recursive_order(trait_, variants, &incomparable)
140+
#[cfg(not(feature = "safe"))]
141+
Discriminant::Repr(repr) => {
142+
quote! {
143+
#path::#method(
144+
unsafe { *<*const _>::from(self).cast::<#repr>() },
145+
unsafe { *<*const _>::from(__other).cast::<#repr>() },
146+
)
147+
}
186148
}
149+
#[cfg(feature = "safe")]
150+
Discriminant::Repr(_) => build_recursive_order(trait_, variants, &incomparable),
187151
};
188152

189153
quote! {

0 commit comments

Comments
 (0)