cow_vec 1.4.0

A vector-like container optimized for efficient cloning with copy-on-write semantics
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
use std::fmt;
use std::ops::{Bound, Index, IndexMut, RangeBounds};
use std::sync::{Arc, Mutex};

use typed_arena::Arena;

use super::CowVecIter;

/// Shared arena that stores values allocated by `CowVec` instances.
///
/// The arena is append-only: values are never removed or moved once allocated.
/// This guarantees that pointers to arena items remain valid for the arena's lifetime.
struct CowArena<T> {
    arena: Mutex<Arena<T>>,
}

impl<T> CowArena<T> {
    fn new() -> Self {
        Self {
            arena: Mutex::new(Arena::new()),
        }
    }

    fn with_capacity(capacity: usize) -> Self {
        Self {
            arena: Mutex::new(Arena::with_capacity(capacity)),
        }
    }

    /// Allocates a value in the arena and returns a raw pointer to it.
    ///
    /// # Safety
    /// The returned pointer is valid for the lifetime of the arena.
    /// Since the arena is append-only and wrapped in Arc, the pointer
    /// remains valid as long as any CowVec holds a reference to this arena.
    fn alloc(&self, value: T) -> *const T {
        let arena = self.arena.lock().unwrap();
        let reference = arena.alloc(value);
        reference as *const T
    }

    /// Returns the total number of allocations in this arena.
    fn len(&self) -> usize {
        self.arena.lock().unwrap().len()
    }
}

/// A vector-like container optimized for efficient cloning.
///
/// `CowVec` uses a shared arena (via `Arc`) for storing values. Each instance
/// maintains its own vector of pointers to items in the shared arena.
/// When cloned, only the pointer vector is cloned while the arena is shared.
///
/// # Copy-on-Write Semantics
/// The `set` method implements copy-on-write: it allocates a new value in the
/// arena and updates only this instance's pointer. Other clones continue to
/// see the original value.
///
/// # Thread Safety
/// `CowVec<T>` is `Send` and `Sync` when `T: Send + Sync`.
///
/// # Example
/// ```
/// use cow_vec::CowVec;
///
/// let vec1 = CowVec::from(vec![1, 2, 3]);
/// let mut vec2 = vec1.clone(); // Cheap clone - shares the arena
/// vec2.set(0, 10); // Only vec2 sees the change
/// assert_eq!(vec1[0], 1);
/// assert_eq!(vec2[0], 10);
/// ```
pub struct CowVec<T> {
    arena: Arc<CowArena<T>>,
    items: Arc<Vec<*const T>>,
}

// SAFETY: CowVec is Send+Sync because:
// - Arc<CowArena<T>> is Send+Sync when T: Send+Sync (CowArena contains Mutex<Arena<T>>)
// - *const T pointers are valid as long as arena lives (guaranteed by Arc)
// - All mutation goes through Mutex
// - We only provide &T access, never &mut T
unsafe impl<T: Send + Sync> Send for CowVec<T> {}
unsafe impl<T: Send + Sync> Sync for CowVec<T> {}

impl<T> CowVec<T> {
    /// Returns a mutable reference to the items vector.
    ///
    /// If the items Arc is shared with other CowVec instances, this will
    /// clone the vector first (copy-on-write semantics).
    #[inline]
    fn items_mut(&mut self) -> &mut Vec<*const T> {
        Arc::make_mut(&mut self.items)
    }

    /// Creates a new empty `CowVec`.
    pub fn new() -> Self {
        Self {
            arena: Arc::new(CowArena::new()),
            items: Arc::new(Vec::new()),
        }
    }

