aboutsummaryrefslogtreecommitdiff
path: root/src/by_ptr.rs
blob: 7dc4c7747b857b56bbd4bf4f746086761d974aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::compat::*;

use super::traits::*;

/// Wrapper struct for using pointer equality and hashes rather
/// than pointed-to value equality and hashes.
#[derive(Clone, Debug)]
pub struct ByPtr<K>(K);

impl<K: WeakElement> WeakElement for ByPtr<K> {
    type Strong = K::Strong;

    fn new(view: &Self::Strong) -> Self {
        ByPtr(K::new(view))
    }

    fn view(&self) -> Option<Self::Strong> {
        self.0.view()
    }
}

impl<K: WeakElement> WeakKey for ByPtr<K>
    where K::Strong: Deref
{
    type Key = *const <K::Strong as Deref>::Target;

    fn with_key<F, R>(view: &Self::Strong, f: F) -> R
        where F: FnOnce(&Self::Key) -> R
    {
        f(&(view.deref() as *const _))
    }
}