Skip to content

Commit

Permalink
Fix comparison operation
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 27, 2022
1 parent 17b65c3 commit d85726c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
44 changes: 20 additions & 24 deletions fixed-map-derive/src/unit_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
let mut keys_iter_init = Vec::new();
let mut iter_init = Vec::new();
let mut entry = Vec::new();
let mut cmp_init = Vec::new();

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

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

let self_cmp_iter_init = quote! {{
let [#(#names),*] = &self.data;
let init: [(usize, &Option<V>); #count] = [#(#cmp_init),*];
#iterator::flat_map(#into_iter(init), |(k, v)| #option::Some((k, #option::as_ref(v)?)))
}};

let other_cmp_iter_init = quote! {{
let [#(#names),*] = &other.data;
let init: [(usize, &Option<V>); #count] = [#(#cmp_init),*];
#iterator::flat_map(#into_iter(init), |(k, v)| #option::Some((k, #option::as_ref(v)?)))
}};

Ok(quote! {
const #const_wrapper: () = {
#[repr(transparent)]
Expand Down Expand Up @@ -226,53 +240,35 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
impl<V> #partial_ord for Storage<V> where V: #partial_ord {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<#ordering> {
#partial_ord::partial_cmp(&self.data, &other.data)
#iterator::partial_cmp(#self_cmp_iter_init, #other_cmp_iter_init)
}

#[inline]
fn lt(&self, other: &Self) -> bool {
#partial_ord::lt(&self.data, &other.data)
#iterator::lt(#self_cmp_iter_init, #other_cmp_iter_init)
}

#[inline]
fn le(&self, other: &Self) -> bool {
#partial_ord::le(&self.data, &other.data)
#iterator::le(#self_cmp_iter_init, #other_cmp_iter_init)
}

#[inline]
fn gt(&self, other: &Self) -> bool {
#partial_ord::gt(&self.data, &other.data)
#iterator::gt(#self_cmp_iter_init, #other_cmp_iter_init)
}

#[inline]
fn ge(&self, other: &Self) -> bool {
#partial_ord::ge(&self.data, &other.data)
#iterator::ge(#self_cmp_iter_init, #other_cmp_iter_init)
}
}

#[automatically_derived]
impl<V> #ord for Storage<V> where V: #ord {
#[inline]
fn cmp(&self, other: &Self) -> #ordering {
#ord::cmp(self, other)
}

#[inline]
fn max(self, other: Self) -> Self {
Self { data: #ord::max(self.data, other.data) }
}

#[inline]
fn min(self, other: Self) -> Self {
Self { data: #ord::min(self.data, other.data) }
}

#[inline]
fn clamp(self, min: Self, max: Self) -> Self
where
Self: #partial_ord<Self>
{
Self { data: #ord::clamp(self.data, min.data, max.data) }
#iterator::cmp(#self_cmp_iter_init, #other_cmp_iter_init)
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,10 +1105,10 @@ where
/// let mut b = Map::new();
/// b.insert(Key::Second, 1);
///
/// assert!(a > b);
/// assert!(a >= b);
/// assert!(!(a < b));
/// assert!(!(a <= b));
/// assert!(a < b);
/// assert!(a <= b);
/// assert!(!(a > b));
/// assert!(!(a >= b));
/// ```
///
/// Using a composite key:
Expand All @@ -1129,7 +1129,7 @@ where
/// b.insert(Key::Second, 1);
///
/// // TODO: support this
/// // assert!(a > b);
/// // assert!(a < b);
/// ```
impl<K, V> PartialOrd for Map<K, V>
where
Expand Down Expand Up @@ -1181,10 +1181,10 @@ where
/// let mut b = Map::new();
/// b.insert(Key::Second, 1);
///
/// let mut list = vec![a, b];
/// let mut list = vec![b, a];
/// list.sort();
///
/// assert_eq!(list, [b, a]);
/// assert_eq!(list, [a, b]);
/// ```
///
/// Using a composite key:
Expand Down
12 changes: 6 additions & 6 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@ where
/// let mut b = Set::new();
/// b.insert(Key::Second);
///
/// assert!(a > b);
/// assert!(a >= b);
/// assert!(!(a < b));
/// assert!(!(a <= b));
/// assert!(a < b);
/// assert!(a <= b);
/// assert!(!(a > b));
/// assert!(!(a >= b));
/// ```
///
/// Using a composite key:
Expand Down Expand Up @@ -738,10 +738,10 @@ where
/// let mut b = Set::new();
/// b.insert(Key::Second);
///
/// let mut list = vec![a, b];
/// let mut list = vec![b, a];
/// list.sort();
///
/// assert_eq!(list, [b, a]);
/// assert_eq!(list, [a, b]);
/// ```
///
/// Using a composite key:
Expand Down

0 comments on commit d85726c

Please sign in to comment.