83
83
84
84
use fmt;
85
85
use marker;
86
- use mem;
87
86
88
87
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
89
88
#[ allow( deprecated) ]
@@ -282,34 +281,31 @@ pub trait Hasher {
282
281
#[ inline]
283
282
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
284
283
fn write_u16 ( & mut self , i : u16 ) {
285
- self . write ( & unsafe { mem :: transmute :: < _ , [ u8 ; 2 ] > ( i ) } )
284
+ self . write ( & i . to_ne_bytes ( ) )
286
285
}
287
286
/// Writes a single `u32` into this hasher.
288
287
#[ inline]
289
288
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
290
289
fn write_u32 ( & mut self , i : u32 ) {
291
- self . write ( & unsafe { mem :: transmute :: < _ , [ u8 ; 4 ] > ( i ) } )
290
+ self . write ( & i . to_ne_bytes ( ) )
292
291
}
293
292
/// Writes a single `u64` into this hasher.
294
293
#[ inline]
295
294
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
296
295
fn write_u64 ( & mut self , i : u64 ) {
297
- self . write ( & unsafe { mem :: transmute :: < _ , [ u8 ; 8 ] > ( i ) } )
296
+ self . write ( & i . to_ne_bytes ( ) )
298
297
}
299
298
/// Writes a single `u128` into this hasher.
300
299
#[ inline]
301
300
#[ stable( feature = "i128" , since = "1.26.0" ) ]
302
301
fn write_u128 ( & mut self , i : u128 ) {
303
- self . write ( & unsafe { mem :: transmute :: < _ , [ u8 ; 16 ] > ( i ) } )
302
+ self . write ( & i . to_ne_bytes ( ) )
304
303
}
305
304
/// Writes a single `usize` into this hasher.
306
305
#[ inline]
307
306
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
308
307
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 ( ) )
313
309
}
314
310
315
311
/// Writes a single `i8` into this hasher.
@@ -322,31 +318,31 @@ pub trait Hasher {
322
318
#[ inline]
323
319
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
324
320
fn write_i16 ( & mut self , i : i16 ) {
325
- self . write_u16 ( i as u16 )
321
+ self . write ( & i . to_ne_bytes ( ) )
326
322
}
327
323
/// Writes a single `i32` into this hasher.
328
324
#[ inline]
329
325
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
330
326
fn write_i32 ( & mut self , i : i32 ) {
331
- self . write_u32 ( i as u32 )
327
+ self . write ( & i . to_ne_bytes ( ) )
332
328
}
333
329
/// Writes a single `i64` into this hasher.
334
330
#[ inline]
335
331
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
336
332
fn write_i64 ( & mut self , i : i64 ) {
337
- self . write_u64 ( i as u64 )
333
+ self . write ( & i . to_ne_bytes ( ) )
338
334
}
339
335
/// Writes a single `i128` into this hasher.
340
336
#[ inline]
341
337
#[ stable( feature = "i128" , since = "1.26.0" ) ]
342
338
fn write_i128 ( & mut self , i : i128 ) {
343
- self . write_u128 ( i as u128 )
339
+ self . write ( & i . to_ne_bytes ( ) )
344
340
}
345
341
/// Writes a single `isize` into this hasher.
346
342
#[ inline]
347
343
#[ stable( feature = "hasher_write" , since = "1.3.0" ) ]
348
344
fn write_isize ( & mut self , i : isize ) {
349
- self . write_usize ( i as usize )
345
+ self . write ( & i . to_ne_bytes ( ) )
350
346
}
351
347
}
352
348
0 commit comments