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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! A library for scoped `Vec`s, allowing multi-level divergence from
//! the root element.
//!
//! This is useful for monitoring state within a de facto tree where
//! links to parents aren't necessarily needed. Consumers can keep
//! references to a specific parent if required and check the values
//! from the scope of their choosing, parents are free to be dropped if
//! they're no longer required.
//!
//! The full [std::vec::Vec] spec has not yet been implemented but as
//! the library stabilises, more and more of the `Vec` library will be
//! supported - however there will be some divergence from the API where
//! necessary given the structural differences of a `ScopedVec`.
//!
//! Example:
//! ```
//! # use scoped_vec::ScopedVec;
//! let mut root = ScopedVec::new();
//! root.push(3);
//!
//! {
//!     let mut scope1 = root.scope();
//!     scope1.push(4);
//!     {
//!         let mut scope1_scope1 = scope1.scope();
//!         scope1_scope1.push(5);
//!     }
//!
//!     let mut iter = scope1.iter();
//!     assert_eq!(iter.next(), Some(&4));
//!     assert_eq!(iter.next(), Some(&5));
//!     assert_eq!(iter.next(), None);
//! }
//!
//! {
//!     let mut scope2 = root.scope();
//!     scope2.push(6);
//! }
//!
//! let mut iter = root.iter();
//! assert_eq!(iter.next(), Some(&3));
//! assert_eq!(iter.next(), Some(&4));
//! assert_eq!(iter.next(), Some(&5));
//! assert_eq!(iter.next(), Some(&6));
//! assert_eq!(iter.next(), None);
//! ```

use std::sync::{Arc, RwLock, RwLockReadGuard};
use owning_ref::OwningHandle;

/// A `ScopedVec` instance can either represent the root element or a
/// divergence of it. Refer to the crate's documentation for usage
/// examples of the scoped-vec library.
///
/// Cloning a `ScopedVec` will result in a reference to the same scope,
/// and adding a value to one of the cloned instances will result in
/// the value being added to all instances and available for all the
/// parent instances to iterate over.
#[derive(Clone)]
pub struct ScopedVec<T: Clone> {
    inner: Arc<RwLock<Vec<T>>>,
    children: Arc<RwLock<Vec<ScopedVec<T>>>>,
}

impl<T: Clone> ScopedVec<T> {
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::default()),
            children: Arc::new(RwLock::default())
        }
    }

    /// Create a new `ScopedVec` as a child of this one.
    pub fn scope(&mut self) -> ScopedVec<T> {
        let new = ScopedVec::new();
        //           .get_mut()?
        self.children.write().unwrap().push(new.clone());
        new
    }

    pub fn push(&mut self, val: T) {
        //        .get_mut()?
        self.inner.write().unwrap().push(val);
    }

    pub fn iter(&self) -> ScopedVecIterator<T> {
        ScopedVecIterator::new(self)
    }
}

impl<T: Clone + PartialEq> ScopedVec<T> {
    pub fn contains(&self, val: &T) -> bool {
        self.iter().any(|f| *f == *val)
    }
}

pub struct ScopedVecGuardHolder<'a, T: Clone> {
    inner: RwLockReadGuard<'a, Vec<T>>,
    children: RwLockReadGuard<'a, Vec<ScopedVec<T>>>,
}

pub struct ScopedVecIterator<'a, T: Clone> {
    iterator: OwningHandle<Box<ScopedVecGuardHolder<'a, T>>, Box<dyn Iterator<Item = &'a T> + 'a>>,
}
impl<'a, T: Clone> ScopedVecIterator<'a, T> {
    fn new(vec: &'a ScopedVec<T>) -> Self {
        Self {
            iterator: OwningHandle::new_with_fn(
                Box::new(ScopedVecGuardHolder {
                    inner: vec.inner.read().unwrap(),
                    children: vec.children.read().unwrap()
                }),
                |g| {
                    // the value behind the raw pointer `g` is boxed, so we're safe to dereference
                    let guards = unsafe { &*g };

                    Box::new(guards.inner.iter()
                        .chain(
                            guards.children.iter()
                                .map(ScopedVec::iter)
                                .flatten()
                        )) as Box<dyn Iterator<Item = &'a T>>
                }
            )
        }
    }
}
impl<'a, T: Clone> Iterator for ScopedVecIterator<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.iterator.next()
    }
}

#[cfg(test)]
mod tests {
    use crate::ScopedVec;

    #[test]
    fn unscoped_standard() {
        let mut root = ScopedVec::new();
        root.push(3);
        let mut iter = root.iter();
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn scoped_cant_read_root() {
        let mut root = ScopedVec::new();
        root.push(3);

        let scoped = root.scope();
        let mut iter = scoped.iter();
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn root_can_read_scoped() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped = root.scope();
        scoped.push(4);

        let mut iter = root.iter();
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next(), Some(&4));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn root_can_read_nested_scoped() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped = root.scope();
        scoped.push(4);

        let mut nested_scoped = scoped.scope();
        nested_scoped.push(5);

        let mut iter = root.iter();
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next(), Some(&4));
        assert_eq!(iter.next(), Some(&5));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn scoped_can_read_nested_scoped() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped = root.scope();
        scoped.push(4);

        let mut nested_scoped = scoped.scope();
        nested_scoped.push(5);

        let mut iter = scoped.iter();
        assert_eq!(iter.next(), Some(&4));
        assert_eq!(iter.next(), Some(&5));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn nested_scoped_cant_read_backwards() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped = root.scope();
        scoped.push(4);

        let mut nested_scoped = scoped.scope();
        nested_scoped.push(5);

        let mut iter = nested_scoped.iter();
        assert_eq!(iter.next(), Some(&5));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn can_drop_scopes() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped = root.scope();
        scoped.push(4);

        drop(root);

        let mut nested_scoped = scoped.scope();
        nested_scoped.push(5);

        {
            let mut iter = scoped.iter();
            assert_eq!(iter.next(), Some(&4));
            assert_eq!(iter.next(), Some(&5));
            assert_eq!(iter.next(), None);
        }

        drop(scoped);

        {
            let mut iter = nested_scoped.iter();
            assert_eq!(iter.next(), Some(&5));
            assert_eq!(iter.next(), None);
        }
    }

    #[test]
    fn diverged_scopes_can_be_read() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped = root.scope();
        scoped.push(4);

        let mut nested_scoped1 = scoped.scope();
        nested_scoped1.push(5);

        let mut nested_scoped2 = scoped.scope();
        nested_scoped2.push(6);

        let mut iter = root.iter();
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next(), Some(&4));
        assert_eq!(iter.next(), Some(&5));
        assert_eq!(iter.next(), Some(&6));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn diverged_adjacent_scopes_cant_interact() {
        let mut root = ScopedVec::new();
        root.push(3);

        let mut scoped1 = root.scope();
        scoped1.push(4);

        let mut scoped2 = root.scope();
        scoped2.push(5);

        let mut iter = scoped1.iter();
        assert_eq!(iter.next(), Some(&4));
        assert_eq!(iter.next(), None);

        let mut iter = scoped2.iter();
        assert_eq!(iter.next(), Some(&5));
        assert_eq!(iter.next(), None);
    }
}