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
use std::cell::{Cell, UnsafeCell};
use std::rc::Rc;
use std::ptr::null_mut;

/// A reference counted pointer that allows interior mutability
///
/// The [RcRcu] is functionally roughly equivalent to
/// `Rc<RefCell<T>>`, except that reads (of the old value) may happen
/// while a write is taking place.  Reads are actually slightly slower
/// than an `Rc<RefCell<T>>`, so mostly you gaining ergonomics by
/// using this.
///
/// ```
/// let x = rcu_clean::RcRcu::new(3);
/// let y: &usize = &(*x);
/// let z = x.clone();
/// *x.update() = 7; // Wow, we are mutating something we have borrowed!
/// assert_eq!(*y, 3); // the old reference is still valid.
/// assert_eq!(*x, 7); // but the pointer now points to the new value.
/// assert_eq!(*z, 7); // but the cloned pointer also points to the new value.
/// ```
pub struct RcRcu<T> {
    inner: Rc<Inner<T>>,
    have_borrowed: Cell<bool>,
}
impl<T: Clone> Clone for RcRcu<T> {
    fn clone(&self) -> Self {
        RcRcu {
            inner: self.inner.clone(),
            have_borrowed: Cell::new(false),
        }
    }
}
pub struct Inner<T> {
    borrow_count: Cell<usize>,
    am_writing: Cell<bool>,
    list: List<T>
}
pub struct List<T> {
    value: UnsafeCell<T>,
    next: Cell<*mut List<T>>,
}

impl<T> std::ops::Deref for RcRcu<T> {
    type Target = T;
    fn deref(&self) -> &T {
        let aleady_borrowed = self.have_borrowed.get();
        if !aleady_borrowed {
            self.inner.borrow_count.set(self.inner.borrow_count.get() + 1);
            self.have_borrowed.set(true); // indicate we have borrowed this once.
        }
        if self.inner.list.next.get() == null_mut() {
            unsafe { &*self.inner.list.value.get() }
        } else {
            unsafe { &* (*self.inner.list.next.get()).value.get() }
        }
    }
}
impl<T> std::borrow::Borrow<T> for RcRcu<T> {
    fn borrow(&self) -> &T {
        &*self
    }
}
impl<T> Drop for List<T> {
    fn drop(&mut self) {
        if self.next.get() != null_mut() {
            unsafe { Box::from_raw(self.next.get()); }
        }
    }
}
impl<'a,T: Clone> RcRcu<T> {
    pub fn new(x: T) -> Self {
        RcRcu {
            have_borrowed: Cell::new(false),
            inner: Rc::new(Inner {
                borrow_count: Cell::new(0),
                am_writing: Cell::new(false),
                list: List {
                    value: UnsafeCell::new(x),
                    next: Cell::new(null_mut()),
                },
            }),
        }
    }
    pub fn update(&'a self) -> Guard<'a, T> {
        if self.inner.am_writing.get() {
            panic!("Cannont update an RcRcu twice simultaneously.");
        }
        self.inner.am_writing.set(true);
        Guard {
            list: Some(List {
                value: UnsafeCell::new((*(*self)).clone()),
                next: self.inner.list.next.clone(),
            }),
            rc_guts: &self.inner,
        }
    }
    pub fn clean(&mut self) {
        let aleady_borrowed = self.have_borrowed.get();
        if aleady_borrowed {
            self.inner.borrow_count.set(self.inner.borrow_count.get() - 1);
            self.have_borrowed.set(false); // indicate we have no longer borrowed this.
        }
        if self.inner.borrow_count.get() == 0 &&
            self.inner.list.next.get() != null_mut()
        {
            unsafe {
                std::mem::swap(&mut *self.inner.list.value.get(),
                               &mut *(*self.inner.list.next.get()).value.get());
                Box::from_raw(self.inner.list.next.replace(null_mut()));
            }
        }
    }
}

pub struct Guard<'a,T: Clone> {
    list: Option<List<T>>,
    rc_guts: &'a Inner<T>,
}
impl<'a,T: Clone> std::ops::Deref for Guard<'a,T> {
    type Target = T;
    fn deref(&self) -> &T {
        if let Some(ref list) = self.list {
            unsafe { & *list.value.get() }
        } else {
            unreachable!()
        }
    }
}
impl<'a,T: Clone> std::ops::DerefMut for Guard<'a,T> {
    fn deref_mut(&mut self) -> &mut T {
        if let Some(ref list) = self.list {
            unsafe { &mut *list.value.get() }
        } else {
            unreachable!()
        }
    }
}
impl<'a,T: Clone> Drop for Guard<'a,T> {
    fn drop(&mut self) {
        let list = std::mem::replace(&mut self.list, None);
        self.rc_guts.list.next.set(Box::into_raw(Box::new(list.unwrap())));
        self.rc_guts.am_writing.set(false);
    }
}