#66780 suggested an optimization for impl Ord for bool, and it was implemented in #66881.
However, for some reason, impl PartialOrd for bool was not optimized the same way:
|
impl Ord for bool { |
|
#[inline] |
|
fn cmp(&self, other: &bool) -> Ordering { |
|
// Casting to i8's and converting the difference to an Ordering generates |
|
// more optimal assembly. |
|
// See <https://github.com/rust-lang/rust/issues/66780> for more info. |
|
match (*self as i8) - (*other as i8) { |
|
-1 => Less, |
|
0 => Equal, |
|
1 => Greater, |
|
// SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else |
|
_ => unsafe { unreachable_unchecked() }, |
|
} |
|
} |
|
} |
|
impl PartialOrd for bool { |
|
#[inline] |
|
fn partial_cmp(&self, other: &bool) -> Option<Ordering> { |
|
(*self as u8).partial_cmp(&(*other as u8)) |
|
} |
|
} |
It can be implemented in terms of Ord:
impl PartialOrd for bool {
#[inline]
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
Some(self.cmp(other))
}
}
Is this deliberate or just something that no-one noticed?
#66780 suggested an optimization for
impl Ord for bool, and it was implemented in #66881.However, for some reason,
impl PartialOrd for boolwas not optimized the same way:rust/library/core/src/cmp.rs
Lines 1286 to 1300 in 1f7762b
rust/library/core/src/cmp.rs
Lines 1236 to 1241 in 1f7762b
It can be implemented in terms of
Ord:Is this deliberate or just something that no-one noticed?