David LeGare | 77e4bb9 | 2022-03-02 16:21:23 +0000 | [diff] [blame] | 1 | use crate::compat::*; |
Joel Galenson | 2370d12 | 2020-10-12 16:02:26 -0700 | [diff] [blame] | 2 | |
| 3 | use 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)] |
| 8 | pub struct ByPtr<K>(K); |
| 9 | |
| 10 | impl<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 | |
| 22 | impl<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 | |