]> git.lizzy.rs Git - rust.git/blob - src/libcore/hash/mod.rs
Auto merge of #62069 - Centril:rollup-m8n4uw7, r=Centril
[rust.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 /// A trait for hashing an arbitrary stream of bytes.
202 ///
203 /// Instances of `Hasher` usually represent state that is changed while hashing
204 /// data.
205 ///
206 /// `Hasher` provides a fairly basic interface for retrieving the generated hash
207 /// (with [`finish`]), and writing integers as well as slices of bytes into an
208 /// instance (with [`write`] and [`write_u8`] etc.). Most of the time, `Hasher`
209 /// instances are used in conjunction with the [`Hash`] trait.
210 ///
211 /// # Examples
212 ///
213 /// ```
214 /// use std::collections::hash_map::DefaultHasher;
215 /// use std::hash::Hasher;
216 ///
217 /// let mut hasher = DefaultHasher::new();
218 ///
219 /// hasher.write_u32(1989);
220 /// hasher.write_u8(11);
221 /// hasher.write_u8(9);
222 /// hasher.write(b"Huh?");
223 ///
224 /// println!("Hash is {:x}!", hasher.finish());
225 /// ```
226 ///
227 /// [`Hash`]: trait.Hash.html
228 /// [`finish`]: #tymethod.finish
229 /// [`write`]: #tymethod.write
230 /// [`write_u8`]: #method.write_u8
231 #[stable(feature = "rust1", since = "1.0.0")]
232 pub trait Hasher {
233     /// Returns the hash value for the values written so far.
234     ///
235     /// Despite its name, the method does not reset the hasher’s internal
236     /// state. Additional [`write`]s will continue from the current value.
237     /// If you need to start a fresh hash value, you will have to create
238     /// a new hasher.
239     ///
240     /// # Examples
241     ///
242     /// ```
243     /// use std::collections::hash_map::DefaultHasher;
244     /// use std::hash::Hasher;
245     ///
246     /// let mut hasher = DefaultHasher::new();
247     /// hasher.write(b"Cool!");
248     ///
249     /// println!("Hash is {:x}!", hasher.finish());
250     /// ```
251     ///
252     /// [`write`]: #tymethod.write
253     #[stable(feature = "rust1", since = "1.0.0")]
254     fn finish(&self) -> u64;
255
256     /// Writes some data into this `Hasher`.
257     ///
258     /// # Examples
259     ///
260     /// ```
261     /// use std::collections::hash_map::DefaultHasher;
262     /// use std::hash::Hasher;
263     ///
264     /// let mut hasher = DefaultHasher::new();
265     /// let data = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
266     ///
267     /// hasher.write(&data);
268     ///
269     /// println!("Hash is {:x}!", hasher.finish());
270     /// ```
271     #[stable(feature = "rust1", since = "1.0.0")]
272     fn write(&mut self, bytes: &[u8]);
273
274     /// Writes a single `u8` into this hasher.
275     #[inline]
276     #[stable(feature = "hasher_write", since = "1.3.0")]
277     fn write_u8(&mut self, i: u8) {
278         self.write(&[i])
279     }
280     /// Writes a single `u16` into this hasher.
281     #[inline]
282     #[stable(feature = "hasher_write", since = "1.3.0")]
283     fn write_u16(&mut self, i: u16) {
284         self.write(&i.to_ne_bytes())
285     }
286     /// Writes a single `u32` into this hasher.
287     #[inline]
288     #[stable(feature = "hasher_write", since = "1.3.0")]
289     fn write_u32(&mut self, i: u32) {
290         self.write(&i.to_ne_bytes())
291     }
292     /// Writes a single `u64` into this hasher.
293     #[inline]
294     #[stable(feature = "hasher_write", since = "1.3.0")]
295     fn write_u64(&mut self, i: u64) {
296         self.write(&i.to_ne_bytes())
297     }
298     /// Writes a single `u128` into this hasher.
299     #[inline]
300     #[stable(feature = "i128", since = "1.26.0")]
301     fn write_u128(&mut self, i: u128) {
302         self.write(&i.to_ne_bytes())
303     }
304     /// Writes a single `usize` into this hasher.
305     #[inline]
306     #[stable(feature = "hasher_write", since = "1.3.0")]
307     fn write_usize(&mut self, i: usize) {
308         self.write(&i.to_ne_bytes())
309     }
310
311     /// Writes a single `i8` into this hasher.
312     #[inline]
313     #[stable(feature = "hasher_write", since = "1.3.0")]
314     fn write_i8(&mut self, i: i8) {
315         self.write_u8(i as u8)
316     }
317     /// Writes a single `i16` into this hasher.
318     #[inline]
319     #[stable(feature = "hasher_write", since = "1.3.0")]
320     fn write_i16(&mut self, i: i16) {
321         self.write(&i.to_ne_bytes())
322     }
323     /// Writes a single `i32` into this hasher.
324     #[inline]
325     #[stable(feature = "hasher_write", since = "1.3.0")]
326     fn write_i32(&mut self, i: i32) {
327         self.write(&i.to_ne_bytes())
328     }
329     /// Writes a single `i64` into this hasher.
330     #[inline]
331     #[stable(feature = "hasher_write", since = "1.3.0")]
332     fn write_i64(&mut self, i: i64) {
333         self.write(&i.to_ne_bytes())
334     }
335     /// Writes a single `i128` into this hasher.
336     #[inline]
337     #[stable(feature = "i128", since = "1.26.0")]
338     fn write_i128(&mut self, i: i128) {
339         self.write(&i.to_ne_bytes())
340     }
341     /// Writes a single `isize` into this hasher.
342     #[inline]
343     #[stable(feature = "hasher_write", since = "1.3.0")]
344     fn write_isize(&mut self, i: isize) {
345         self.write(&i.to_ne_bytes())
346     }
347 }
348
349 #[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
350 impl<H: Hasher + ?Sized> Hasher for &mut H {
351     fn finish(&self) -> u64 {
352         (**self).finish()
353     }
354     fn write(&mut self, bytes: &[u8]) {
355         (**self).write(bytes)
356     }
357     fn write_u8(&mut self, i: u8) {
358         (**self).write_u8(i)
359     }
360     fn write_u16(&mut self, i: u16) {
361         (**self).write_u16(i)
362     }
363     fn write_u32(&mut self, i: u32) {
364         (**self).write_u32(i)
365     }
366     fn write_u64(&mut self, i: u64) {
367         (**self).write_u64(i)
368     }
369     fn write_u128(&mut self, i: u128) {
370         (**self).write_u128(i)
371     }
372     fn write_usize(&mut self, i: usize) {
373         (**self).write_usize(i)
374     }
375     fn write_i8(&mut self, i: i8) {
376         (**self).write_i8(i)
377     }
378     fn write_i16(&mut self, i: i16) {
379         (**self).write_i16(i)
380     }
381     fn write_i32(&mut self, i: i32) {
382         (**self).write_i32(i)
383     }
384     fn write_i64(&mut self, i: i64) {
385         (**self).write_i64(i)
386     }
387     fn write_i128(&mut self, i: i128) {
388         (**self).write_i128(i)
389     }
390     fn write_isize(&mut self, i: isize) {
391         (**self).write_isize(i)
392     }
393 }
394
395 /// A trait for creating instances of [`Hasher`].
396 ///
397 /// A `BuildHasher` is typically used (e.g., by [`HashMap`]) to create
398 /// [`Hasher`]s for each key such that they are hashed independently of one
399 /// another, since [`Hasher`]s contain state.
400 ///
401 /// For each instance of `BuildHasher`, the [`Hasher`]s created by
402 /// [`build_hasher`] should be identical. That is, if the same stream of bytes
403 /// is fed into each hasher, the same output will also be generated.
404 ///
405 /// # Examples
406 ///
407 /// ```
408 /// use std::collections::hash_map::RandomState;
409 /// use std::hash::{BuildHasher, Hasher};
410 ///
411 /// let s = RandomState::new();
412 /// let mut hasher_1 = s.build_hasher();
413 /// let mut hasher_2 = s.build_hasher();
414 ///
415 /// hasher_1.write_u32(8128);
416 /// hasher_2.write_u32(8128);
417 ///
418 /// assert_eq!(hasher_1.finish(), hasher_2.finish());
419 /// ```
420 ///
421 /// [`build_hasher`]: #tymethod.build_hasher
422 /// [`Hasher`]: trait.Hasher.html
423 /// [`HashMap`]: ../../std/collections/struct.HashMap.html
424 #[stable(since = "1.7.0", feature = "build_hasher")]
425 pub trait BuildHasher {
426     /// Type of the hasher that will be created.
427     #[stable(since = "1.7.0", feature = "build_hasher")]
428     type Hasher: Hasher;
429
430     /// Creates a new hasher.
431     ///
432     /// Each call to `build_hasher` on the same instance should produce identical
433     /// [`Hasher`]s.
434     ///
435     /// # Examples
436     ///
437     /// ```
438     /// use std::collections::hash_map::RandomState;
439     /// use std::hash::BuildHasher;
440     ///
441     /// let s = RandomState::new();
442     /// let new_s = s.build_hasher();
443     /// ```
444     ///
445     /// [`Hasher`]: trait.Hasher.html
446     #[stable(since = "1.7.0", feature = "build_hasher")]
447     fn build_hasher(&self) -> Self::Hasher;
448 }
449
450 /// Used to create a default [`BuildHasher`] instance for types that implement
451 /// [`Hasher`] and [`Default`].
452 ///
453 /// `BuildHasherDefault<H>` can be used when a type `H` implements [`Hasher`] and
454 /// [`Default`], and you need a corresponding [`BuildHasher`] instance, but none is
455 /// defined.
456 ///
457 /// Any `BuildHasherDefault` is [zero-sized]. It can be created with
458 /// [`default`][method.Default]. When using `BuildHasherDefault` with [`HashMap`] or
459 /// [`HashSet`], this doesn't need to be done, since they implement appropriate
460 /// [`Default`] instances themselves.
461 ///
462 /// # Examples
463 ///
464 /// Using `BuildHasherDefault` to specify a custom [`BuildHasher`] for
465 /// [`HashMap`]:
466 ///
467 /// ```
468 /// use std::collections::HashMap;
469 /// use std::hash::{BuildHasherDefault, Hasher};
470 ///
471 /// #[derive(Default)]
472 /// struct MyHasher;
473 ///
474 /// impl Hasher for MyHasher {
475 ///     fn write(&mut self, bytes: &[u8]) {
476 ///         // Your hashing algorithm goes here!
477 ///        unimplemented!()
478 ///     }
479 ///
480 ///     fn finish(&self) -> u64 {
481 ///         // Your hashing algorithm goes here!
482 ///         unimplemented!()
483 ///     }
484 /// }
485 ///
486 /// type MyBuildHasher = BuildHasherDefault<MyHasher>;
487 ///
488 /// let hash_map = HashMap::<u32, u32, MyBuildHasher>::default();
489 /// ```
490 ///
491 /// [`BuildHasher`]: trait.BuildHasher.html
492 /// [`Default`]: ../default/trait.Default.html
493 /// [method.default]: #method.default
494 /// [`Hasher`]: trait.Hasher.html
495 /// [`HashMap`]: ../../std/collections/struct.HashMap.html
496 /// [`HashSet`]: ../../std/collections/struct.HashSet.html
497 /// [zero-sized]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts
498 #[stable(since = "1.7.0", feature = "build_hasher")]
499 pub struct BuildHasherDefault<H>(marker::PhantomData<H>);
500
501 #[stable(since = "1.9.0", feature = "core_impl_debug")]
502 impl<H> fmt::Debug for BuildHasherDefault<H> {
503     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
504         f.pad("BuildHasherDefault")
505     }
506 }
507
508 #[stable(since = "1.7.0", feature = "build_hasher")]
509 impl<H: Default + Hasher> BuildHasher for BuildHasherDefault<H> {
510     type Hasher = H;
511
512     fn build_hasher(&self) -> H {
513         H::default()
514     }
515 }
516
517 #[stable(since = "1.7.0", feature = "build_hasher")]
518 impl<H> Clone for BuildHasherDefault<H> {
519     fn clone(&self) -> BuildHasherDefault<H> {
520         BuildHasherDefault(marker::PhantomData)
521     }
522 }
523
524 #[stable(since = "1.7.0", feature = "build_hasher")]
525 impl<H> Default for BuildHasherDefault<H> {
526     fn default() -> BuildHasherDefault<H> {
527         BuildHasherDefault(marker::PhantomData)
528     }
529 }
530
531 #[stable(since = "1.29.0", feature = "build_hasher_eq")]
532 impl<H> PartialEq for BuildHasherDefault<H> {
533     fn eq(&self, _other: &BuildHasherDefault<H>) -> bool {
534         true
535     }
536 }
537
538 #[stable(since = "1.29.0", feature = "build_hasher_eq")]
539 impl<H> Eq for BuildHasherDefault<H> {}
540
541 //////////////////////////////////////////////////////////////////////////////
542
543 mod impls {
544     use crate::mem;
545     use crate::slice;
546
547     use super::*;
548
549     macro_rules! impl_write {
550         ($(($ty:ident, $meth:ident),)*) => {$(
551             #[stable(feature = "rust1", since = "1.0.0")]
552             impl Hash for $ty {
553                 fn hash<H: Hasher>(&self, state: &mut H) {
554                     state.$meth(*self)
555                 }
556
557                 fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
558                     let newlen = data.len() * mem::size_of::<$ty>();
559                     let ptr = data.as_ptr() as *const u8;
560                     state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
561                 }
562             }
563         )*}
564     }
565
566     impl_write! {
567         (u8, write_u8),
568         (u16, write_u16),
569         (u32, write_u32),
570         (u64, write_u64),
571         (usize, write_usize),
572         (i8, write_i8),
573         (i16, write_i16),
574         (i32, write_i32),
575         (i64, write_i64),
576         (isize, write_isize),
577         (u128, write_u128),
578         (i128, write_i128),
579     }
580
581     #[stable(feature = "rust1", since = "1.0.0")]
582     impl Hash for bool {
583         fn hash<H: Hasher>(&self, state: &mut H) {
584             state.write_u8(*self as u8)
585         }
586     }
587
588     #[stable(feature = "rust1", since = "1.0.0")]
589     impl Hash for char {
590         fn hash<H: Hasher>(&self, state: &mut H) {
591             state.write_u32(*self as u32)
592         }
593     }
594
595     #[stable(feature = "rust1", since = "1.0.0")]
596     impl Hash for str {
597         fn hash<H: Hasher>(&self, state: &mut H) {
598             state.write(self.as_bytes());
599             state.write_u8(0xff)
600         }
601     }
602
603     #[stable(feature = "never_hash", since = "1.29.0")]
604     impl Hash for ! {
605         fn hash<H: Hasher>(&self, _: &mut H) {
606             *self
607         }
608     }
609
610     macro_rules! impl_hash_tuple {
611         () => (
612             #[stable(feature = "rust1", since = "1.0.0")]
613             impl Hash for () {
614                 fn hash<H: Hasher>(&self, _state: &mut H) {}
615             }
616         );
617
618         ( $($name:ident)+) => (
619             #[stable(feature = "rust1", since = "1.0.0")]
620             impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
621                 #[allow(non_snake_case)]
622                 fn hash<S: Hasher>(&self, state: &mut S) {
623                     let ($(ref $name,)+) = *self;
624                     $($name.hash(state);)+
625                 }
626             }
627         );
628     }
629
630     macro_rules! last_type {
631         ($a:ident,) => { $a };
632         ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
633     }
634
635     impl_hash_tuple! {}
636     impl_hash_tuple! { A }
637     impl_hash_tuple! { A B }
638     impl_hash_tuple! { A B C }
639     impl_hash_tuple! { A B C D }
640     impl_hash_tuple! { A B C D E }
641     impl_hash_tuple! { A B C D E F }
642     impl_hash_tuple! { A B C D E F G }
643     impl_hash_tuple! { A B C D E F G H }
644     impl_hash_tuple! { A B C D E F G H I }
645     impl_hash_tuple! { A B C D E F G H I J }
646     impl_hash_tuple! { A B C D E F G H I J K }
647     impl_hash_tuple! { A B C D E F G H I J K L }
648
649     #[stable(feature = "rust1", since = "1.0.0")]
650     impl<T: Hash> Hash for [T] {
651         fn hash<H: Hasher>(&self, state: &mut H) {
652             self.len().hash(state);
653             Hash::hash_slice(self, state)
654         }
655     }
656
657
658     #[stable(feature = "rust1", since = "1.0.0")]
659     impl<T: ?Sized + Hash> Hash for &T {
660         fn hash<H: Hasher>(&self, state: &mut H) {
661             (**self).hash(state);
662         }
663     }
664
665     #[stable(feature = "rust1", since = "1.0.0")]
666     impl<T: ?Sized + Hash> Hash for &mut T {
667         fn hash<H: Hasher>(&self, state: &mut H) {
668             (**self).hash(state);
669         }
670     }
671
672     #[stable(feature = "rust1", since = "1.0.0")]
673     impl<T: ?Sized> Hash for *const T {
674         fn hash<H: Hasher>(&self, state: &mut H) {
675             if mem::size_of::<Self>() == mem::size_of::<usize>() {
676                 // Thin pointer
677                 state.write_usize(*self as *const () as usize);
678             } else {
679                 // Fat pointer
680                 let (a, b) = unsafe {
681                     *(self as *const Self as *const (usize, usize))
682                 };
683                 state.write_usize(a);
684                 state.write_usize(b);
685             }
686         }
687     }
688
689     #[stable(feature = "rust1", since = "1.0.0")]
690     impl<T: ?Sized> Hash for *mut T {
691         fn hash<H: Hasher>(&self, state: &mut H) {
692             if mem::size_of::<Self>() == mem::size_of::<usize>() {
693                 // Thin pointer
694                 state.write_usize(*self as *const () as usize);
695             } else {
696                 // Fat pointer
697                 let (a, b) = unsafe {
698                     *(self as *const Self as *const (usize, usize))
699                 };
700                 state.write_usize(a);
701                 state.write_usize(b);
702             }
703         }
704     }
705 }