    /// Creates a new `CowVec` with the specified capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            arena: Arc::new(CowArena::with_capacity(capacity)),
            items: Arc::new(Vec::with_capacity(capacity)),
        }
    }

    /// Returns the number of elements in this vector.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Returns `true` if this vector contains no elements.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Returns `true` if the structure (element pointers/order) is shared with other clones.
    ///
    /// When this returns `true`, the next mutation will trigger a copy of the
    /// internal pointer vector (copy-on-write semantics).
    pub fn is_structure_shared(&self) -> bool {
        Arc::strong_count(&self.items) > 1
    }

    /// Returns `true` if the storage (arena with actual values) is shared with other clones.
    ///
    /// This typically returns `true` after any clone operation, as all clones share
    /// the same arena for value storage.
    pub fn is_storage_shared(&self) -> bool {
        Arc::strong_count(&self.arena) > 1
    }

    /// Returns the elements as a slice of references.
    ///
    /// This provides efficient access to all elements without iteration,
    /// useful when you need to pass the data to APIs expecting `&[&T]`.
    ///
    /// # Example
    /// ```
    /// use cow_vec::CowVec;
    ///
    /// let vec = CowVec::from(vec![1, 2, 3]);
    /// let slice: &[&i32] = vec.as_slice();
    /// assert_eq!(slice.len(), 3);
    /// assert_eq!(*slice[0], 1);
    /// ```
    pub fn as_slice(&self) -> &[&T] {
        // SAFETY: This transmute is sound because:
        // 1. `*const T` and `&T` have identical memory layouts (both are pointers)
        // 2. All pointers in `self.items` are valid for the arena's lifetime
        // 3. The arena outlives this `CowVec` (guaranteed by Arc)
        // 4. The returned slice borrows `&self`, so it cannot outlive the CowVec
        // 5. The arena is append-only, so pointers are never invalidated
        unsafe { std::mem::transmute(self.items.as_slice()) }
    }

    /// Returns a reference to the element at the given index, or `None` if out of bounds.
    pub fn get(&self, index: usize) -> Option<&T> {
        self.items.get(index).map(|ptr| {
            // SAFETY: The pointer is valid because:
            // 1. It was obtained from arena.alloc()
            // 2. The arena never moves or deallocates items
            // 3. The arena lives as long as this CowVec (via Arc)
            unsafe { &**ptr }
        })
    }

    /// Appends an element to the back of this vector.
    ///
    /// The element is stored in the shared arena, and this instance's
    /// pointer list is updated to include it.
    pub fn push(&mut self, value: T) {
        let ptr = self.arena.alloc(value);
        self.items_mut().push(ptr);
    }

    /// Returns an iterator over references to the elements.
    pub fn iter(&self) -> CowVecIter<'_, T> {
        CowVecIter {
            vec: self,
            position: 0,
        }
    }

    /// Returns a reference to the first element, or `None` if empty.
    pub fn first(&self) -> Option<&T> {
        self.get(0)
    }

    /// Returns a reference to the last element, or `None` if empty.
    pub fn last(&self) -> Option<&T> {
        if self.is_empty() {
            None
        } else {
            self.get(self.len() - 1)
        }
    }

    /// Removes the last element and returns it, or `None` if empty.
    ///
    /// Note: The value remains in the shared arena but is no longer
    /// accessible through this `CowVec` instance.
    pub fn pop(&mut self) -> Option<&T> {
        self.items_mut().pop().map(|ptr| {
            // SAFETY: Same as get() - pointer is valid for arena's lifetime
            unsafe { &*ptr }
        })
    }

    /// Removes and returns the element at the given index.
    ///
    /// All elements after the index are shifted left.
    ///
    /// Note: The value remains in the shared arena but is no longer
    /// accessible through this `CowVec` instance.
    ///
    /// # Panics
    /// Panics if `index >= len()`.
    pub fn remove(&mut self, index: usize) -> &T {
        let ptr = self.items_mut().remove(index);
        // SAFETY: Same as get() - pointer is valid for arena's lifetime
        unsafe { &*ptr }
    }

    /// Swaps two elements in the vector.
    ///
    /// # Panics
    /// Panics if either index is out of bounds.
    pub fn swap(&mut self, a: usize, b: usize) {
        self.items_mut().swap(a, b);
    }

    /// Reverses the order of elements in the vector.
    pub fn reverse(&mut self) {
        self.items_mut().reverse();
    }

    /// Shortens the vector, keeping the first `len` elements.
    ///
    /// If `len` is greater than or equal to the current length, this has no effect.
    ///
    /// Note: Removed values remain in the shared arena.
    pub fn truncate(&mut self, len: usize) {
        self.items_mut().truncate(len);
    }

    /// Clears the vector, removing all elements.
    ///
    /// Note: Values remain in the shared arena but are no longer
    /// accessible through this `CowVec` instance.
    pub fn clear(&mut self) {
        self.items_mut().clear();
    }

    /// Extends the vector with elements from an iterator.
    pub fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for item in iter {
            self.push(item);
        }
    }

    /// Returns the index of the first element matching the predicate.
    pub fn position<P>(&self, predicate: P) -> Option<usize>
    where
        P: FnMut(&T) -> bool,
    {
        self.iter().position(predicate)
    }

    /// Inserts an element at position `index`, shifting all elements after it to the right.
    ///
    /// # Panics
    /// Panics if `index > len()`.
    ///
    /// # Example
    /// ```
    /// use cow_vec::CowVec;
    ///
    /// let mut vec = CowVec::from(vec![1, 2, 3]);
    /// vec.insert(1, 10);
    /// assert_eq!(vec.to_vec(), vec![1, 10, 2, 3]);
    /// ```
    pub fn insert(&mut self, index: usize, value: T) {
        let ptr = self.arena.alloc(value);
        self.items_mut().insert(index, ptr);
    }

    /// Retains only the elements specified by the predicate.
    ///
    /// Removes all elements for which the predicate returns `false`.
    ///
    /// Note: Removed values remain in the shared arena.
    ///
    /// # Example
    /// ```
    /// use cow_vec::CowVec;
    ///
    /// let mut vec = CowVec::from(vec![1, 2, 3, 4, 5]);
    /// vec.retain(|&x| x % 2 == 0);
    /// assert_eq!(vec.to_vec(), vec![2, 4]);
    /// ```
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&T) -> bool,
    {
        self.items_mut().retain(|ptr| {
            // SAFETY: Pointer is valid for arena's lifetime
            let value = unsafe { &**ptr };
            f(value)
        });
    }

    /// Splits the vector into two at the given index.
    ///
    /// Returns a new `CowVec` containing elements from `at` to the end.
    /// After this call, `self` contains elements `[0, at)` and the returned
    /// `CowVec` contains elements `[at, len)`.
    ///
    /// Both vectors share the same arena, so this is an efficient operation.
    ///
    /// # Panics
    /// Panics if `at > len()`.
    ///
    /// # Example
    /// ```
    /// use cow_vec::CowVec;
    ///
    /// let mut vec = CowVec::from(vec![1, 2, 3, 4, 5]);
    /// let tail = vec.split_off(3);
    /// assert_eq!(vec.to_vec(), vec![1, 2, 3]);
    /// assert_eq!(tail.to_vec(), vec![4, 5]);
    /// ```
    pub fn split_off(&mut self, at: usize) -> Self {
        let tail_items = self.items_mut().split_off(at);
        Self {
            arena: Arc::clone(&self.arena),
            items: Arc::new(tail_items),
        }
    }

    /// Removes the specified range and replaces it with elements from the iterator.
    ///
    /// Returns the removed elements as a `Vec` of references.
    ///
    /// # Panics
    /// Panics if the range is out of bounds.
    ///
    /// # Example
    /// ```
    /// use cow_vec::CowVec;
    ///
    /// let mut vec = CowVec::from(vec![1, 2, 3, 4, 5]);
    /// let removed: Vec<&i32> = vec.splice(1..3, vec![10, 20, 30]);
    /// assert_eq!(removed, vec![&2, &3]);
    /// assert_eq!(vec.to_vec(), vec![1, 10, 20, 30, 4, 5]);
    /// ```
    pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Vec<&T>
    where
        R: RangeBounds<usize>,
        I: IntoIterator<Item = T>,
    {
        let start = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n + 1,
            Bound::Unbounded => 0,
        };
        let end = match range.end_bound() {
            Bound::Included(&n) => n + 1,
            Bound::Excluded(&n) => n,
            Bound::Unbounded => self.len(),
        };

        // Allocate new elements in arena
        let new_ptrs: Vec<*const T> = replace_with
            .into_iter()
            .map(|item| self.arena.alloc(item))
            .collect();

        // Splice the pointer vector and collect removed pointers
        let removed_ptrs: Vec<*const T> = self.items_mut().splice(start..end, new_ptrs).collect();

        // Convert removed pointers to references
        removed_ptrs
            .into_iter()
            .map(|ptr| {
                // SAFETY: Pointer is valid for arena's lifetime
                unsafe { &*ptr }
            })
            .collect()
    }
}

