lazy_list 0.1.1

Lazily-populated lists, finite or infinite
Documentation
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::{
    array,
    marker::PhantomData,
    mem::{ManuallyDrop, MaybeUninit},
    ptr::addr_of,
    sync::{
        atomic::{AtomicUsize, Ordering::SeqCst},
        Mutex,
    },
};

/// Number of elements in a chunk
const CHUNK_SIZE: usize = 64;

type ChunkArray<T> = [MaybeUninit<T>; CHUNK_SIZE];
type Chunk<T> = Box<ChunkArray<T>>;

/// A `Vec`-like data structure that stores elements in chunks of fixed size. It
/// supports mutation only by appending elements to the end. As a result, once
/// an element is pushed into a `ChunkedVec`, it will never be moved for as long
/// as the `ChunkedVec` exists. It has interior mutability, so its elements `T`
/// may be mutated through a `ChunkedVec`.
pub struct ChunkedVec<T> {
    /// The i'th element is at `chunks[i / CHUNK_SIZE][i % CHUNK_SIZE]`.
    ///
    /// SAFETY invariant: Each chunk points to a valid allocation, and the first
    /// `len` elements of this `ChunkedVec` are initialized.
    ///
    /// SAFETY invariant:
    /// * No references to the ChunkedVecInner exist except as allowed by the
    ///   mutex. Mutating the len field also requires holding the mutex lock.
    /// * No references to the contents of the Vec exist at any point.
    /// * Any number of mutable or shared references to the contents of the
    ///   chunks may exist, but they must obey the usual aliasing rules.
    inner: Mutex<ChunkedVecInner<T>>,
    len: AtomicUsize,
    _marker: PhantomData<Vec<T>>,
}

struct ChunkedVecInner<T> {
    chunks: Vec<Chunk<T>>,
}

pub struct Iter<'a, T> {
    chunked_vec: &'a ChunkedVec<T>,
    next_index: usize,
    /// SAFETY: This, if Some, is a valid pointer pointing to where the next
    /// element would be if it existed.
    next_ptr: Option<*const MaybeUninit<T>>,
}

pub struct IterMut<'a, T> {
    chunked_vec: &'a ChunkedVec<T>,
    next_index: usize,
    /// SAFETY: This, if Some, is a valid pointer pointing to where the next
    /// element would be if it existed.
    next_ptr: Option<*mut MaybeUninit<T>>,
}

pub struct IntoIter<T> {
    /// Same as `ChunkedVec`, except that in the SAFETY invariant, the
    /// initialized values are at indexes `current..len`, and there are no
    /// restrictions on when references to the `ChunkedVecInner` may exist.
    inner: ChunkedVecInner<T>,
    len: AtomicUsize,
    current: usize,
}

impl<T> ChunkedVecInner<T> {
    /// Returns a pointer to the i'th element of the `ChunkedVec`. Panics if the
    /// index is greater than the capacity.
    ///
    /// SAFETY: Always returns a valid pointer. The data there might or might
    /// not be initialized.
    fn index_ptr(&mut self, index: usize) -> *mut MaybeUninit<T> {
        unsafe {
            assert!(
                index / CHUNK_SIZE < self.chunks.len(),
                "index greater than capacity"
            );
            // SAFETY: No references to the contents of the Vec.
            let chunk: *mut ChunkArray<T> = self
                .chunks
                .as_mut_ptr()
                .add(index / CHUNK_SIZE)
                .cast::<*mut ChunkArray<T>>()
                .read();
            chunk.cast::<MaybeUninit<T>>().add(index % CHUNK_SIZE)
        }
    }
}

impl<T> ChunkedVec<T> {
    /// Creates a new, empty `ChunkedVec`.
    pub const fn new() -> Self {
        Self {
            inner: Mutex::new(ChunkedVecInner { chunks: Vec::new() }),
            len: AtomicUsize::new(0),
            _marker: PhantomData,
        }
    }

    /// The number of elements in the `ChunkedVec`.
    pub fn len(&self) -> usize {
        self.len.load(SeqCst)
    }

    /// Appends an element to the end of the `ChunkedVec`.
    pub fn push(&self, value: T) {
        unsafe {
            // SAFETY: After locking, we gain mutable access to the len.
            let mut inner = self.inner.lock().unwrap();
            // If capacity is full, add a new chunk
            if self.len() == inner.chunks.len() * CHUNK_SIZE {
                inner
                    .chunks
                    .push(Box::new(array::from_fn(|_| MaybeUninit::uninit())));
            }
            let new_element: *mut MaybeUninit<T> = inner.index_ptr(self.len());
            new_element.write(MaybeUninit::new(value));
            self.len.fetch_add(1, SeqCst);
        }
    }

