61
61
62
62
#![ allow( unused_must_use) ]
63
63
64
- use core :: prelude:: * ;
64
+ use prelude:: * ;
65
65
66
- use alloc:: boxed:: Box ;
67
- use alloc:: rc:: Rc ;
68
- use core:: borrow:: { Cow , ToOwned } ;
69
- use core:: intrinsics:: TypeId ;
70
- use core:: mem;
71
- use core:: num:: Int ;
72
-
73
- use vec:: Vec ;
66
+ use borrow:: { Cow , ToOwned } ;
67
+ use intrinsics:: TypeId ;
68
+ use mem;
69
+ use num:: Int ;
74
70
75
71
/// Reexport the `sip::hash` function as our default hasher.
76
72
pub use self :: sip:: hash as hash;
@@ -92,6 +88,7 @@ pub trait Hasher<S> {
92
88
fn hash < Sized ? T : Hash < S > > ( & self , value : & T ) -> u64 ;
93
89
}
94
90
91
+ #[ allow( missing_docs) ]
95
92
pub trait Writer {
96
93
fn write ( & mut self , bytes : & [ u8 ] ) ;
97
94
}
@@ -103,7 +100,7 @@ macro_rules! impl_hash {
103
100
impl <S : Writer > Hash <S > for $ty {
104
101
#[ inline]
105
102
fn hash( & self , state: & mut S ) {
106
- let a: [ u8 , ..:: core :: $ty:: BYTES ] = unsafe {
103
+ let a: [ u8 , ..:: $ty:: BYTES ] = unsafe {
107
104
mem:: transmute( ( * self as $uty) . to_le( ) as $ty)
108
105
} ;
109
106
state. write( a. as_slice( ) )
@@ -197,13 +194,6 @@ impl<S: Writer, T: Hash<S>> Hash<S> for [T] {
197
194
}
198
195
199
196
200
- impl < S : Writer , T : Hash < S > > Hash < S > for Vec < T > {
201
- #[ inline]
202
- fn hash ( & self , state : & mut S ) {
203
- self . as_slice ( ) . hash ( state) ;
204
- }
205
- }
206
-
207
197
impl < ' a , S : Writer , Sized ? T : Hash < S > > Hash < S > for & ' a T {
208
198
#[ inline]
209
199
fn hash ( & self , state : & mut S ) {
@@ -218,36 +208,6 @@ impl<'a, S: Writer, Sized? T: Hash<S>> Hash<S> for &'a mut T {
218
208
}
219
209
}
220
210
221
- impl < S : Writer , Sized ? T : Hash < S > > Hash < S > for Box < T > {
222
- #[ inline]
223
- fn hash ( & self , state : & mut S ) {
224
- ( * * self ) . hash ( state) ;
225
- }
226
- }
227
-
228
- // FIXME (#18248) Make `T` `Sized?`
229
- impl < S : Writer , T : Hash < S > > Hash < S > for Rc < T > {
230
- #[ inline]
231
- fn hash ( & self , state : & mut S ) {
232
- ( * * self ) . hash ( state) ;
233
- }
234
- }
235
-
236
- impl < S : Writer , T : Hash < S > > Hash < S > for Option < T > {
237
- #[ inline]
238
- fn hash ( & self , state : & mut S ) {
239
- match * self {
240
- Some ( ref x) => {
241
- 0u8 . hash ( state) ;
242
- x. hash ( state) ;
243
- }
244
- None => {
245
- 1u8 . hash ( state) ;
246
- }
247
- }
248
- }
249
- }
250
-
251
211
impl < S : Writer , T > Hash < S > for * const T {
252
212
#[ inline]
253
213
fn hash ( & self , state : & mut S ) {
@@ -273,119 +233,9 @@ impl<S: Writer> Hash<S> for TypeId {
273
233
}
274
234
}
275
235
276
- impl < S : Writer , T : Hash < S > , U : Hash < S > > Hash < S > for Result < T , U > {
277
- #[ inline]
278
- fn hash ( & self , state : & mut S ) {
279
- match * self {
280
- Ok ( ref t) => { 1 u. hash ( state) ; t. hash ( state) ; }
281
- Err ( ref t) => { 2 u. hash ( state) ; t. hash ( state) ; }
282
- }
283
- }
284
- }
285
-
286
236
impl < ' a , T , Sized ? B , S > Hash < S > for Cow < ' a , T , B > where B : Hash < S > + ToOwned < T > {
287
237
#[ inline]
288
238
fn hash ( & self , state : & mut S ) {
289
239
Hash :: hash ( & * * self , state)
290
240
}
291
241
}
292
-
293
- //////////////////////////////////////////////////////////////////////////////
294
-
295
- #[ cfg( test) ]
296
- mod tests {
297
- use core:: kinds:: Sized ;
298
- use std:: mem;
299
-
300
- use slice:: SliceExt ;
301
- use super :: { Hash , Hasher , Writer } ;
302
-
303
- struct MyWriterHasher ;
304
-
305
- impl Hasher < MyWriter > for MyWriterHasher {
306
- fn hash < Sized ? T : Hash < MyWriter > > ( & self , value : & T ) -> u64 {
307
- let mut state = MyWriter { hash : 0 } ;
308
- value. hash ( & mut state) ;
309
- state. hash
310
- }
311
- }
312
-
313
- struct MyWriter {
314
- hash : u64 ,
315
- }
316
-
317
- impl Writer for MyWriter {
318
- // Most things we'll just add up the bytes.
319
- fn write ( & mut self , buf : & [ u8 ] ) {
320
- for byte in buf. iter ( ) {
321
- self . hash += * byte as u64 ;
322
- }
323
- }
324
- }
325
-
326
- #[ test]
327
- fn test_writer_hasher ( ) {
328
- use alloc:: boxed:: Box ;
329
-
330
- let hasher = MyWriterHasher ;
331
-
332
- assert_eq ! ( hasher. hash( & ( ) ) , 0 ) ;
333
-
334
- assert_eq ! ( hasher. hash( & 5u8 ) , 5 ) ;
335
- assert_eq ! ( hasher. hash( & 5u16 ) , 5 ) ;
336
- assert_eq ! ( hasher. hash( & 5u32 ) , 5 ) ;
337
- assert_eq ! ( hasher. hash( & 5u64 ) , 5 ) ;
338
- assert_eq ! ( hasher. hash( & 5 u) , 5 ) ;
339
-
340
- assert_eq ! ( hasher. hash( & 5i8 ) , 5 ) ;
341
- assert_eq ! ( hasher. hash( & 5i16 ) , 5 ) ;
342
- assert_eq ! ( hasher. hash( & 5i32 ) , 5 ) ;
343
- assert_eq ! ( hasher. hash( & 5i64 ) , 5 ) ;
344
- assert_eq ! ( hasher. hash( & 5 i) , 5 ) ;
345
-
346
- assert_eq ! ( hasher. hash( & false ) , 0 ) ;
347
- assert_eq ! ( hasher. hash( & true ) , 1 ) ;
348
-
349
- assert_eq ! ( hasher. hash( & 'a' ) , 97 ) ;
350
-
351
- let s: & str = "a" ;
352
- assert_eq ! ( hasher. hash( & s) , 97 + 0xFF ) ;
353
- // FIXME (#18283) Enable test
354
- //let s: Box<str> = box "a";
355
- //assert_eq!(hasher.hash(& s), 97 + 0xFF);
356
- let cs: & [ u8 ] = & [ 1u8 , 2u8 , 3u8 ] ;
357
- assert_eq ! ( hasher. hash( & cs) , 9 ) ;
358
- let cs: Box < [ u8 ] > = box [ 1u8 , 2u8 , 3u8 ] ;
359
- assert_eq ! ( hasher. hash( & cs) , 9 ) ;
360
-
361
- // FIXME (#18248) Add tests for hashing Rc<str> and Rc<[T]>
362
-
363
- unsafe {
364
- let ptr: * const int = mem:: transmute ( 5 i) ;
365
- assert_eq ! ( hasher. hash( & ptr) , 5 ) ;
366
- }
367
-
368
- unsafe {
369
- let ptr: * mut int = mem:: transmute ( 5 i) ;
370
- assert_eq ! ( hasher. hash( & ptr) , 5 ) ;
371
- }
372
- }
373
-
374
- struct Custom {
375
- hash : u64
376
- }
377
-
378
- impl Hash < u64 > for Custom {
379
- fn hash ( & self , state : & mut u64 ) {
380
- * state = self . hash ;
381
- }
382
- }
383
-
384
- #[ test]
385
- fn test_custom_state ( ) {
386
- let custom = Custom { hash : 5 } ;
387
- let mut state = 0 ;
388
- custom. hash ( & mut state) ;
389
- assert_eq ! ( state, 5 ) ;
390
- }
391
- }
0 commit comments