]> git.proxmox.com Git - rustc.git/blame - src/librustc_data_structures/ptr_key.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_data_structures / ptr_key.rs
CommitLineData
8faf50e0 1use std::ops::Deref;
dfeec247 2use std::{hash, ptr};
8faf50e0
XL
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 7pub struct PtrKey<'a, T>(pub &'a T);
8faf50e0
XL
8
9impl<'a, T> Clone for PtrKey<'a, T> {
dfeec247
XL
10 fn clone(&self) -> Self {
11 *self
12 }
8faf50e0
XL
13}
14
15impl<'a, T> Copy for PtrKey<'a, T> {}
16
17impl<'a, T> PartialEq for PtrKey<'a, T> {
18 fn eq(&self, rhs: &Self) -> bool {
19 ptr::eq(self.0, rhs.0)
20 }
21}
22
23impl<'a, T> Eq for PtrKey<'a, T> {}
24
25impl<'a, T> hash::Hash for PtrKey<'a, T> {
26 fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
27 (self.0 as *const T).hash(hasher)
28 }
29}
30
31impl<'a, T> Deref for PtrKey<'a, T> {
32 type Target = T;
33
34 fn deref(&self) -> &Self::Target {
35 self.0
36 }
37}