2 use std::cmp::Ordering::{self, *};
5 // Minimal type with an `Ord` implementation violating transitivity.
14 impl PartialOrd for Cyclic3 {
15 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
20 impl Ord for Cyclic3 {
21 fn cmp(&self, other: &Self) -> Ordering {
23 (A, A) | (B, B) | (C, C) => Equal,
24 (A, B) | (B, C) | (C, A) => Less,
25 (A, C) | (B, A) | (C, B) => Greater,
30 impl PartialEq for Cyclic3 {
31 fn eq(&self, other: &Self) -> bool {
32 self.cmp(&other) == Equal
36 impl Eq for Cyclic3 {}
38 // Controls the ordering of values wrapped by `Governed`.
45 pub fn new() -> Self {
46 Governor { flipped: Cell::new(false) }
50 self.flipped.set(!self.flipped.get());
54 // Type with an `Ord` implementation that forms a total order at any moment
55 // (assuming that `T` respects total order), but can suddenly be made to invert
58 pub struct Governed<'a, T>(pub T, pub &'a Governor);
60 impl<T: Ord> PartialOrd for Governed<'_, T> {
61 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
66 impl<T: Ord> Ord for Governed<'_, T> {
67 fn cmp(&self, other: &Self) -> Ordering {
68 assert!(ptr::eq(self.1, other.1));
69 let ord = self.0.cmp(&other.0);
70 if self.1.flipped.get() { ord.reverse() } else { ord }
74 impl<T: PartialEq> PartialEq for Governed<'_, T> {
75 fn eq(&self, other: &Self) -> bool {
76 assert!(ptr::eq(self.1, other.1));
81 impl<T: Eq> Eq for Governed<'_, T> {}