]>
Commit | Line | Data |
---|---|---|
8faf50e0 XL |
1 | use std::{hash, ptr}; |
2 | use std::ops::Deref; | |
3 | ||
4 | /// A wrapper around reference that compares and hashes like a pointer. | |
5 | /// Can be used as a key in sets/maps indexed by pointers to avoid `unsafe`. | |
6 | #[derive(Debug)] | |
9fa01778 | 7 | pub struct PtrKey<'a, T>(pub &'a T); |
8faf50e0 XL |
8 | |
9 | impl<'a, T> Clone for PtrKey<'a, T> { | |
10 | fn clone(&self) -> Self { *self } | |
11 | } | |
12 | ||
13 | impl<'a, T> Copy for PtrKey<'a, T> {} | |
14 | ||
15 | impl<'a, T> PartialEq for PtrKey<'a, T> { | |
16 | fn eq(&self, rhs: &Self) -> bool { | |
17 | ptr::eq(self.0, rhs.0) | |
18 | } | |
19 | } | |
20 | ||
21 | impl<'a, T> Eq for PtrKey<'a, T> {} | |
22 | ||
23 | impl<'a, T> hash::Hash for PtrKey<'a, T> { | |
24 | fn hash<H: hash::Hasher>(&self, hasher: &mut H) { | |
25 | (self.0 as *const T).hash(hasher) | |
26 | } | |
27 | } | |
28 | ||
29 | impl<'a, T> Deref for PtrKey<'a, T> { | |
30 | type Target = T; | |
31 | ||
32 | fn deref(&self) -> &Self::Target { | |
33 | self.0 | |
34 | } | |
35 | } |