Commit c47f2ed
committed
std: Stabilize the std::hash module
This commit aims to stabilize the `std::hash` module by standardizing on its
hashing interface while rationalizing the current usage with the `HashMap` and
`HashSet` types. The primary goal of this slight redesign is to separate the
concepts of a hasher's state from a hashing algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher: Writer {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
Note that the `Output` associated type is currently a type parameter due to
associated types not being fully implemented yet. This new `Hasher` trait
emphasizes that all hashers are sinks for bytes, and hashing algorithms may
produce outputs other than a `u64`, so a the output type is made generic.
With this definition, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an experimental addition for
now. The current definition looks like:
trait HashState {
type H: Hasher;
fn hasher(&self) -> H;
}
Note that the `H` associated type (along with its `O` output) are both type
parameters on the `HashState` trait due to the current limitations of associated
types. The purpose of this trait is to emphasize that the one piece of
functionality for implementors is that new instances of `Hasher` can be created.
This conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a `HashMap`,
not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
Some other stability decision made for the `std::hash` module are:
* The name of the module, hash, is `#![stable]`
* The `Hash` and `Hasher` traits are `#[unstable]` due to type parameters that
want to be associated types.
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]1 parent a592124 commit c47f2ed
File tree
23 files changed
+503
-449
lines changed- src
- libcollections
- hash
- libflate
- librustc_back
- librustc_llvm
- librustc_trans/trans
- librustc
- middle
- util
- libserialize
- libstd
- collections
- hash
- libsyntax
- ext/deriving
23 files changed
+503
-449
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
72 | | - | |
73 | 74 | | |
74 | 75 | | |
75 | | - | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | | - | |
| 78 | + | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
| 83 | + | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | | - | |
91 | | - | |
92 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
93 | 102 | | |
94 | 103 | | |
| 104 | + | |
95 | 105 | | |
96 | 106 | | |
97 | 107 | | |
98 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
99 | 120 | | |
100 | 121 | | |
101 | 122 | | |
| |||
283 | 304 | | |
284 | 305 | | |
285 | 306 | | |
286 | | - | |
| 307 | + | |
| 308 | + | |
287 | 309 | | |
288 | 310 | | |
289 | 311 | | |
| |||
300 | 322 | | |
301 | 323 | | |
302 | 324 | | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | 325 | | |
314 | 326 | | |
315 | 327 | | |
| |||
323 | 335 | | |
324 | 336 | | |
325 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
326 | 343 | | |
327 | 344 | | |
328 | 345 | | |
329 | 346 | | |
330 | | - | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
331 | 352 | | |
332 | | - | |
| 353 | + | |
333 | 354 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
339 | 360 | | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
345 | 366 | | |
346 | | - | |
347 | | - | |
| 367 | + | |
| 368 | + | |
348 | 369 | | |
349 | | - | |
| 370 | + | |
350 | 371 | | |
351 | 372 | | |
352 | | - | |
| 373 | + | |
353 | 374 | | |
354 | 375 | | |
355 | 376 | | |
356 | 377 | | |
357 | | - | |
| 378 | + | |
358 | 379 | | |
359 | | - | |
| 380 | + | |
360 | 381 | | |
361 | 382 | | |
362 | 383 | | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
| 384 | + | |
| 385 | + | |
367 | 386 | | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
| 387 | + | |
| 388 | + | |
372 | 389 | | |
373 | 390 | | |
374 | 391 | | |
375 | 392 | | |
376 | 393 | | |
377 | 394 | | |
378 | | - | |
379 | | - | |
380 | | - | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
381 | 404 | | |
382 | 405 | | |
383 | 406 | | |
384 | 407 | | |
385 | 408 | | |
386 | 409 | | |
387 | | - | |
| 410 | + | |
388 | 411 | | |
389 | | - | |
| 412 | + | |
390 | 413 | | |
391 | 414 | | |
0 commit comments