Skip to content

Commit 9e0a88b

Browse files
committed
Improve code generation of Ord and PartialOrd for the first and last variant
1 parent c4e01dc commit 9e0a88b

File tree

4 files changed

+52
-115
lines changed

4 files changed

+52
-115
lines changed

src/test/basic.rs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,7 @@ fn enum_() -> Result<()> {
220220
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
221221
let ord = quote! {
222222
match self {
223-
Test::A { field: ref __field } =>
224-
match __other {
225-
Test::B { } => ::core::cmp::Ordering::Less,
226-
Test::C(ref __other_0) => ::core::cmp::Ordering::Less,
227-
Test::D() => ::core::cmp::Ordering::Less,
228-
Test::E => ::core::cmp::Ordering::Less,
229-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
230-
},
223+
Test::A { field: ref __field } => ::core::cmp::Ordering::Less,
231224
Test::B { } =>
232225
match __other {
233226
Test::A { field: ref __other_field } => ::core::cmp::Ordering::Greater,
@@ -252,14 +245,7 @@ fn enum_() -> Result<()> {
252245
Test::E => ::core::cmp::Ordering::Less,
253246
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
254247
},
255-
Test::E =>
256-
match __other {
257-
Test::A { field: ref __other_field } => ::core::cmp::Ordering::Greater,
258-
Test::B { } => ::core::cmp::Ordering::Greater,
259-
Test::C(ref __other_0) => ::core::cmp::Ordering::Greater,
260-
Test::D() => ::core::cmp::Ordering::Greater,
261-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
262-
},
248+
Test::E => ::core::cmp::Ordering::Greater,
263249
}
264250
};
265251
#[cfg(feature = "nightly")]
@@ -276,14 +262,7 @@ fn enum_() -> Result<()> {
276262
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
277263
let partial_ord = quote! {
278264
match self {
279-
Test::A { field: ref __field } =>
280-
match __other {
281-
Test::B { } => ::core::option::Option::Some(::core::cmp::Ordering::Less),
282-
Test::C(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Less),
283-
Test::D() => ::core::option::Option::Some(::core::cmp::Ordering::Less),
284-
Test::E => ::core::option::Option::Some(::core::cmp::Ordering::Less),
285-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
286-
},
265+
Test::A { field: ref __field } => ::core::option::Option::Some(::core::cmp::Ordering::Less),
287266
Test::B { } =>
288267
match __other {
289268
Test::A { field: ref __other_field } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
@@ -308,14 +287,7 @@ fn enum_() -> Result<()> {
308287
Test::E => ::core::option::Option::Some(::core::cmp::Ordering::Less),
309288
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
310289
},
311-
Test::E =>
312-
match __other {
313-
Test::A { field: ref __other_field } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
314-
Test::B { } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
315-
Test::C(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
316-
Test::D() => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
317-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
318-
},
290+
Test::E => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
319291
}
320292
};
321293

src/test/enum_.rs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,8 @@ fn two_data() -> Result<()> {
136136
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
137137
let partial_ord = quote! {
138138
match self {
139-
Test::A(ref __0) =>
140-
match __other {
141-
Test::B(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Less),
142-
_ => #unreachable,
143-
},
144-
Test::B(ref __0) =>
145-
match __other {
146-
Test::A(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
147-
_ => #unreachable,
148-
},
139+
Test::A(ref __0) => ::core::option::Option::Some(::core::cmp::Ordering::Less),
140+
Test::B(ref __0) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
149141
}
150142
};
151143

@@ -228,16 +220,8 @@ fn unit() -> Result<()> {
228220
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
229221
let partial_ord = quote! {
230222
match self {
231-
Test::A(ref __0) =>
232-
match __other {
233-
Test::B => ::core::option::Option::Some(::core::cmp::Ordering::Less),
234-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
235-
},
236-
Test::B =>
237-
match __other {
238-
Test::A(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
239-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
240-
},
223+
Test::A(ref __0) => ::core::option::Option::Some(::core::cmp::Ordering::Less),
224+
Test::B => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
241225
}
242226
};
243227

@@ -313,16 +297,8 @@ fn struct_unit() -> Result<()> {
313297
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
314298
let partial_ord = quote! {
315299
match self {
316-
Test::A(ref __0) =>
317-
match __other {
318-
Test::B { } => ::core::option::Option::Some(::core::cmp::Ordering::Less),
319-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
320-
},
321-
Test::B { } =>
322-
match __other {
323-
Test::A(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
324-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
325-
},
300+
Test::A(ref __0) => ::core::option::Option::Some(::core::cmp::Ordering::Less),
301+
Test::B { } => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
326302
}
327303
};
328304

@@ -398,16 +374,8 @@ fn tuple_unit() -> Result<()> {
398374
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
399375
let partial_ord = quote! {
400376
match self {
401-
Test::A(ref __0) =>
402-
match __other {
403-
Test::B() => ::core::option::Option::Some(::core::cmp::Ordering::Less),
404-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
405-
},
406-
Test::B() =>
407-
match __other {
408-
Test::A(ref __other_0) => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
409-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
410-
},
377+
Test::A(ref __0) => ::core::option::Option::Some(::core::cmp::Ordering::Less),
378+
Test::B() => ::core::option::Option::Some(::core::cmp::Ordering::Greater),
411379
}
412380
};
413381

src/test/skip.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,8 @@ fn variants_empty() -> Result<()> {
122122
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
123123
let ord = quote! {
124124
match self {
125-
Test::A(ref __0) =>
126-
match __other {
127-
Test::B (ref __other_0) => ::core::cmp::Ordering::Less,
128-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
129-
},
130-
Test::B(ref __0) =>
131-
match __other {
132-
Test::A (ref __other_0) => ::core::cmp::Ordering::Greater,
133-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
134-
},
125+
Test::A(ref __0) => ::core::cmp::Ordering::Less,
126+
Test::B(ref __0) => ::core::cmp::Ordering::Greater,
135127
}
136128
};
137129

@@ -190,16 +182,8 @@ fn variants_partly_empty() -> Result<()> {
190182
#[cfg(all(not(feature = "nightly"), feature = "safe"))]
191183
let ord = quote! {
192184
match self {
193-
Test::A(ref __0) =>
194-
match __other {
195-
Test::B (ref __other_0, ref __other_1) => ::core::cmp::Ordering::Less,
196-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
197-
},
198-
Test::B(ref __0, ref __1) =>
199-
match __other {
200-
Test::A (ref __other_0) => ::core::cmp::Ordering::Greater,
201-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
202-
},
185+
Test::A(ref __0) => ::core::cmp::Ordering::Less,
186+
Test::B(ref __0, ref __1) => ::core::cmp::Ordering::Greater,
203187
}
204188
};
205189

src/trait_/common_ord.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,35 +105,48 @@ pub fn build_ord_signature(item: &Item, trait_: &DeriveTrait, body: &TokenStream
105105
// other. The index for these variants is used to determine which
106106
// `Ordering` to return.
107107
for (index, variant) in variants.iter().enumerate() {
108-
let mut arms = Vec::with_capacity(variants.len() - 1);
108+
let pattern = &variant.self_pattern();
109109

110-
for (index_other, variant_other) in variants.iter().enumerate() {
111-
// Make sure we aren't comparing the same variant with itself.
112-
if index != index_other {
113-
use std::cmp::Ordering::*;
110+
// The first variant is always `Less` then everything.
111+
if index == 0 {
112+
different.push(quote! {
113+
#pattern => #less,
114+
})
115+
}
116+
// The last variant is always `Greater` then everything.
117+
else if index == variants.len() - 1 {
118+
different.push(quote! {
119+
#pattern => #greater,
120+
})
121+
} else {
122+
let mut arms = Vec::with_capacity(variants.len() - 1);
114123

115-
let ordering = match index.cmp(&index_other) {
116-
Less => &less,
117-
Equal => &equal,
118-
Greater => &greater,
119-
};
124+
for (index_other, variant_other) in variants.iter().enumerate() {
125+
// Make sure we aren't comparing the same variant with itself.
126+
if index != index_other {
127+
use std::cmp::Ordering::*;
120128

121-
let pattern = &variant_other.other_pattern();
129+
let ordering = match index.cmp(&index_other) {
130+
Less => &less,
131+
Equal => &equal,
132+
Greater => &greater,
133+
};
122134

123-
arms.push(quote! {
124-
#pattern => #ordering,
125-
});
126-
}
127-
}
135+
let pattern = &variant_other.other_pattern();
128136

129-
let pattern = &variant.self_pattern();
137+
arms.push(quote! {
138+
#pattern => #ordering,
139+
});
140+
}
141+
}
130142

131-
different.push(quote! {
132-
#pattern => match __other {
133-
#(#arms)*
134-
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
135-
},
136-
});
143+
different.push(quote! {
144+
#pattern => match __other {
145+
#(#arms)*
146+
_ => ::core::unreachable!("comparing variants yielded unexpected results"),
147+
},
148+
});
149+
}
137150
}
138151

139152
quote! {

0 commit comments

Comments
 (0)