@@ -68,6 +68,7 @@ use core::prelude::*;
6868use alloc:: owned:: Box ;
6969use alloc:: rc:: Rc ;
7070use core:: intrinsics:: TypeId ;
71+ use core:: mem;
7172
7273use vec:: Vec ;
7374
@@ -97,14 +98,16 @@ pub trait Writer {
9798
9899//////////////////////////////////////////////////////////////////////////////
99100
101+ fn id < T > ( t : T ) -> T { t }
102+
100103macro_rules! impl_hash(
101- ( $( $ty: ident) * ) => (
104+ ( $( $ty: ident, $uty : ident , $f : path ; ) * ) => (
102105 $(
103106 impl <S : Writer > Hash <S > for $ty {
104107 #[ inline]
105108 fn hash( & self , state: & mut S ) {
106109 let a: [ u8 , ..:: core:: $ty:: BYTES ] = unsafe {
107- :: core :: mem:: transmute( * self )
110+ mem:: transmute( $f ( * self as $uty ) as $ty )
108111 } ;
109112 state. write( a. as_slice( ) )
110113 }
@@ -113,7 +116,28 @@ macro_rules! impl_hash(
113116 )
114117)
115118
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+ )
117141
118142impl < S : Writer > Hash < S > for bool {
119143 #[ inline]
@@ -292,14 +316,11 @@ impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
292316
293317#[ cfg( test) ]
294318mod 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;
301321
302- use super :: { Hash , Hasher } ;
322+ use slice:: ImmutableVector ;
323+ use super :: { Hash , Hasher , Writer } ;
303324
304325 struct MyWriterHasher ;
305326
@@ -317,11 +338,10 @@ mod tests {
317338
318339 impl Writer for MyWriter {
319340 // Most things we'll just add up the bytes.
320- fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > {
341+ fn write ( & mut self , buf : & [ u8 ] ) {
321342 for byte in buf. iter ( ) {
322343 self . hash += * byte as u64 ;
323344 }
324- Ok ( ( ) )
325345 }
326346 }
327347
0 commit comments