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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::{
    any::{Any, TypeId},
    fmt::Debug,
    marker::PhantomData,
};

#[derive(Hash)]
pub struct SharedForgottenKey<T: Any>(usize, PhantomData<T>);

impl<T: Any> SharedForgottenKey<T> {
    pub(crate) fn new(n: usize) -> Self {
        Self(n, PhantomData)
    }
}

impl<T: Any> SharedForgottenKey<T> {
    pub fn as_usize(&self) -> &usize {
        &self.0
    }

    pub fn into_type_and_usize(self) -> (TypeId, usize) {
        (TypeId::of::<T>(), self.0)
    }

    pub unsafe fn from_usize(n: usize) -> Self {
        Self(n, PhantomData)
    }
}

impl<T: Any> PartialEq for SharedForgottenKey<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T: Any> Eq for SharedForgottenKey<T> {}

impl<T: Any> Clone for SharedForgottenKey<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone(), PhantomData)
    }
}

impl<T: Any> Copy for SharedForgottenKey<T> {}

impl<T: Any> Debug for SharedForgottenKey<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple(format!("SharedForgottenKey<{:?}>", TypeId::of::<T>()).as_str())
            .field(&self.0)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use std::marker::PhantomData;

    use super::SharedForgottenKey;

    #[test]
    fn test_clone_eq() {
        struct NotClone {
            _val: u8,
        }

        let a = SharedForgottenKey::<NotClone>(1, PhantomData);

        let b = a.clone();

        assert_eq!(a, b);
    }
}