blob: 7dc4c7747b857b56bbd4bf4f746086761d974aca [file] [log] [blame]
David LeGare77e4bb92022-03-02 16:21:23 +00001use crate::compat::*;
Joel Galenson2370d122020-10-12 16:02:26 -07002
3use super::traits::*;
4
5/// Wrapper struct for using pointer equality and hashes rather
6/// than pointed-to value equality and hashes.
7#[derive(Clone, Debug)]
8pub struct ByPtr<K>(K);
9
10impl<K: WeakElement> WeakElement for ByPtr<K> {
11 type Strong = K::Strong;
12
13 fn new(view: &Self::Strong) -> Self {
14 ByPtr(K::new(view))
15 }
16
17 fn view(&self) -> Option<Self::Strong> {
18 self.0.view()
19 }
20}
21
22impl<K: WeakElement> WeakKey for ByPtr<K>
23 where K::Strong: Deref
24{
25 type Key = *const <K::Strong as Deref>::Target;
26
27 fn with_key<F, R>(view: &Self::Strong, f: F) -> R
28 where F: FnOnce(&Self::Key) -> R
29 {
30 f(&(view.deref() as *const _))
31 }
32}
33