Skip to content

Commit b5d0149

Browse files
committed
Implement Eq and Ord for Const
1 parent fe4f169 commit b5d0149

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

fathom/src/core.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub enum UIntStyle {
555555
}
556556

557557
/// Constants
558-
#[derive(Debug, Copy, Clone, PartialOrd)]
558+
#[derive(Debug, Copy, Clone)]
559559
pub enum Const {
560560
Bool(bool),
561561
U8(u8, UIntStyle),
@@ -566,7 +566,6 @@ pub enum Const {
566566
S16(i16),
567567
S32(i32),
568568
S64(i64),
569-
// TODO: use logical equality for floating point numbers
570569
F32(f32),
571570
F64(f64),
572571
Pos(usize),
@@ -585,15 +584,66 @@ impl PartialEq for Const {
585584
(Const::S16(a), Const::S16(b)) => a == b,
586585
(Const::S32(a), Const::S32(b)) => a == b,
587586
(Const::S64(a), Const::S64(b)) => a == b,
588-
(Const::F32(a), Const::F32(b)) => a == b,
589-
(Const::F64(a), Const::F64(b)) => a == b,
587+
(Const::F32(a), Const::F32(b)) => a.total_cmp(&b).is_eq(),
588+
(Const::F64(a), Const::F64(b)) => a.total_cmp(&b).is_eq(),
590589
(Const::Pos(a), Const::Pos(b)) => a == b,
591590
(Const::Ref(a), Const::Ref(b)) => a == b,
592591
_ => false,
593592
}
594593
}
595594
}
596595

596+
impl Eq for Const {}
597+
598+
impl PartialOrd for Const {
599+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
600+
Some(self.cmp(other))
601+
}
602+
}
603+
604+
impl Ord for Const {
605+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
606+
match (*self, *other) {
607+
(Const::Bool(a), Const::Bool(b)) => a.cmp(&b),
608+
(Const::U8(a, _), Const::U8(b, _)) => a.cmp(&b),
609+
(Const::U16(a, _), Const::U16(b, _)) => a.cmp(&b),
610+
(Const::U32(a, _), Const::U32(b, _)) => a.cmp(&b),
611+
(Const::U64(a, _), Const::U64(b, _)) => a.cmp(&b),
612+
(Const::S8(a), Const::S8(b)) => a.cmp(&b),
613+
(Const::S16(a), Const::S16(b)) => a.cmp(&b),
614+
(Const::S32(a), Const::S32(b)) => a.cmp(&b),
615+
(Const::S64(a), Const::S64(b)) => a.cmp(&b),
616+
(Const::F32(a), Const::F32(b)) => a.total_cmp(&b),
617+
(Const::F64(a), Const::F64(b)) => a.total_cmp(&b),
618+
(Const::Pos(a), Const::Pos(b)) => a.cmp(&b),
619+
(Const::Ref(a), Const::Ref(b)) => a.cmp(&b),
620+
_ => {
621+
fn discriminant(r#const: &Const) -> usize {
622+
match r#const {
623+
Const::Bool(_) => 0,
624+
Const::U8(_, _) => 1,
625+
Const::U16(_, _) => 2,
626+
Const::U32(_, _) => 3,
627+
Const::U64(_, _) => 4,
628+
Const::S8(_) => 5,
629+
Const::S16(_) => 6,
630+
Const::S32(_) => 7,
631+
Const::S64(_) => 8,
632+
Const::F32(_) => 9,
633+
Const::F64(_) => 10,
634+
Const::Pos(_) => 11,
635+
Const::Ref(_) => 12,
636+
}
637+
}
638+
639+
let tag1 = discriminant(self);
640+
let tag2 = discriminant(other);
641+
tag1.cmp(&tag2)
642+
}
643+
}
644+
}
645+
}
646+
597647
pub trait ToBeBytes<const N: usize> {
598648
fn to_be_bytes(self) -> [u8; N];
599649
}

0 commit comments

Comments
 (0)