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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! This crate provides a unique token type.

use triomphe::Arc;

/// This type represents a unique token.
///
/// # Examples
///
/// ```
/// use unique_token::Unique;
///
/// let x = Unique::new();
/// let y = Unique::new();
///
/// // clones are equal
/// assert_eq!(x, x.clone());
/// assert_eq!(y, y.clone());
///
/// // tokens from different calls are unequal
/// assert_ne!(x, y);
/// ```
///
/// # Implementation
///
/// Each token carries an [`Arc`]
/// (not the std one, but a variant from [`triomphe`]
/// that doesn't track weak references).
/// Equality checks are implemented as
/// a pointer check on the zero-sized type ([`unit`])
/// inside the `Arc`.
#[derive(Clone, Eq)]
pub struct Unique(Arc<()>);

impl Unique {
    /// Create a new token.
    ///
    /// All tokens created by this function compare unequal.
    #[inline]
    pub fn new() -> Self {
        Self(Arc::new(()))
    }
}

impl PartialEq for Unique {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl std::hash::Hash for Unique {
    #[inline]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        usize::from(self).hash(state)
    }
}

impl std::fmt::Debug for Unique {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        let len = (usize::BITS / 4) as usize;
        write!(fmt, "0x{:0len$X}", usize::from(self))
    }
}

impl From<&Unique> for usize {
    #[inline]
    fn from(token: &Unique) -> usize {
        Arc::as_ptr(&token.0) as usize
    }
}

#[cfg(test)]
mod tests {
    use super::Unique;

    #[test]
    fn test_eq() {
        let x = Unique::new();
        let y = Unique::new();
        assert_ne!(&x, &y);
        assert_eq!(&x, &x.clone());
        assert_eq!(&y, &y.clone());
    }

    #[test]
    fn test_into_usize() {
        let x = Unique::new();
        let y = Unique::new();
        assert_ne!(usize::from(&x), usize::from(&y));
        assert_eq!(usize::from(&x), usize::from(&x.clone()));
        assert_eq!(usize::from(&y), usize::from(&y.clone()));
    }
}