Skip to main content

object_rainbow/
ordering.rs

1use std::cmp::Ordering;
2
3use crate::*;
4
5/// Traits for which total order matches that of [`ToOutput::vec`].
6pub trait ByteOrd: ToOutput + PartialOrd {
7    fn bytes_cmp(&self, other: &Self) -> Ordering {
8        self.vec().cmp(&other.vec())
9    }
10}
11
12pub struct OrderedByBytes<T>(pub T);
13
14impl<T: ByteOrd> PartialEq for OrderedByBytes<T> {
15    fn eq(&self, other: &Self) -> bool {
16        self.0.bytes_cmp(&other.0).is_eq()
17    }
18}
19
20impl<T: ByteOrd> Eq for OrderedByBytes<T> {}
21
22impl<T: ByteOrd> PartialOrd for OrderedByBytes<T> {
23    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
24        Some(self.cmp(other))
25    }
26}
27
28impl<T: ByteOrd> Ord for OrderedByBytes<T> {
29    fn cmp(&self, other: &Self) -> Ordering {
30        self.0.bytes_cmp(&other.0)
31    }
32}
33
34pub trait SignificantLength: ByteOrd {}