@@ -7,10 +7,15 @@ use syn::PatOr;
77
88#[ cfg( not( feature = "nightly" ) ) ]
99use crate :: Discriminant ;
10- use crate :: { data :: SimpleType , Data , DeriveTrait , Item } ;
10+ use crate :: { Data , DeriveTrait , Item , SimpleType , Trait } ;
1111
1212/// Build signature for [`PartialOrd`] and [`Ord`].
13- pub fn build_ord_signature ( item : & Item , trait_ : & DeriveTrait , body : & TokenStream ) -> TokenStream {
13+ pub fn build_ord_signature (
14+ item : & Item ,
15+ traits : & [ DeriveTrait ] ,
16+ trait_ : & DeriveTrait ,
17+ body : & TokenStream ,
18+ ) -> TokenStream {
1419 let mut equal = quote ! { :: core:: cmp:: Ordering :: Equal } ;
1520
1621 // Add `Option` to `Ordering` if we are implementing `PartialOrd`.
@@ -122,23 +127,50 @@ pub fn build_ord_signature(item: &Item, trait_: &DeriveTrait, body: &TokenStream
122127 Discriminant :: Single => {
123128 unreachable ! ( "we should only generate this code with multiple variants" )
124129 }
125- Discriminant :: UnitDefault => quote ! {
126- #path:: #method(
127- self as isize ,
128- __other as isize ,
129- )
130- } ,
130+ Discriminant :: UnitDefault => {
131+ if traits. iter ( ) . any ( |trait_| trait_ == Trait :: Copy ) {
132+ quote ! {
133+ #path:: #method(
134+ * self as isize ,
135+ * __other as isize ,
136+ )
137+ }
138+ } else if traits. iter ( ) . any ( |trait_| trait_ == Trait :: Clone ) {
139+ quote ! {
140+ #path:: #method(
141+ self . clone( ) as isize ,
142+ __other. clone( ) as isize ,
143+ )
144+ }
145+ } else {
146+ build_recursive_order ( trait_, variants, & incomparable)
147+ }
148+ }
131149 Discriminant :: Unknown => {
132150 build_recursive_order ( trait_, variants, & incomparable)
133151 }
134- Discriminant :: UnitRepr ( repr) => quote ! {
135- #path:: #method(
136- self as #repr,
137- __other as #repr,
138- )
139- } ,
152+ #[ cfg( feature = "safe" ) ]
153+ Discriminant :: UnitRepr ( repr) => {
154+ if traits. iter ( ) . any ( |trait_| trait_ == Trait :: Copy ) {
155+ quote ! {
156+ #path:: #method(
157+ * self as #repr,
158+ * __other as #repr,
159+ )
160+ }
161+ } else if traits. iter ( ) . any ( |trait_| trait_ == Trait :: Clone ) {
162+ quote ! {
163+ #path:: #method(
164+ self . clone( ) as #repr,
165+ __other. clone( ) as #repr,
166+ )
167+ }
168+ } else {
169+ build_recursive_order ( trait_, variants, & incomparable)
170+ }
171+ }
140172 #[ cfg( not( feature = "safe" ) ) ]
141- Discriminant :: Repr ( repr) => {
173+ Discriminant :: UnitRepr ( repr ) | Discriminant :: Repr ( repr) => {
142174 quote ! {
143175 #path:: #method(
144176 unsafe { * <* const _>:: from( self ) . cast:: <#repr>( ) } ,
0 commit comments