@@ -30,69 +30,48 @@ the required methods.
30
30
31
31
# Detailed design
32
32
33
- Add an enum to ` core::cmp ` :
34
- ``` rust
35
- pub enum PartialOrdering {
36
- PartialOrdLess ,
37
- PartialOrdEqual ,
38
- PartialOrdGreater ,
39
- PartialOrdUnordered ,
40
- }
41
- ```
42
- and a method to ` PartialOrd ` , changing the default implementations of the other
33
+ Add a method to ` PartialOrd ` , changing the default implementations of the other
43
34
methods:
44
35
``` rust
45
36
pub trait PartialOrd {
46
- fn partial_cmp (& self , other : & Self ) -> PartialOrdering ;
37
+ fn partial_cmp (& self , other : & Self ) -> Option < Ordering > ;
47
38
48
39
fn lt (& self , other : & Self ) -> bool {
49
40
match self . partial_cmp (other ) {
50
- PartialOrdLess => true ,
41
+ Some ( Less ) => true ,
51
42
_ => false ,
52
43
}
53
44
}
54
45
55
46
le (& self , other : & Self ) -> bool {
56
47
match self . partial_cmp (other ) {
57
- PartialOrdLess | PartialOrdEqual => true ,
48
+ Some ( Less ) | Some ( Equal ) => true ,
58
49
_ => false ,
59
50
}
60
51
}
61
52
62
53
fn gt (& self , other : & Self ) -> bool {
63
54
match self . partial_cmp (other ) {
64
- PartialOrdGreater => true ,
55
+ Some ( Greater ) => true ,
65
56
_ => false ,
66
57
}
67
58
}
68
59
69
60
ge (& self , other : & Self ) -> bool {
70
61
match self . partial_cmp (other ) {
71
- PartialOrdGreater | PartialOrdEqual => true ,
62
+ Some ( Greater ) | Some ( Equal ) => true ,
72
63
_ => false ,
73
64
}
74
65
}
75
66
}
76
67
```
77
68
78
- Since almost all ordered types have a total ordering, add a method to convert
79
- an ` Ordering ` to a ` PartialOrdering ` :
80
- ``` rust
81
- impl Ordering {
82
- pub fn to_partial (& self ) -> PartialOrdering {
83
- Less => PartialOrdLess ,
84
- Equal => PartialOrdEqual ,
85
- Greater => PartialOrdGreater ,
86
- }
87
- }
88
- ```
89
-
90
- This allows the implementation of ` PartialOrd ` to in most cases be as simple
91
- as
69
+ Since almost all ordered types have a total ordering, the implementation of
70
+ ` partial_cmp ` is trivial in most cases:
92
71
``` rust
93
72
impl PartialOrd for Foo {
94
- fn partial_cmp (& self , other : & Self ) -> PartialOrdering {
95
- self . cmp (other ). to_partial ( )
73
+ fn partial_cmp (& self , other : & Self ) -> Option < Ordering > {
74
+ Some ( self . cmp (other ))
96
75
}
97
76
}
98
77
```
@@ -121,18 +100,28 @@ be implemented first for that to be workrable.
121
100
We may want to add something similar to ` PartialEq ` as well. I don't know what
122
101
it would be called, though (maybe ` partial_eq ` ?):
123
102
``` rust
124
- #[deriving( Eq )]
125
- pub enum PartialEquality {
126
- PartialEqEqual ,
127
- PartialEqNotEqual ,
128
- PartialEqUncomparable ,
103
+ // I don't feel great about these variant names, but `Equal` is already taken
104
+ // by `Ordering` which is in the same module.
105
+ pub enum Equality {
106
+ AreEqual ,
107
+ AreUnequal ,
129
108
}
130
109
131
110
pub trait PartialEq {
132
- fn partial_eq (& self , other : & Self ) -> PartialEquality ;
111
+ fn partial_eq (& self , other : & Self ) -> Option < Equality > ;
133
112
134
- fn eq (& self , other : & Self ) -> bool { self . partial_eq (other ) == PartialEqEqual }
113
+ fn eq (& self , other : & Self ) -> bool {
114
+ match self . partial_eq (other ) {
115
+ Some (AreEqual ) => true ,
116
+ _ => false ,
117
+ }
118
+ }
135
119
136
- fn neq (& self , other : & Self ) -> bool { self . partial_eq (other ) == PartialEqNotEqual }
120
+ fn neq (& self , other : & Self ) -> bool {
121
+ match self . partial_eq (other ) {
122
+ Some (AreUnequal ) => true ,
123
+ _ => false ,
124
+ }
125
+ }
137
126
}
138
127
```
0 commit comments