mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 06:44:27 +01:00
Fix the ordering functions of the Number type
This commit is contained in:
parent
840217b111
commit
d9678f0040
@ -1,3 +1,4 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::{ParseFloatError, ParseIntError};
|
use std::num::{ParseFloatError, ParseIntError};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -5,7 +6,7 @@ use std::str::FromStr;
|
|||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash)]
|
||||||
pub enum Number {
|
pub enum Number {
|
||||||
Unsigned(u64),
|
Unsigned(u64),
|
||||||
Signed(i64),
|
Signed(i64),
|
||||||
@ -39,6 +40,50 @@ impl FromStr for Number {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Number {
|
||||||
|
fn eq(&self, other: &Number) -> bool {
|
||||||
|
self.cmp(other) == Ordering::Equal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for Number {}
|
||||||
|
|
||||||
|
impl PartialOrd for Number {
|
||||||
|
fn partial_cmp(&self, other: &Number) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for Number {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
use Number::{Float, Signed, Unsigned};
|
||||||
|
|
||||||
|
match (*self, *other) {
|
||||||
|
(Unsigned(a), Unsigned(b)) => a.cmp(&b),
|
||||||
|
(Unsigned(a), Signed(b)) => {
|
||||||
|
if b < 0 {
|
||||||
|
Ordering::Greater
|
||||||
|
} else {
|
||||||
|
a.cmp(&(b as u64))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Unsigned(a), Float(b)) => (OrderedFloat(a as f64)).cmp(&b),
|
||||||
|
(Signed(a), Unsigned(b)) => {
|
||||||
|
if a < 0 {
|
||||||
|
Ordering::Less
|
||||||
|
} else {
|
||||||
|
(a as u64).cmp(&b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Signed(a), Signed(b)) => a.cmp(&b),
|
||||||
|
(Signed(a), Float(b)) => OrderedFloat(a as f64).cmp(&b),
|
||||||
|
(Float(a), Unsigned(b)) => a.cmp(&OrderedFloat(b as f64)),
|
||||||
|
(Float(a), Signed(b)) => a.cmp(&OrderedFloat(b as f64)),
|
||||||
|
(Float(a), Float(b)) => a.cmp(&b),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct ParseNumberError {
|
pub struct ParseNumberError {
|
||||||
uint_error: ParseIntError,
|
uint_error: ParseIntError,
|
||||||
|
Loading…
Reference in New Issue
Block a user