Skip to content

Commit fd4ac0e

Browse files
committed
Hasher: replace unsafe trasmute with to_ne_bytes
Spead the knowledge of `to_ne_bytes` functions existence.
1 parent aa99abe commit fd4ac0e

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/libcore/hash/mod.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383

8484
use fmt;
8585
use marker;
86-
use mem;
8786

8887
#[stable(feature = "rust1", since = "1.0.0")]
8988
#[allow(deprecated)]
@@ -282,34 +281,31 @@ pub trait Hasher {
282281
#[inline]
283282
#[stable(feature = "hasher_write", since = "1.3.0")]
284283
fn write_u16(&mut self, i: u16) {
285-
self.write(&unsafe { mem::transmute::<_, [u8; 2]>(i) })
284+
self.write(&i.to_ne_bytes())
286285
}
287286
/// Writes a single `u32` into this hasher.
288287
#[inline]
289288
#[stable(feature = "hasher_write", since = "1.3.0")]
290289
fn write_u32(&mut self, i: u32) {
291-
self.write(&unsafe { mem::transmute::<_, [u8; 4]>(i) })
290+
self.write(&i.to_ne_bytes())
292291
}
293292
/// Writes a single `u64` into this hasher.
294293
#[inline]
295294
#[stable(feature = "hasher_write", since = "1.3.0")]
296295
fn write_u64(&mut self, i: u64) {
297-
self.write(&unsafe { mem::transmute::<_, [u8; 8]>(i) })
296+
self.write(&i.to_ne_bytes())
298297
}
299298
/// Writes a single `u128` into this hasher.
300299
#[inline]
301300
#[stable(feature = "i128", since = "1.26.0")]
302301
fn write_u128(&mut self, i: u128) {
303-
self.write(&unsafe { mem::transmute::<_, [u8; 16]>(i) })
302+
self.write(&i.to_ne_bytes())
304303
}
305304
/// Writes a single `usize` into this hasher.
306305
#[inline]
307306
#[stable(feature = "hasher_write", since = "1.3.0")]
308307
fn write_usize(&mut self, i: usize) {
309-
let bytes = unsafe {
310-
::slice::from_raw_parts(&i as *const usize as *const u8, mem::size_of::<usize>())
311-
};
312-
self.write(bytes);
308+
self.write(&i.to_ne_bytes())
313309
}
314310

315311
/// Writes a single `i8` into this hasher.
@@ -322,31 +318,31 @@ pub trait Hasher {
322318
#[inline]
323319
#[stable(feature = "hasher_write", since = "1.3.0")]
324320
fn write_i16(&mut self, i: i16) {
325-
self.write_u16(i as u16)
321+
self.write(&i.to_ne_bytes())
326322
}
327323
/// Writes a single `i32` into this hasher.
328324
#[inline]
329325
#[stable(feature = "hasher_write", since = "1.3.0")]
330326
fn write_i32(&mut self, i: i32) {
331-
self.write_u32(i as u32)
327+
self.write(&i.to_ne_bytes())
332328
}
333329
/// Writes a single `i64` into this hasher.
334330
#[inline]
335331
#[stable(feature = "hasher_write", since = "1.3.0")]
336332
fn write_i64(&mut self, i: i64) {
337-
self.write_u64(i as u64)
333+
self.write(&i.to_ne_bytes())
338334
}
339335
/// Writes a single `i128` into this hasher.
340336
#[inline]
341337
#[stable(feature = "i128", since = "1.26.0")]
342338
fn write_i128(&mut self, i: i128) {
343-
self.write_u128(i as u128)
339+
self.write(&i.to_ne_bytes())
344340
}
345341
/// Writes a single `isize` into this hasher.
346342
#[inline]
347343
#[stable(feature = "hasher_write", since = "1.3.0")]
348344
fn write_isize(&mut self, i: isize) {
349-
self.write_usize(i as usize)
345+
self.write(&i.to_ne_bytes())
350346
}
351347
}
352348

0 commit comments

Comments
 (0)