    /// Returns a reference to the i'th element of the `ChunkedVec`. Panics if
    /// out of bounds.
    ///
    /// SAFETY: The caller must ensure that nobody is holding a mutable
    /// reference to this exact element.
    pub unsafe fn index(&self, index: usize) -> &T {
        assert!(index < self.len(), "index out of bounds");
        let mut inner = self.inner.lock().unwrap();
        let ptr: *mut MaybeUninit<T> = inner.index_ptr(index);
        // SAFETY: This index is already initialized, and won't be moved.
        (*ptr).as_ptr().as_ref().unwrap()
    }

    /// Returns a mutable reference to the i'th element of the `ChunkedVec`.
    /// Panics if out of bounds.
    ///
    /// SAFETY: The caller must ensure that nobody is holding a reference to
    /// this exact element.
    #[allow(clippy::mut_from_ref)]
    pub unsafe fn index_mut(&self, index: usize) -> &mut T {
        assert!(index < self.len(), "index out of bounds");
        let mut inner = self.inner.lock().unwrap();
        let ptr: *mut MaybeUninit<T> = inner.index_ptr(index);
        // SAFETY: This index is already initialized, and won't be moved.
        (*ptr).as_mut_ptr().as_mut().unwrap()
    }

    /// Returns an iterator over the elements of the `ChunkedVec`.
    ///
    /// SAFETY: The caller must ensure that nobody is holding a mutable
    /// reference to any element of the `ChunkedVec`.
    pub unsafe fn iter(&self) -> Iter<'_, T> {
        Iter {
            chunked_vec: self,
            next_index: 0,
            next_ptr: None,
        }
    }

    /// Returns a mutable iterator over the elements of the `ChunkedVec`.
    ///
    /// SAFETY: The caller must ensure that nobody is holding a reference to
    /// any element of the `ChunkedVec`.
    pub unsafe fn iter_mut(&self) -> IterMut<'_, T> {
        IterMut {
            chunked_vec: self,
            next_index: 0,
            next_ptr: None,
        }
    }
}

impl<T> IntoIterator for ChunkedVec<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;
    /// Returns an iterator that consumes the `ChunkedVec`.
    fn into_iter(self) -> IntoIter<T> {
        // SAFETY: No other references to the `ChunkedVecInner` exist.
        let self_manually_drop = ManuallyDrop::new(self);
        let mutex = unsafe { addr_of!(self_manually_drop.inner).read() };
        let inner = mutex.into_inner().unwrap();
        unsafe {
            IntoIter {
                inner,
                len: addr_of!(self_manually_drop.len).read(),
                current: 0,
            }
        }
    }
}

impl<T> Drop for ChunkedVec<T> {
    fn drop(&mut self) {
        unsafe {
            let inner: &mut ChunkedVecInner<T> = &mut self.inner.lock().unwrap();
            // SAFETY: We're dropping self already, so nobody else can have
            // references to things inside it.
            for i in 0..self.len() {
                (*inner.index_ptr(i)).assume_init_drop();
            }
        }
        // The Vec and the chunks are automatically dropped.
    }
}

impl<T> Drop for IntoIter<T> {
    fn drop(&mut self) {
        unsafe {
            // SAFETY: We're dropping self already, so nobody else can have
            // references to things inside it.
            for i in self.current..self.len.load(SeqCst) {
                (*self.inner.index_ptr(i)).assume_init_drop();
            }
        }
        // The Vec and the chunks are automatically dropped.
    }
}

impl<'a, T> Iter<'a, T> {
    pub fn next_index(&self) -> usize {
        self.next_index
    }
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<&'a T> {
        if self.next_index < self.chunked_vec.len() {
            let current_ptr = self.next_ptr.unwrap_or_else(|| {
                self.chunked_vec
                    .inner
                    .lock()
                    .unwrap()
                    .index_ptr(self.next_index)
            });
            self.next_index += 1;
            self.next_ptr = if self.next_index % CHUNK_SIZE == 0 {
                // End of the current chunk
                None
            } else {
                // SAFETY: We're not at the end of the current chunk.
                unsafe { Some(current_ptr.add(1)) }
            };
            // SAFETY: The client must ensure that nobody is holding a mutable
            // reference to any element of the `ChunkedVec`.
            Some(unsafe { &*current_ptr.cast::<T>() })
        } else {
            None
        }
    }
}

