1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Traits for dynamically-typed equality comparison and ordering.
use Ordering;
use Any;
/// A trait for comparing dynamically-typed values for equality.
///
/// After coercing your values to a trait object of type `DynEq`,
/// you can directly compare references (and smart pointers) to
/// instances via the usual `==` or `!=` operators.
///
/// Trait objects created from different concrete underlying types
/// are considered not equal. Trait objects created from the same
/// underlying concrete type are compared using `PartialEq`.
///
/// ```
/// # use dyn_ord::DynEq;
/// let x: &dyn DynEq = &42;
/// let y: &dyn DynEq = &String::from("qux");
/// let z: &dyn DynEq = &String::from("baz");
///
/// assert!(*x == *x);
/// assert!(*x != *y);
/// assert!(*x != *z);
///
/// assert!(*y != *x);
/// assert!(*y == *y);
/// assert!(*y != *z);
///
/// assert!(*z != *x);
/// assert!(*z != *y);
/// assert!(*z == *z);
/// ```
/// A trait for comparing dynamically-typed values for ordering.
///
/// After coercing your values to a trait object of type `DynOrd`,
/// you can directly compare references (and smart pointers) to
/// instances via the usual `<`, `<=`, `>`, `>=`, `==` or `!=`.
///
/// Trait objects created from different concrete underlying types
/// are considered not comparable. Trait objects created from the
/// same underlying concrete type are compared using `PartialOrd`.
///
/// ```
/// # use core::cmp::Ordering;
/// # use std::rc::Rc;
/// # use dyn_ord::DynOrd;
/// let x: Rc<dyn DynOrd> = Rc::new(1337);
/// let y: Rc<dyn DynOrd> = Rc::new(String::from("qux"));
/// let z: Rc<dyn DynOrd> = Rc::new(String::from("baz"));
///
/// assert_eq!(y.partial_cmp(&z), Some(Ordering::Greater));
/// assert_eq!(z.partial_cmp(&y), Some(Ordering::Less));
/// assert_eq!(y.partial_cmp(&y), Some(Ordering::Equal));
/// assert_eq!(x.partial_cmp(&y), None);
/// ```