]> git.proxmox.com Git - rustc.git/blob - src/libcore/hash/mod.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / libcore / hash / mod.rs
1 //! Generic hashing support.
2 //!
3 //! This module provides a generic way to compute the hash of a value. The
4 //! simplest way to make a type hashable is to use `#[derive(Hash)]`:
5 //!
6 //! # Examples
7 //!
8 //! ```rust
9 //! use std::collections::hash_map::DefaultHasher;
10 //! use std::hash::{Hash, Hasher};
11 //!
12 //! #[derive(Hash)]
13 //! struct Person {
14 //! id: u32,
15 //! name: String,
16 //! phone: u64,
17 //! }
18 //!
19 //! let person1 = Person {
20 //! id: 5,
21 //! name: "Janet".to_string(),
22 //! phone: 555_666_7777,
23 //! };
24 //! let person2 = Person {
25 //! id: 5,
26 //! name: "Bob".to_string(),
27 //! phone: 555_666_7777,
28 //! };
29 //!
30 //! assert!(calculate_hash(&person1) != calculate_hash(&person2));
31 //!
32 //! fn calculate_hash<T: Hash>(t: &T) -> u64 {
33 //! let mut s = DefaultHasher::new();
34 //! t.hash(&mut s);
35 //! s.finish()
36 //! }
37 //! ```
38 //!
39 //! If you need more control over how a value is hashed, you need to implement
40 //! the [`Hash`] trait:
41 //!
42 //! [`Hash`]: trait.Hash.html
43 //!
44 //! ```rust
45 //! use std::collections::hash_map::DefaultHasher;
46 //! use std::hash::{Hash, Hasher};
47 //!
48 //! struct Person {
49 //! id: u32,
50 //! # #[allow(dead_code)]
51 //! name: String,
52 //! phone: u64,
53 //! }
54 //!
55 //! impl Hash for Person {
56 //! fn hash<H: Hasher>(&self, state: &mut H) {
57 //! self.id.hash(state);
58 //! self.phone.hash(state);
59 //! }
60 //! }
61 //!
62 //! let person1 = Person {
63 //! id: 5,
64 //! name: "Janet".to_string(),
65 //! phone: 555_666_7777,
66 //! };
67 //! let person2 = Person {
68 //! id: 5,
69 //! name: "Bob".to_string(),
70 //! phone: 555_666_7777,
71 //! };
72 //!
73 //! assert_eq!(calculate_hash(&person1), calculate_hash(&person2));
74 //!
75 //! fn calculate_hash<T: Hash>(t: &T) -> u64 {
76 //! let mut s = DefaultHasher::new();
77 //! t.hash(&mut s);
78 //! s.finish()
79 //! }
80 //! ```
81
82 #![stable(feature = "rust1", since = "1.0.0")]
83
84 use crate::fmt;
85 use crate::marker;
86
87 #[stable(feature = "rust1", since = "1.0.0")]
88 #[allow(deprecated)]
89 pub use self::sip::SipHasher;
90
91 #[unstable(feature = "hashmap_internals", issue = "0")]
92 #[allow(deprecated)]
93 #[doc(hidden)]
94 pub use self::sip::SipHasher13;
95
96 mod sip;
97
98 /// A hashable type.
99 ///
100 /// Types implementing `Hash` are able to be [`hash`]ed with an instance of
101 /// [`Hasher`].
102 ///
103 /// ## Implementing `Hash`
104 ///
105 /// You can derive `Hash` with `#[derive(Hash)]` if all fields implement `Hash`.
106 /// The resulting hash will be the combination of the values from calling
107 /// [`hash`] on each field.
108 ///
109 /// ```
110 /// #[derive(Hash)]
111 /// struct Rustacean {
112 /// name: String,
113 /// country: String,
114 /// }
115 /// ```
116 ///
117 /// If you need more control over how a value is hashed, you can of course
118 /// implement the `Hash` trait yourself:
119 ///
120 /// ```
121 /// use std::hash::{Hash, Hasher};
122 ///
123 /// struct Person {
124 /// id: u32,
125 /// name: String,
126 /// phone: u64,
127 /// }
128 ///
129 /// impl Hash for Person {
130 /// fn hash<H: Hasher>(&self, state: &mut H) {
131 /// self.id.hash(state);
132 /// self.phone.hash(state);
133 /// }
134 /// }
135 /// ```
136 ///
137 /// ## `Hash` and `Eq`
138 ///
139 /// When implementing both `Hash` and [`Eq`], it is important that the following
140 /// property holds:
141 ///
142 /// ```text
143 /// k1 == k2 -> hash(k1) == hash(k2)
144 /// ```
145 ///
146 /// In other words, if two keys are equal, their hashes must also be equal.
147 /// [`HashMap`] and [`HashSet`] both rely on this behavior.
148 ///
149 /// Thankfully, you won't need to worry about upholding this property when
150 /// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`.
151 ///
152 /// [`Eq`]: ../../std/cmp/trait.Eq.html
153 /// [`Hasher`]: trait.Hasher.html
154 /// [`HashMap`]: ../../std/collections/struct.HashMap.html
155 /// [`HashSet`]: ../../std/collections/struct.HashSet.html
156 /// [`hash`]: #tymethod.hash
157 #[stable(feature = "rust1", since = "1.0.0")]
158 pub trait Hash {
159 /// Feeds this value into the given [`Hasher`].
160 ///
161 /// # Examples
162 ///
163 /// ```
164 /// use std::collections::hash_map::DefaultHasher;
165 /// use std::hash::{Hash, Hasher};
166 ///
167 /// let mut hasher = DefaultHasher::new();
168 /// 7920.hash(&mut hasher);
169 /// println!("Hash is {:x}!", hasher.finish());
170 /// ```
171 ///
172 /// [`Hasher`]: trait.Hasher.html
173 #[stable(feature = "rust1", since = "1.0.0")]
174 fn hash<H: Hasher>(&self, state: &mut H);
175
176 /// Feeds a slice of this type into the given [`Hasher`].
177 ///
178 /// # Examples
179 ///
180 /// ```
181 /// use std::collections::hash_map::DefaultHasher;
182 /// use std::hash::{Hash, Hasher};
183 ///
184 /// let mut hasher = DefaultHasher::new();
185 /// let numbers = [6, 28, 496, 8128];
186 /// Hash::hash_slice(&numbers, &mut hasher);
187 /// println!("Hash is {:x}!", hasher.finish());
188 /// ```
189 ///
190 /// [`Hasher`]: trait.Hasher.html
191 #[stable(feature = "hash_slice", since = "1.3.0")]
192 fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
193 where Self: Sized
194 {
195 for piece in data {
196 piece.hash(state);
197 }
198 }
199 }
200
201 // Separate module to reexport the macro `Hash` from prelude without the trait `Hash`.
202 #[cfg(not(bootstrap))]
203 pub(crate) mod macros {
204 /// Derive macro generating an impl of the trait `Hash`.
205 #[rustc_builtin_macro]
206 #[rustc_macro_transparency = "semitransparent"]
207 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
208 #[allow_internal_unstable(core_intrinsics)]
209 pub macro Hash($item:item) { /* compiler built-in */ }
210 }
211 #[cfg(not(bootstrap))]
212 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
213 #[doc(inline)]
214 pub use macros::Hash;
215
216 /// A trait for hashing an arbitrary stream of bytes.
217 ///
218 /// Instances of `Hasher` usually represent state that is changed while hashing
219 /// data.
220 ///
221 /// `Hasher` provides a fairly basic interface for retrieving the generated hash
222 /// (with [`finish`]), and writing integers as well as slices of bytes into an
223 /// instance (with [`write`] and [`write_u8`] etc.). Most of the time, `Hasher`
224 /// instances are used in conjunction with the [`Hash`] trait.
225 ///
226 /// # Examples
227 ///
228 /// ```
229 /// use std::collections::hash_map::DefaultHasher;
230 /// use std::hash::Hasher;
231 ///
232 /// let mut hasher = DefaultHasher::new();
233 ///
234 /// hasher.write_u32(1989);
235 /// hasher.write_u8(11);
236 /// hasher.write_u8(9);
237 /// hasher.write(b"Huh?");
238 ///
239 /// println!("Hash is {:x}!", hasher.finish());
240 /// ```
241 ///
242 /// [`Hash`]: trait.Hash.html
243 /// [`finish`]: #tymethod.finish
244 /// [`write`]: #tymethod.write
245 /// [`write_u8`]: #method.write_u8
246 #[stable(feature = "rust1", since = "1.0.0")]
247 pub trait Hasher {
248 /// Returns the hash value for the values written so far.
249 ///
250 /// Despite its name, the method does not reset the hasher’s internal
251 /// state. Additional [`write`]s will continue from the current value.
252 /// If you need to start a fresh hash value, you will have to create
253 /// a new hasher.
254 ///
255 /// # Examples
256 ///
257 /// ```
258 /// use std::collections::hash_map::DefaultHasher;
259 /// use std::hash::Hasher;
260 ///
261 /// let mut hasher = DefaultHasher::new();
262 /// hasher.write(b"Cool!");
263 ///
264 /// println!("Hash is {:x}!", hasher.finish());
265 /// ```
266 ///
267 /// [`write`]: #tymethod.write
268 #[stable(feature = "rust1", since = "1.0.0")]
269 fn finish(&self) -> u64;
270
271 /// Writes some data into this `Hasher`.
272 ///
273 /// # Examples
274 ///
275 /// ```
276 /// use std::collections::hash_map::DefaultHasher;
277 /// use std::hash::Hasher;
278 ///
279 /// let mut hasher = DefaultHasher::new();
280 /// let data = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
281 ///
282 /// hasher.write(&data);
283 ///
284 /// println!("Hash is {:x}!", hasher.finish());
285 /// ```
286 #[stable(feature = "rust1", since = "1.0.0")]
287 fn write(&mut self, bytes: &[u8]);
288
289 /// Writes a single `u8` into this hasher.
290 #[inline]
291 #[stable(feature = "hasher_write", since = "1.3.0")]
292 fn write_u8(&mut self, i: u8) {
293 self.write(&[i])
294 }
295 /// Writes a single `u16` into this hasher.
296 #[inline]
297 #[stable(feature = "hasher_write", since = "1.3.0")]
298 fn write_u16(&mut self, i: u16) {
299 self.write(&i.to_ne_bytes())
300 }
301 /// Writes a single `u32` into this hasher.
302 #[inline]
303 #[stable(feature = "hasher_write", since = "1.3.0")]
304 fn write_u32(&mut self, i: u32) {
305 self.write(&i.to_ne_bytes())
306 }
307 /// Writes a single `u64` into this hasher.
308 #[inline]
309 #[stable(feature = "hasher_write", since = "1.3.0")]
310 fn write_u64(&mut self, i: u64) {
311 self.write(&i.to_ne_bytes())
312 }
313 /// Writes a single `u128` into this hasher.
314 #[inline]
315 #[stable(feature = "i128", since = "1.26.0")]
316 fn write_u128(&mut self, i: u128) {
317 self.write(&i.to_ne_bytes())
318 }
319 /// Writes a single `usize` into this hasher.
320 #[inline]
321 #[stable(feature = "hasher_write", since = "1.3.0")]
322 fn write_usize(&mut self, i: usize) {
323 self.write(&i.to_ne_bytes())
324 }
325
326 /// Writes a single `i8` into this hasher.
327 #[inline]
328 #[stable(feature = "hasher_write", since = "1.3.0")]
329 fn write_i8(&mut self, i: i8) {
330 self.write_u8(i as u8)
331 }
332 /// Writes a single `i16` into this hasher.
333 #[inline]
334 #[stable(feature = "hasher_write", since = "1.3.0")]
335 fn write_i16(&mut self, i: i16) {
336 self.write(&i.to_ne_bytes())
337 }
338 /// Writes a single `i32` into this hasher.
339 #[inline]
340 #[stable(feature = "hasher_write", since = "1.3.0")]
341 fn write_i32(&mut self, i: i32) {
342 self.write(&i.to_ne_bytes())
343 }
344 /// Writes a single `i64` into this hasher.
345 #[inline]
346 #[stable(feature = "hasher_write", since = "1.3.0")]
347 fn write_i64(&mut self, i: i64) {
348 self.write(&i.to_ne_bytes())
349 }
350 /// Writes a single `i128` into this hasher.
351 #[inline]
352 #[stable(feature = "i128", since = "1.26.0")]
353 fn write_i128(&mut self, i: i128) {
354 self.write(&i.to_ne_bytes())
355 }
356 /// Writes a single `isize` into this hasher.
357 #[inline]
358 #[stable(feature = "hasher_write", since = "1.3.0")]
359 fn write_isize(&mut self, i: isize) {
360 self.write(&i.to_ne_bytes())
361 }
362 }
363
364 #[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
365 impl<H: Hasher + ?Sized> Hasher for &mut H {
366 fn finish(&self) -> u64 {
367 (**self).finish()
368 }
369 fn write(&mut self, bytes: &[u8]) {
370 (**self).write(bytes)
371 }
372 fn write_u8(&mut self, i: u8) {
373 (**self).write_u8(i)
374 }
375 fn write_u16(&mut self, i: u16) {
376 (**self).write_u16(i)
377 }
378 fn write_u32(&mut self, i: u32) {
379 (**self).write_u32(i)
380 }
381 fn write_u64(&mut self, i: u64) {
382 (**self).write_u64(i)
383 }
384 fn write_u128(&mut self, i: u128) {
385 (**self).write_u128(i)
386 }
387 fn write_usize(&mut self, i: usize) {
388 (**self).write_usize(i)
389 }
390 fn write_i8(&mut self, i: i8) {
391 (**self).write_i8(i)
392 }
393 fn write_i16(&mut self, i: i16) {
394 (**self).write_i16(i)
395 }
396 fn write_i32(&mut self, i: i32) {
397 (**self).write_i32(i)
398 }
399 fn write_i64(&mut self, i: i64) {
400 (**self).write_i64(i)
401 }
402 fn write_i128(&mut self, i: i128) {
403 (**self).write_i128(i)
404 }
405 fn write_isize(&mut self, i: isize) {
406 (**self).write_isize(i)
407 }
408 }
409
410 /// A trait for creating instances of [`Hasher`].
411 ///
412 /// A `BuildHasher` is typically used (e.g., by [`HashMap`]) to create
413 /// [`Hasher`]s for each key such that they are hashed independently of one
414 /// another, since [`Hasher`]s contain state.
415 ///
416 /// For each instance of `BuildHasher`, the [`Hasher`]s created by
417 /// [`build_hasher`] should be identical. That is, if the same stream of bytes
418 /// is fed into each hasher, the same output will also be generated.
419 ///
420 /// # Examples
421 ///
422 /// ```
423 /// use std::collections::hash_map::RandomState;
424 /// use std::hash::{BuildHasher, Hasher};
425 ///
426 /// let s = RandomState::new();
427 /// let mut hasher_1 = s.build_hasher();
428 /// let mut hasher_2 = s.build_hasher();
429 ///
430 /// hasher_1.write_u32(8128);
431 /// hasher_2.write_u32(8128);
432 ///
433 /// assert_eq!(hasher_1.finish(), hasher_2.finish());
434 /// ```
435 ///
436 /// [`build_hasher`]: #tymethod.build_hasher
437 /// [`Hasher`]: trait.Hasher.html
438 /// [`HashMap`]: ../../std/collections/struct.HashMap.html
439 #[stable(since = "1.7.0", feature = "build_hasher")]
440 pub trait BuildHasher {
441 /// Type of the hasher that will be created.
442 #[stable(since = "1.7.0", feature = "build_hasher")]
443 type Hasher: Hasher;
444
445 /// Creates a new hasher.
446 ///
447 /// Each call to `build_hasher` on the same instance should produce identical
448 /// [`Hasher`]s.
449 ///
450 /// # Examples
451 ///
452 /// ```
453 /// use std::collections::hash_map::RandomState;
454 /// use std::hash::BuildHasher;
455 ///
456 /// let s = RandomState::new();
457 /// let new_s = s.build_hasher();
458 /// ```
459 ///
460 /// [`Hasher`]: trait.Hasher.html
461 #[stable(since = "1.7.0", feature = "build_hasher")]
462 fn build_hasher(&self) -> Self::Hasher;
463 }
464
465 /// Used to create a default [`BuildHasher`] instance for types that implement
466 /// [`Hasher`] and [`Default`].
467 ///
468 /// `BuildHasherDefault<H>` can be used when a type `H` implements [`Hasher`] and
469 /// [`Default`], and you need a corresponding [`BuildHasher`] instance, but none is
470 /// defined.
471 ///
472 /// Any `BuildHasherDefault` is [zero-sized]. It can be created with
473 /// [`default`][method.Default]. When using `BuildHasherDefault` with [`HashMap`] or
474 /// [`HashSet`], this doesn't need to be done, since they implement appropriate
475 /// [`Default`] instances themselves.
476 ///
477 /// # Examples
478 ///
479 /// Using `BuildHasherDefault` to specify a custom [`BuildHasher`] for
480 /// [`HashMap`]:
481 ///
482 /// ```
483 /// use std::collections::HashMap;
484 /// use std::hash::{BuildHasherDefault, Hasher};
485 ///
486 /// #[derive(Default)]
487 /// struct MyHasher;
488 ///
489 /// impl Hasher for MyHasher {
490 /// fn write(&mut self, bytes: &[u8]) {
491 /// // Your hashing algorithm goes here!
492 /// unimplemented!()
493 /// }
494 ///
495 /// fn finish(&self) -> u64 {
496 /// // Your hashing algorithm goes here!
497 /// unimplemented!()
498 /// }
499 /// }
500 ///
501 /// type MyBuildHasher = BuildHasherDefault<MyHasher>;
502 ///
503 /// let hash_map = HashMap::<u32, u32, MyBuildHasher>::default();
504 /// ```
505 ///
506 /// [`BuildHasher`]: trait.BuildHasher.html
507 /// [`Default`]: ../default/trait.Default.html
508 /// [method.default]: #method.default
509 /// [`Hasher`]: trait.Hasher.html
510 /// [`HashMap`]: ../../std/collections/struct.HashMap.html
511 /// [`HashSet`]: ../../std/collections/struct.HashSet.html
512 /// [zero-sized]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts
513 #[stable(since = "1.7.0", feature = "build_hasher")]
514 pub struct BuildHasherDefault<H>(marker::PhantomData<H>);
515
516 #[stable(since = "1.9.0", feature = "core_impl_debug")]
517 impl<H> fmt::Debug for BuildHasherDefault<H> {
518 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
519 f.pad("BuildHasherDefault")
520 }
521 }
522
523 #[stable(since = "1.7.0", feature = "build_hasher")]
524 impl<H: Default + Hasher> BuildHasher for BuildHasherDefault<H> {
525 type Hasher = H;
526
527 fn build_hasher(&self) -> H {
528 H::default()
529 }
530 }
531
532 #[stable(since = "1.7.0", feature = "build_hasher")]
533 impl<H> Clone for BuildHasherDefault<H> {
534 fn clone(&self) -> BuildHasherDefault<H> {
535 BuildHasherDefault(marker::PhantomData)
536 }
537 }
538
539 #[stable(since = "1.7.0", feature = "build_hasher")]
540 impl<H> Default for BuildHasherDefault<H> {
541 fn default() -> BuildHasherDefault<H> {
542 BuildHasherDefault(marker::PhantomData)
543 }
544 }
545
546 #[stable(since = "1.29.0", feature = "build_hasher_eq")]
547 impl<H> PartialEq for BuildHasherDefault<H> {
548 fn eq(&self, _other: &BuildHasherDefault<H>) -> bool {
549 true
550 }
551 }
552
553 #[stable(since = "1.29.0", feature = "build_hasher_eq")]
554 impl<H> Eq for BuildHasherDefault<H> {}
555
556 //////////////////////////////////////////////////////////////////////////////
557
558 mod impls {
559 use crate::mem;
560 use crate::slice;
561
562 use super::*;
563
564 macro_rules! impl_write {
565 ($(($ty:ident, $meth:ident),)*) => {$(
566 #[stable(feature = "rust1", since = "1.0.0")]
567 impl Hash for $ty {
568 fn hash<H: Hasher>(&self, state: &mut H) {
569 state.$meth(*self)
570 }
571
572 fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
573 let newlen = data.len() * mem::size_of::<$ty>();
574 let ptr = data.as_ptr() as *const u8;
575 state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
576 }
577 }
578 )*}
579 }
580
581 impl_write! {
582 (u8, write_u8),
583 (u16, write_u16),
584 (u32, write_u32),
585 (u64, write_u64),
586 (usize, write_usize),
587 (i8, write_i8),
588 (i16, write_i16),
589 (i32, write_i32),
590 (i64, write_i64),
591 (isize, write_isize),
592 (u128, write_u128),
593 (i128, write_i128),
594 }
595
596 #[stable(feature = "rust1", since = "1.0.0")]
597 impl Hash for bool {
598 fn hash<H: Hasher>(&self, state: &mut H) {
599 state.write_u8(*self as u8)
600 }
601 }
602
603 #[stable(feature = "rust1", since = "1.0.0")]
604 impl Hash for char {
605 fn hash<H: Hasher>(&self, state: &mut H) {
606 state.write_u32(*self as u32)
607 }
608 }
609
610 #[stable(feature = "rust1", since = "1.0.0")]
611 impl Hash for str {
612 fn hash<H: Hasher>(&self, state: &mut H) {
613 state.write(self.as_bytes());
614 state.write_u8(0xff)
615 }
616 }
617
618 #[stable(feature = "never_hash", since = "1.29.0")]
619 impl Hash for ! {
620 fn hash<H: Hasher>(&self, _: &mut H) {
621 *self
622 }
623 }
624
625 macro_rules! impl_hash_tuple {
626 () => (
627 #[stable(feature = "rust1", since = "1.0.0")]
628 impl Hash for () {
629 fn hash<H: Hasher>(&self, _state: &mut H) {}
630 }
631 );
632
633 ( $($name:ident)+) => (
634 #[stable(feature = "rust1", since = "1.0.0")]
635 impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
636 #[allow(non_snake_case)]
637 fn hash<S: Hasher>(&self, state: &mut S) {
638 let ($(ref $name,)+) = *self;
639 $($name.hash(state);)+
640 }
641 }
642 );
643 }
644
645 macro_rules! last_type {
646 ($a:ident,) => { $a };
647 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
648 }
649
650 impl_hash_tuple! {}
651 impl_hash_tuple! { A }
652 impl_hash_tuple! { A B }
653 impl_hash_tuple! { A B C }
654 impl_hash_tuple! { A B C D }
655 impl_hash_tuple! { A B C D E }
656 impl_hash_tuple! { A B C D E F }
657 impl_hash_tuple! { A B C D E F G }
658 impl_hash_tuple! { A B C D E F G H }
659 impl_hash_tuple! { A B C D E F G H I }
660 impl_hash_tuple! { A B C D E F G H I J }
661 impl_hash_tuple! { A B C D E F G H I J K }
662 impl_hash_tuple! { A B C D E F G H I J K L }
663
664 #[stable(feature = "rust1", since = "1.0.0")]
665 impl<T: Hash> Hash for [T] {
666 fn hash<H: Hasher>(&self, state: &mut H) {
667 self.len().hash(state);
668 Hash::hash_slice(self, state)
669 }
670 }
671
672
673 #[stable(feature = "rust1", since = "1.0.0")]
674 impl<T: ?Sized + Hash> Hash for &T {
675 fn hash<H: Hasher>(&self, state: &mut H) {
676 (**self).hash(state);
677 }
678 }
679
680 #[stable(feature = "rust1", since = "1.0.0")]
681 impl<T: ?Sized + Hash> Hash for &mut T {
682 fn hash<H: Hasher>(&self, state: &mut H) {
683 (**self).hash(state);
684 }
685 }
686
687 #[stable(feature = "rust1", since = "1.0.0")]
688 impl<T: ?Sized> Hash for *const T {
689 fn hash<H: Hasher>(&self, state: &mut H) {
690 if mem::size_of::<Self>() == mem::size_of::<usize>() {
691 // Thin pointer
692 state.write_usize(*self as *const () as usize);
693 } else {
694 // Fat pointer
695 let (a, b) = unsafe {
696 *(self as *const Self as *const (usize, usize))
697 };
698 state.write_usize(a);
699 state.write_usize(b);
700 }
701 }
702 }
703
704 #[stable(feature = "rust1", since = "1.0.0")]
705 impl<T: ?Sized> Hash for *mut T {
706 fn hash<H: Hasher>(&self, state: &mut H) {
707 if mem::size_of::<Self>() == mem::size_of::<usize>() {
708 // Thin pointer
709 state.write_usize(*self as *const () as usize);
710 } else {
711 // Fat pointer
712 let (a, b) = unsafe {
713 *(self as *const Self as *const (usize, usize))
714 };
715 state.write_usize(a);
716 state.write_usize(b);
717 }
718 }
719 }
720 }