Skip to content

Commit d85726c

Browse files
committed
Fix comparison operation
1 parent 17b65c3 commit d85726c

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

fixed-map-derive/src/unit_variants.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
5959
let mut keys_iter_init = Vec::new();
6060
let mut iter_init = Vec::new();
6161
let mut entry = Vec::new();
62+
let mut cmp_init = Vec::new();
6263

6364
for (index, variant) in en.variants.iter().enumerate() {
6465
let var = &variant.ident;
@@ -85,6 +86,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
8586
iter_init.push(quote!((#ident::#var, #name)));
8687
names.push(name.clone());
8788
entry.push(quote!(option_to_entry(#name, key)));
89+
cmp_init.push(quote!((#index, #name)));
8890
}
8991

9092
let count = en.variants.len();
@@ -174,6 +176,18 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
174176
quote!()
175177
};
176178

179+
let self_cmp_iter_init = quote! {{
180+
let [#(#names),*] = &self.data;
181+
let init: [(usize, &Option<V>); #count] = [#(#cmp_init),*];
182+
#iterator::flat_map(#into_iter(init), |(k, v)| #option::Some((k, #option::as_ref(v)?)))
183+
}};
184+
185+
let other_cmp_iter_init = quote! {{
186+
let [#(#names),*] = &other.data;
187+
let init: [(usize, &Option<V>); #count] = [#(#cmp_init),*];
188+
#iterator::flat_map(#into_iter(init), |(k, v)| #option::Some((k, #option::as_ref(v)?)))
189+
}};
190+
177191
Ok(quote! {
178192
const #const_wrapper: () = {
179193
#[repr(transparent)]
@@ -226,53 +240,35 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
226240
impl<V> #partial_ord for Storage<V> where V: #partial_ord {
227241
#[inline]
228242
fn partial_cmp(&self, other: &Self) -> Option<#ordering> {
229-
#partial_ord::partial_cmp(&self.data, &other.data)
243+
#iterator::partial_cmp(#self_cmp_iter_init, #other_cmp_iter_init)
230244
}
231245

232246
#[inline]
233247
fn lt(&self, other: &Self) -> bool {
234-
#partial_ord::lt(&self.data, &other.data)
248+
#iterator::lt(#self_cmp_iter_init, #other_cmp_iter_init)
235249
}
236250

237251
#[inline]
238252
fn le(&self, other: &Self) -> bool {
239-
#partial_ord::le(&self.data, &other.data)
253+
#iterator::le(#self_cmp_iter_init, #other_cmp_iter_init)
240254
}
241255

242256
#[inline]
243257
fn gt(&self, other: &Self) -> bool {
244-
#partial_ord::gt(&self.data, &other.data)
258+
#iterator::gt(#self_cmp_iter_init, #other_cmp_iter_init)
245259
}
246260

247261
#[inline]
248262
fn ge(&self, other: &Self) -> bool {
249-
#partial_ord::ge(&self.data, &other.data)
263+
#iterator::ge(#self_cmp_iter_init, #other_cmp_iter_init)
250264
}
251265
}
252266

253267
#[automatically_derived]
254268
impl<V> #ord for Storage<V> where V: #ord {
255269
#[inline]
256270
fn cmp(&self, other: &Self) -> #ordering {
257-
#ord::cmp(self, other)
258-
}
259-
260-
#[inline]
261-
fn max(self, other: Self) -> Self {
262-
Self { data: #ord::max(self.data, other.data) }
263-
}
264-
265-
#[inline]
266-
fn min(self, other: Self) -> Self {
267-
Self { data: #ord::min(self.data, other.data) }
268-
}
269-
270-
#[inline]
271-
fn clamp(self, min: Self, max: Self) -> Self
272-
where
273-
Self: #partial_ord<Self>
274-
{
275-
Self { data: #ord::clamp(self.data, min.data, max.data) }
271+
#iterator::cmp(#self_cmp_iter_init, #other_cmp_iter_init)
276272
}
277273
}
278274

src/map.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,10 @@ where
11051105
/// let mut b = Map::new();
11061106
/// b.insert(Key::Second, 1);
11071107
///
1108-
/// assert!(a > b);
1109-
/// assert!(a >= b);
1110-
/// assert!(!(a < b));
1111-
/// assert!(!(a <= b));
1108+
/// assert!(a < b);
1109+
/// assert!(a <= b);
1110+
/// assert!(!(a > b));
1111+
/// assert!(!(a >= b));
11121112
/// ```
11131113
///
11141114
/// Using a composite key:
@@ -1129,7 +1129,7 @@ where
11291129
/// b.insert(Key::Second, 1);
11301130
///
11311131
/// // TODO: support this
1132-
/// // assert!(a > b);
1132+
/// // assert!(a < b);
11331133
/// ```
11341134
impl<K, V> PartialOrd for Map<K, V>
11351135
where
@@ -1181,10 +1181,10 @@ where
11811181
/// let mut b = Map::new();
11821182
/// b.insert(Key::Second, 1);
11831183
///
1184-
/// let mut list = vec![a, b];
1184+
/// let mut list = vec![b, a];
11851185
/// list.sort();
11861186
///
1187-
/// assert_eq!(list, [b, a]);
1187+
/// assert_eq!(list, [a, b]);
11881188
/// ```
11891189
///
11901190
/// Using a composite key:

src/set.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,10 @@ where
662662
/// let mut b = Set::new();
663663
/// b.insert(Key::Second);
664664
///
665-
/// assert!(a > b);
666-
/// assert!(a >= b);
667-
/// assert!(!(a < b));
668-
/// assert!(!(a <= b));
665+
/// assert!(a < b);
666+
/// assert!(a <= b);
667+
/// assert!(!(a > b));
668+
/// assert!(!(a >= b));
669669
/// ```
670670
///
671671
/// Using a composite key:
@@ -738,10 +738,10 @@ where
738738
/// let mut b = Set::new();
739739
/// b.insert(Key::Second);
740740
///
741-
/// let mut list = vec![a, b];
741+
/// let mut list = vec![b, a];
742742
/// list.sort();
743743
///
744-
/// assert_eq!(list, [b, a]);
744+
/// assert_eq!(list, [a, b]);
745745
/// ```
746746
///
747747
/// Using a composite key:

0 commit comments

Comments
 (0)