Skip to content

Commit fc4dee4

Browse files
committed
Use an Option<Ordering> instead of PartialOrdering
1 parent 18f92ff commit fc4dee4

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

active/0000-partial-cmp.md

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,69 +30,48 @@ the required methods.
3030

3131
# Detailed design
3232

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
4334
methods:
4435
```rust
4536
pub trait PartialOrd {
46-
fn partial_cmp(&self, other: &Self) -> PartialOrdering;
37+
fn partial_cmp(&self, other: &Self) -> Option<Ordering>;
4738

4839
fn lt(&self, other: &Self) -> bool {
4940
match self.partial_cmp(other) {
50-
PartialOrdLess => true,
41+
Some(Less) => true,
5142
_ => false,
5243
}
5344
}
5445

5546
le(&self, other: &Self) -> bool {
5647
match self.partial_cmp(other) {
57-
PartialOrdLess | PartialOrdEqual => true,
48+
Some(Less) | Some(Equal) => true,
5849
_ => false,
5950
}
6051
}
6152

6253
fn gt(&self, other: &Self) -> bool {
6354
match self.partial_cmp(other) {
64-
PartialOrdGreater => true,
55+
Some(Greater) => true,
6556
_ => false,
6657
}
6758
}
6859

6960
ge(&self, other: &Self) -> bool {
7061
match self.partial_cmp(other) {
71-
PartialOrdGreater | PartialOrdEqual => true,
62+
Some(Greater) | Some(Equal) => true,
7263
_ => false,
7364
}
7465
}
7566
}
7667
```
7768

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:
9271
```rust
9372
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))
9675
}
9776
}
9877
```
@@ -121,18 +100,28 @@ be implemented first for that to be workrable.
121100
We may want to add something similar to `PartialEq` as well. I don't know what
122101
it would be called, though (maybe `partial_eq`?):
123102
```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,
129108
}
130109

131110
pub trait PartialEq {
132-
fn partial_eq(&self, other: &Self) -> PartialEquality;
111+
fn partial_eq(&self, other: &Self) -> Option<Equality>;
133112

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+
}
135119

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+
}
137126
}
138127
```

0 commit comments

Comments
 (0)