Skip to content

Commit e1cb0a3

Browse files
committed
Auto merge of #31084 - ranma42:cmd-no-unsafe, r=alexcrichton
Instead of transmuting, use a match; the compiler has learnt how to optimize it.
2 parents 188c50c + 2f4622a commit e1cb0a3

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

src/libcore/cmp.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use self::Ordering::*;
2121

22-
use mem;
2322
use marker::Sized;
2423
use option::Option::{self, Some};
2524

@@ -119,10 +118,6 @@ pub enum Ordering {
119118
}
120119

121120
impl Ordering {
122-
unsafe fn from_i8_unchecked(v: i8) -> Ordering {
123-
mem::transmute(v)
124-
}
125-
126121
/// Reverse the `Ordering`.
127122
///
128123
/// * `Less` becomes `Greater`.
@@ -155,14 +150,10 @@ impl Ordering {
155150
#[inline]
156151
#[stable(feature = "rust1", since = "1.0.0")]
157152
pub fn reverse(self) -> Ordering {
158-
unsafe {
159-
// this compiles really nicely (to a single instruction);
160-
// an explicit match has a pile of branches and
161-
// comparisons.
162-
//
163-
// NB. it is safe because of the explicit discriminants
164-
// given above.
165-
Ordering::from_i8_unchecked(-(self as i8))
153+
match self {
154+
Less => Greater,
155+
Equal => Equal,
156+
Greater => Less,
166157
}
167158
}
168159
}

0 commit comments

Comments
 (0)