Skip to content

Commit

Permalink
Remove unsafe code from core::cmp
Browse files Browse the repository at this point in the history
Instead of transmuting, use a match; the compiler has learnt how to
optimize it.
  • Loading branch information
ranma42 committed Jan 21, 2016
1 parent c6ba7fe commit 2f4622a
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/libcore/cmp.rs
Expand Up @@ -19,7 +19,6 @@

use self::Ordering::*;

use mem;
use marker::Sized;
use option::Option::{self, Some};

Expand Down Expand Up @@ -119,10 +118,6 @@ pub enum Ordering {
}

impl Ordering {
unsafe fn from_i8_unchecked(v: i8) -> Ordering {
mem::transmute(v)
}

/// Reverse the `Ordering`.
///
/// * `Less` becomes `Greater`.
Expand Down Expand Up @@ -155,14 +150,10 @@ impl Ordering {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reverse(self) -> Ordering {
unsafe {
// this compiles really nicely (to a single instruction);
// an explicit match has a pile of branches and
// comparisons.
//
// NB. it is safe because of the explicit discriminants
// given above.
Ordering::from_i8_unchecked(-(self as i8))
match self {
Less => Greater,
Equal => Equal,
Greater => Less,
}
}
}
Expand Down

0 comments on commit 2f4622a

Please sign in to comment.