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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::{cmp::PartialEq, fmt, ops::Deref};

use crate::{CellRef, RefOverflow};

/// Reference to a value.
pub struct Ref<'a, V>
where
    V: 'a,
{
    pub(crate) inner: CellRef<'a, V>,
}

impl<'a, V> Ref<'a, V> {
    /// Returns a new `Ref`.
    pub fn new(inner: CellRef<'a, V>) -> Self {
        Self { inner }
    }

    /// Returns a clone of this `Ref`.
    ///
    /// This method allows handling of reference overflows, but:
    ///
    /// * Having 2 billion (32-bit system) / 9 quintillion (64-bit system)
    ///   references to an object is not a realistic scenario in most
    ///   applications.
    ///
    /// * Applications that hold `Ref`s with an ever-increasing reference count
    ///   are not supported by this library.
    ///
    ///     Reaching `isize::MAX` may be possible with
    ///     `std::mem::forget(Ref::clone(&r))`.
    pub fn try_clone(&self) -> Result<Self, RefOverflow> {
        self.inner.try_clone().map(Self::new)
    }
}

impl<'a, V> Deref for Ref<'a, V> {
    type Target = V;

    fn deref(&self) -> &V {
        &self.inner
    }
}

impl<'a, V> fmt::Debug for Ref<'a, V>
where
    V: fmt::Debug + 'a,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let inner: &V = self;
        f.debug_struct("Ref").field("inner", inner).finish()
    }
}

impl<'a, V> PartialEq for Ref<'a, V>
where
    V: PartialEq + 'a,
{
    fn eq(&self, other: &Self) -> bool {
        let r_self: &V = self;
        let r_other: &V = other;
        r_self == r_other
    }
}

impl<'a, V> Clone for Ref<'a, V> {
    /// Returns a clone of this `Ref`.
    ///
    /// # Panics
    ///
    /// Panics if the number of references is `isize::MAX`:
    ///
    /// * Having 2 billion / 9 quintillion references to an object is not a
    ///   realistic scenario in most applications.
    /// * Applications that hold `Ref`s with an ever-increasing reference count
    ///   are not supported by this library.
    ///
    ///     Reaching `isize::MAX` may be possible with
    ///     `std::mem::forget(Ref::clone(&r))`.
    fn clone(&self) -> Self {
        Ref {
            inner: self.inner.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        fmt::{self, Write},
        sync::atomic::{AtomicUsize, Ordering},
    };

    use crate::{cell_ref::REF_LIMIT_MAX, CellRef, RefOverflow};

    use super::Ref;

    #[test]
    fn debug_includes_inner_field() -> fmt::Result {
        let flag = AtomicUsize::new(0);
        let value = A(1);
        let r#ref = Ref::new(CellRef {
            flag: &flag,
            value: &value,
        });

        let mut debug_string = String::with_capacity(64);
        write!(&mut debug_string, "{:?}", r#ref)?;
        assert_eq!("Ref { inner: A(1) }", debug_string.as_str());

        Ok(())
    }

    #[test]
    fn partial_eq_compares_value() -> fmt::Result {
        let flag = AtomicUsize::new(0);
        let value = A(1);
        let r#ref = Ref::new(CellRef {
            flag: &flag,
            value: &value,
        });

        assert_eq!(
            Ref::new(CellRef {
                flag: &flag,
                value: &value,
            }),
            r#ref
        );
        assert_ne!(
            Ref::new(CellRef {
                flag: &flag,
                value: &A(2),
            }),
            r#ref
        );

        Ok(())
    }

    #[test]
    fn try_clone_returns_ok_when_ref_count_less_than_usize_max() {
        let flag = &AtomicUsize::new(1);
        let value = &A(1);
        let ref_0 = Ref::new(CellRef { flag, value });

        assert_eq!(1, ref_0.inner.flag.load(Ordering::SeqCst));

        let try_clone_result = ref_0.try_clone();

        let ref_1 = try_clone_result.expect("try_clone_result to be ok");
        assert_eq!(2, ref_0.inner.flag.load(Ordering::SeqCst));
        assert_eq!(2, ref_1.inner.flag.load(Ordering::SeqCst));
    }

    #[test]
    fn try_clone_returns_err_when_ref_count_equals_usize_max() {
        let flag = &AtomicUsize::new(REF_LIMIT_MAX);
        let value = &A(1);
        let ref_0 = Ref::new(CellRef { flag, value });

        assert_eq!(REF_LIMIT_MAX, ref_0.inner.flag.load(Ordering::SeqCst));

        let try_clone_result = ref_0.try_clone();

        let e = try_clone_result.expect_err("try_clone_result to be err");
        assert_eq!(RefOverflow, e);

        // Ensure that the overflow is not persisted
        assert_eq!(REF_LIMIT_MAX, ref_0.inner.flag.load(Ordering::SeqCst));
    }

    #[test]
    fn clone_increments_cell_ref_count() {
        let flag = &AtomicUsize::new(1);
        let value = &A(1);
        let ref_0 = Ref::new(CellRef { flag, value });

        assert_eq!(1, ref_0.inner.flag.load(Ordering::SeqCst));

        let ref_1 = ref_0.clone();

        assert_eq!(2, ref_0.inner.flag.load(Ordering::SeqCst));
        assert_eq!(2, ref_1.inner.flag.load(Ordering::SeqCst));
    }

    #[test]
    #[should_panic(expected = "Failed to clone `CellRef`: Ref count exceeded `isize::MAX`")]
    fn clone_panics_when_ref_count_equals_usize_max() {
        let flag = &AtomicUsize::new(REF_LIMIT_MAX);
        let value = &A(1);
        let ref_0 = Ref::new(CellRef { flag, value });

        assert_eq!(REF_LIMIT_MAX, ref_0.inner.flag.load(Ordering::SeqCst));

        let _cloned = ref_0.clone();
    }

    #[derive(Debug, Clone, PartialEq)]
    struct A(usize);
}