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
use std::cmp::PartialEq;
use std::fmt::{self, Debug};
use std::ops::Deref;

fn get_unique_id() -> u64 {
    use std::sync::atomic::{AtomicU64, Ordering};
    static EQ_COUNTER: AtomicU64 = AtomicU64::new(1);
    EQ_COUNTER.fetch_add(1, Ordering::Relaxed)
}

#[derive(Clone)]
pub struct EqBox<T> {
    id: u64,
    pub value: T,
}

impl<T> EqBox<T> {
    pub fn new(value: T) -> EqBox<T> {
        EqBox {
            id: get_unique_id(),
            value,
        }
    }

    pub fn into_inner(self) -> T {
        self.value
    }
}

impl<T> Deref for EqBox<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T> PartialEq for EqBox<T> {
    fn eq(&self, other: &EqBox<T>) -> bool {
        self.id == other.id
    }
}

impl<T> Debug for EqBox<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EqBox")
            .field("id", &self.id)
            .field("value", &"---")
            .finish()
    }
}