Skip to main content

object_rainbow/
ordering.rs

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