impl<'a, T> IterMut<'a, T> {
    pub fn next_index(&self) -> usize {
        self.next_index
    }
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;
    fn next(&mut self) -> Option<&'a mut T> {
        if self.next_index < self.chunked_vec.len() {
            let current_ptr = self.next_ptr.unwrap_or_else(|| {
                self.chunked_vec
                    .inner
                    .lock()
                    .unwrap()
                    .index_ptr(self.next_index)
            });
            self.next_index += 1;
            self.next_ptr = if self.next_index % CHUNK_SIZE == 0 {
                // End of the current chunk
                None
            } else {
                // SAFETY: We're not at the end of the current chunk.
                unsafe { Some(current_ptr.add(1)) }
            };
            // SAFETY: The client must ensure that nobody is holding a reference
            // to any element of the `ChunkedVec`. And we increment stuff so
            // this element isn't returned from this iterator again.
            Some(unsafe { &mut *current_ptr.cast::<T>() })
        } else {
            None
        }
    }
}

impl<T> Iterator for IntoIter<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        if self.current < self.len.load(SeqCst) {
            let ptr = self.inner.index_ptr(self.current);
            self.current += 1;
            // SAFETY: The index is initialized, and incrementing the index
            // means it won't be consumed again.
            Some(unsafe { ptr.read().assume_init() })
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_chunked_vec_basic() {
        let vec = ChunkedVec::new();
        vec.push(1);
        vec.push(2);
        vec.push(3);
        assert_eq!(vec.len(), 3);
        let ref0 = unsafe { vec.index(0) };
        let ref1 = unsafe { vec.index(1) };
        let ref2 = unsafe { vec.index(2) };
        assert_eq!(*ref0, 1);
        assert_eq!(*ref1, 2);
        assert_eq!(*ref2, 3);
        assert_eq!(*ref0, 1);
        assert_eq!(*ref1, 2);
        assert_eq!(*ref2, 3);
        // This invalidates ref0
        let ref0_mut = unsafe { vec.index_mut(0) };
        *ref0_mut = 4;
        assert_eq!(*ref0_mut, 4);
        assert_eq!(*ref1, 2);
        assert_eq!(*ref2, 3);
    }

    #[test]
    fn test_chunked_vec_large() {
        let vec = ChunkedVec::new();
        for i in 0..1000 {
            vec.push(i);
        }
        assert_eq!(vec.len(), 1000);
        let mut refs_vec = Vec::new();
        for i in 0..1000 {
            let ref_i = unsafe { vec.index(i) };
            assert_eq!(*ref_i, i);
            refs_vec.push(ref_i);
        }
        for i in 1000..2000 {
            vec.push(i);
        }
        assert_eq!(vec.len(), 2000);
        for i in 0..1000 {
            assert_eq!(*refs_vec[i], i);
        }
    }

    #[test]
    fn test_iter() {
        let vec = ChunkedVec::new();
        for i in 0..1000 {
            vec.push(i);
        }
        let mut iter = unsafe { vec.iter() };
        for i in 0..1000 {
            assert_eq!(iter.next(), Some(&i));
        }
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_iter_mut() {
        let vec = ChunkedVec::new();
        for i in 0..1000 {
            vec.push(i);
        }
        let mut iter = unsafe { vec.iter_mut() };
        for i in 0..1000 {
            assert_eq!(iter.next(), Some(&mut { i }));
        }
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_iter_mut_always_near_end() {
        let vec = ChunkedVec::new();
        let mut iter = unsafe { vec.iter_mut() };
        for i in 0..1000 {
            vec.push(i);
            assert_eq!(iter.next(), Some(&mut { i }));
        }
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_into_iter() {
        let vec = ChunkedVec::new();
        for i in 0..1000 {
            vec.push(i);
        }
        let mut iter = vec.into_iter();
        for i in 0..1000 {
            assert_eq!(iter.next(), Some(i));
        }
        assert_eq!(iter.next(), None);

        let vec = ChunkedVec::new();
        for i in 0..1000 {
            vec.push(i);
        }
        let mut iter = vec.into_iter();
        for i in 0..500 {
            assert_eq!(iter.next(), Some(i));
        }
        // Drop the iterator
    }
}