impl<T: PartialEq> CowVec<T> {
    /// Returns `true` if the vector contains the given value.
    pub fn contains(&self, value: &T) -> bool {
        self.iter().any(|item| item == value)
    }
}

impl<T: Clone> CowVec<T> {
    /// Converts this `CowVec` into a `Vec` by cloning all elements.
    pub fn to_vec(&self) -> Vec<T> {
        self.iter().cloned().collect()
    }

    /// Clones this `CowVec`, creating a fresh arena if the current one exceeds max_capacity.
    ///
    /// If the arena's allocation count exceeds `max_capacity`, a new arena is created
    /// containing only the current elements (compacting the data). Otherwise, the arena
    /// is shared as with regular `clone()`.
    ///
    /// This is useful for controlling memory growth when the arena has accumulated
    /// many allocations from `push`, `set`, or garbage from `pop`/`remove` operations.
    pub fn clone_with_max_capacity(&self, max_capacity: usize) -> Self {
        if self.arena.len() <= max_capacity {
            return self.clone();
        }

        // Create a fresh arena with just the current elements.
        let new_arena = Arc::new(CowArena::with_capacity(self.len()));
        let new_items: Vec<*const T> = self
            .iter()
            .map(|item| new_arena.alloc(item.clone()))
            .collect();

        Self {
            arena: new_arena,
            items: Arc::new(new_items),
        }
    }
}

