Remove unsafe code from core::cmp

Instead of transmuting, use a match; the compiler has learnt how to
optimize it.
This commit is contained in:
Andrea Canciani
2016-01-21 20:20:22 +01:00
parent c6ba7fee97
commit 2f4622a36f

View File

@@ -19,7 +19,6 @@
use self::Ordering::*; use self::Ordering::*;
use mem;
use marker::Sized; use marker::Sized;
use option::Option::{self, Some}; use option::Option::{self, Some};
@@ -119,10 +118,6 @@ pub enum Ordering {
} }
impl Ordering { impl Ordering {
unsafe fn from_i8_unchecked(v: i8) -> Ordering {
mem::transmute(v)
}
/// Reverse the `Ordering`. /// Reverse the `Ordering`.
/// ///
/// * `Less` becomes `Greater`. /// * `Less` becomes `Greater`.
@@ -155,14 +150,10 @@ impl Ordering {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub fn reverse(self) -> Ordering { pub fn reverse(self) -> Ordering {
unsafe { match self {
// this compiles really nicely (to a single instruction); Less => Greater,
// an explicit match has a pile of branches and Equal => Equal,
// comparisons. Greater => Less,
//
// NB. it is safe because of the explicit discriminants
// given above.
Ordering::from_i8_unchecked(-(self as i8))
} }
} }
} }