@@ -68,6 +68,7 @@ use core::prelude::*;
68
68
use alloc:: owned:: Box ;
69
69
use alloc:: rc:: Rc ;
70
70
use core:: intrinsics:: TypeId ;
71
+ use core:: mem;
71
72
72
73
use vec:: Vec ;
73
74
@@ -97,14 +98,16 @@ pub trait Writer {
97
98
98
99
//////////////////////////////////////////////////////////////////////////////
99
100
101
+ fn id < T > ( t : T ) -> T { t }
102
+
100
103
macro_rules! impl_hash(
101
- ( $( $ty: ident) * ) => (
104
+ ( $( $ty: ident, $uty : ident , $f : path ; ) * ) => (
102
105
$(
103
106
impl <S : Writer > Hash <S > for $ty {
104
107
#[ inline]
105
108
fn hash( & self , state: & mut S ) {
106
109
let a: [ u8 , ..:: core:: $ty:: BYTES ] = unsafe {
107
- :: core :: mem:: transmute( * self )
110
+ mem:: transmute( $f ( * self as $uty ) as $ty )
108
111
} ;
109
112
state. write( a. as_slice( ) )
110
113
}
@@ -113,7 +116,28 @@ macro_rules! impl_hash(
113
116
)
114
117
)
115
118
116
- impl_hash ! ( u8 u16 u32 u64 uint i8 i16 i32 i64 int )
119
+ impl_hash ! (
120
+ u8 , u8 , id;
121
+ u16 , u16 , mem:: to_le16;
122
+ u32 , u32 , mem:: to_le32;
123
+ u64 , u64 , mem:: to_le64;
124
+ i8 , u8 , id;
125
+ i16 , u16 , mem:: to_le16;
126
+ i32 , u32 , mem:: to_le32;
127
+ i64 , u64 , mem:: to_le64;
128
+ )
129
+
130
+ #[ cfg( target_word_size = "32" ) ]
131
+ impl_hash ! (
132
+ uint, u32 , mem:: to_le32;
133
+ int, u32 , mem:: to_le32;
134
+ )
135
+
136
+ #[ cfg( target_word_size = "64" ) ]
137
+ impl_hash ! (
138
+ uint, u64 , mem:: to_le64;
139
+ int, u64 , mem:: to_le64;
140
+ )
117
141
118
142
impl < S : Writer > Hash < S > for bool {
119
143
#[ inline]
@@ -292,14 +316,11 @@ impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
292
316
293
317
#[ cfg( test) ]
294
318
mod tests {
295
- use mem;
296
- use io:: { IoResult , Writer } ;
297
- use iter:: { Iterator } ;
298
- use option:: { Some , None } ;
299
- use result:: Ok ;
300
- use slice:: ImmutableVector ;
319
+ use std:: prelude:: * ;
320
+ use std:: mem;
301
321
302
- use super :: { Hash , Hasher } ;
322
+ use slice:: ImmutableVector ;
323
+ use super :: { Hash , Hasher , Writer } ;
303
324
304
325
struct MyWriterHasher ;
305
326
@@ -317,11 +338,10 @@ mod tests {
317
338
318
339
impl Writer for MyWriter {
319
340
// Most things we'll just add up the bytes.
320
- fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > {
341
+ fn write ( & mut self , buf : & [ u8 ] ) {
321
342
for byte in buf. iter ( ) {
322
343
self . hash += * byte as u64 ;
323
344
}
324
- Ok ( ( ) )
325
345
}
326
346
}
327
347
0 commit comments