impl<T> CowVec<T> {
    /// Sets the value at the given index.
    ///
    /// This implements copy-on-write semantics: a new entry is allocated in the
    /// arena with the given value, and only this instance's pointer is updated.
    /// Other clones of this `CowVec` continue to see the original value.
    ///
    /// # Panics
    /// Panics if `index >= len()`.
    pub fn set(&mut self, index: usize, value: T) {
        if index >= self.items.len() {
            panic!(
                "index out of bounds: the len is {} but the index is {}",
                self.len(),
                index
            );
        }
        let ptr = self.arena.alloc(value);
        self.items_mut()[index] = ptr;
    }
}

impl<T> Default for CowVec<T> {
    /// Creates an empty `CowVec`.
    ///
    /// Equivalent to [`CowVec::new()`].
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Clone for CowVec<T> {
    /// Clones this `CowVec` in O(1) time.
    ///
    /// Both the arena and the items vector are shared via `Arc`. The items
    /// vector will be automatically cloned on the first mutation to either
    /// copy (copy-on-write semantics).
    ///
    /// This makes cloning extremely cheap, with the cost of copying the items
    /// vector deferred until (and only if) a mutation occurs.
    fn clone(&self) -> Self {
        Self {
            arena: Arc::clone(&self.arena),
            items: Arc::clone(&self.items),
        }
    }
}

impl<T: fmt::Debug> fmt::Debug for CowVec<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<T> From<Vec<T>> for CowVec<T> {
    /// Creates a `CowVec` from a `Vec`.
    fn from(vec: Vec<T>) -> Self {
        let arena = Arc::new(CowArena::with_capacity(vec.len()));
        let items: Vec<*const T> = vec.into_iter().map(|item| arena.alloc(item)).collect();
        Self {
            arena,
            items: Arc::new(items),
        }
    }
}

impl<T> Index<usize> for CowVec<T> {
    type Output = T;

    /// Returns a reference to the element at the given index.
    ///
    /// # Panics
    /// Panics if `index >= len()`.
    fn index(&self, index: usize) -> &Self::Output {
        self.get(index).expect("index out of bounds")
    }
}

/// # WARNING: HIDDEN ALLOCATION ON EVERY MUTABLE ACCESS
///
/// Unlike `Vec`, mutable indexing on `CowVec` allocates a NEW value in the arena
/// on EVERY access, even if you don't actually modify the value. This is because
/// `CowVec` implements copy-on-write semantics and cannot know at the time of
/// `index_mut()` whether you intend to write.
///
/// ## Examples of Hidden Allocations
///
/// ```
/// use cow_vec::CowVec;
///
/// let mut vec = CowVec::from(vec![1, 2, 3]);
///
/// vec[0] = 5;       // Allocates new value (expected)
/// vec[0] += 1;      // Allocates new value (might be surprising)
/// let _ = &mut vec[0];  // Allocates even if never written to!
///
/// // This loop allocates 100 times:
/// for _ in 0..100 {
///     vec[0] += 1;  // Each iteration allocates
/// }
/// ```
///
/// ## Recommendation
///
/// Prefer using `set()` for mutations - it's explicit about the allocation:
///
/// ```
/// use cow_vec::CowVec;
///
/// let mut vec = CowVec::from(vec![1, 2, 3]);
/// vec.set(0, 5);              // Clear: allocates once
/// vec.set(0, vec[0] + 1);     // Clear: allocates once
/// ```
///
/// Only use `IndexMut` when you need compatibility with code expecting `&mut T`.
impl<T: Clone> IndexMut<usize> for CowVec<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        if index >= self.items.len() {
            panic!(
                "index out of bounds: the len is {} but the index is {}",
                self.len(),
                index
            );
        }
        // Clone the current value to a new arena location (copy-on-write).
        let current = unsafe { &*self.items[index] }.clone();
        let ptr = self.arena.alloc(current);
        self.items_mut()[index] = ptr;
        // SAFETY: The pointer was just allocated and is valid. We have exclusive
        // access via &mut self. The arena allocates mutable memory.
        unsafe { &mut *(ptr as *mut T) }
    }
}