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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
//! Forward scan over a vector with mutation and item removal.
#![no_std]

extern crate alloc;

use alloc::{collections::VecDeque, vec::Vec};
use core::{
    mem,
    ops::{Deref, DerefMut},
    ptr,
};

/// Forward scan over a vector with mutation and item removal.
///
/// Provides an iterator like interface over a vector which allows mutation and removal of items.
///
/// If you need to also add new elements, see [`VecGrowScan`].
///
/// Items are kept in order and every item is moved at most once, even when items are removed.
/// Dropping the `VecMutScan` mid-iteration keeps remaining items in the vector.
///
/// This does not implement the iterator trait, as the returned items borrow from this (i.e. this is
/// a streaming iterator).
///
/// The [`next`](VecMutScan::next) method returns [`VecMutScanItem`] values, which auto dereference
/// to the vector's item type but also provide a [`remove`](VecMutScanItem::remove) and
/// [`replace`](VecMutScanItem::replace) method.
pub struct VecMutScan<'a, T: 'a> {
    vec: &'a mut Vec<T>,
    base: *mut T,
    write: usize,
    read: usize,
    end: usize,
}

// Here is a small overview of how this is implemented, which should aid in auditing this library's
// use of unsafe:
//
// The initial state after taking ownership of the data from `vec` looks like this:
//
//   |0 = write = read          |end
//   [ ][ ][ ][ ][ ][ ][ ][ ][ ]
//
// Calling next without deleting items progresses like this:
//
//   |0 |write = read           |end
//   [ ][ ][ ][ ][ ][ ][ ][ ][ ]
//
//   |0    |write = read        |end
//   [ ][ ][ ][ ][ ][ ][ ][ ][ ]
//                .
//                :
//                           |write = read
//   |0                      |  |end
//   [ ][ ][ ][ ][ ][ ][ ][ ][ ]
//
//   |0                         |end = write = read
//   [ ][ ][ ][ ][ ][ ][ ][ ][ ]
//
// If we are in a state like this and delete an item, we introduce a gap of uninitialized data (as
// we moved it elsewere or dropped it) between write and read:
//
//   |0    |write = read        |end
//   [ ][A][B][C][D][E][ ][ ][ ]
//
//         |write
//   |0    |  |read             |end
//   [ ][A] u [C][D][E][ ][ ][ ]
//
// Calling next in that situation moves items over the gap
//
//            |write
//   |0       |  |read          |end
//   [ ][A][C] u [D][E][ ][ ][ ]
//
// Removing more items widens the gap
//
//            |write
//   |0       |     |read       |end
//   [ ][A][C] u  u [E][ ][ ][ ]
//
// Dropping the `VecMutScan` at that point must move the items in the suffix to close the gap before
// passing ownership back to `vec`.

// TODO replace indices with pointers when pointer offset computation is stabilized should
// benchmarks show an improvement.

impl<'a, T: 'a> VecMutScan<'a, T> {
    /// Begin a scan over a vector with mutation and item removal.
    pub fn new(vec: &mut Vec<T>) -> VecMutScan<T> {
        let base = vec.as_mut_ptr();
        let write = 0;
        let read = 0;
        let end = vec.len();

        // Make sure `vec` is in a consistent state should this `VecMutScan` be leaked. In that case
        // all items within `vec` are also leaked, which is safe. This strategy is also called leak
        // amplification. This can be seen as the `VecMustScan` taking ownership over `vec`'s items,
        // while still keeping them in `vec`'s buffer. As we keep a mutable reference to the `vec`
        // we stop others from messing with its items.
        unsafe {
            vec.set_len(0);
        }

        VecMutScan {
            vec,
            base,
            write,
            read,
            end,
        }
    }

    /// Advance to the next item of the vector.
    ///
    /// This returns a reference wrapper that enables item removal (see [`VecMutScanItem`]).
    #[allow(clippy::should_implement_trait)] // can't be an iterator due to lifetimes
    pub fn next<'s>(&'s mut self) -> Option<VecMutScanItem<'s, 'a, T>> {
        // This just constructs a VecMutScanItem without updating any state. The read and write
        // offsets are adjusted by `VecMutScanItem` whenever it is dropped or one of its
        // self-consuming methods are called.
        if self.read != self.end {
            Some(VecMutScanItem { scan: self })
        } else {
            None
        }
    }

    /// Access the whole vector.
    ///
    /// This provides access to the whole vector at any point during the scan. In general while
    /// scanning, the vector content is not contiguous, thus it is returned as two slices, a prefix
    /// and a suffix. The prefix contains all elements already visited while the suffix contains the
    /// remaining elements starting with the element that will be returned by the following
    /// [`next`][VecMutScan::next] call.
    ///
    /// This method is also present on the [`VecMutScanItem`] reference wrapper returned by
    /// [`next`][VecMutScan::next], allowing access while that wrapper borrows this `VecMutScan`.
    pub fn slices(&self) -> (&[T], &[T]) {
        unsafe {
            // These slices cover the two disjoint parts 0..write and read..end which contain the
            // currently valid data.
            (
                core::slice::from_raw_parts(self.base, self.write),
                core::slice::from_raw_parts(self.base.add(self.read), self.end - self.read),
            )
        }
    }

    /// Access and mutate the whole vector.
    ///
    /// This provides mutable access to the whole vector at any point during the scan. In general
    /// while scanning, the vector content is not contiguous, thus it is returned as two slices, a
    /// prefix and a suffix. The prefix contains all elements already visited while the suffix
    /// contains the remaining elements starting with the element that will be returned by the
    /// following [`next`][VecMutScan::next] call.
    ///
    /// This method is also present on the [`VecMutScanItem`] reference wrapper returned by
    /// [`next`][VecMutScan::next], allowing access while that wrapper borrows this `VecMutScan`.
    pub fn slices_mut(&mut self) -> (&mut [T], &mut [T]) {
        unsafe {
            // These slices cover the two disjoint parts 0..write and read..end which contain the
            // currently valid data.
            (
                core::slice::from_raw_parts_mut(self.base, self.write),
                core::slice::from_raw_parts_mut(self.base.add(self.read), self.end - self.read),
            )
        }
    }
}

impl<'a, T: 'a> Drop for VecMutScan<'a, T> {
    fn drop(&mut self) {
        // When we are dropped, there might be a gap of uninitialized (after dropping) memory
        // between a prefix of non-removed items we iterated over and a suffix of items we did not
        // iterate over. We need to move the suffix to close the gap, so we have a consecutive
        // buffer of items. Then we can safely set `vec`'s length to the total number of remaining
        // items.

        unsafe {
            // The read performed by copy is safe as `self.read..self.end` contains valid data and
            // is within `vec`'s buffer.

            // The write performed by copy is safe as `self.write <= self.read` so
            // `self.write..self.write + suffix_len` also stays within `vec`'s buffer.
            let suffix_len = self.end - self.read;
            // This is required to handle overlapping copies.
            ptr::copy(
                self.base.add(self.read),
                self.base.add(self.write),
                suffix_len,
            );
            // `0..self.write` contained valid data before the copy and the copy also moved valid
            // data to `self.write..self.write + suffix_len`. We took ownership of that data and can
            // safely pass that ownership to `vec` here.
            self.vec.set_len(self.write + suffix_len);
        }
    }
}

/// Reference wrapper that enables item removal for [`VecMutScan`].
pub struct VecMutScanItem<'s, 'a, T: 'a> {
    scan: &'s mut VecMutScan<'a, T>,
}

// When a `VecMutScanItem` is created, there must be valid data at `scan.read` i.e. `scan.read` must
// not have reached `scan.end` yet.

impl<'s, 'a, T: 'a> VecMutScanItem<'s, 'a, T> {
    /// Removes and returns this item from the vector.
    pub fn remove(self) -> T {
        unsafe {
            // Read the next item, taking local ownership of the data to return it.
            let result = ptr::read(self.scan.base.add(self.scan.read));
            // Adjust the read pointer but keep the write pointer to create or widen the gap (see
            // diagrams above).
            self.scan.read += 1;
            // Do not run the `VecMutScanItem`'s drop, as it handles the case for a non-removed item
            // and would perform a now invalid update of the `VecMutScan`.
            mem::forget(self);
            result
        }
    }

    /// Replaces this item with a new value, returns the old value.
    ///
    /// This is equivalent to assigning a new value or calling [`mem::replace`] on the mutable
    /// reference obtained by using [`DerefMut`], but can avoid an intermediate move within the
    /// vector's buffer.
    pub fn replace(self, value: T) -> T {
        unsafe {
            // Read the next item, taking local ownership of the data to return it.
            let result = ptr::read(self.scan.base.add(self.scan.read));

            // Write the replacement in place of the removed item, adjusted for the gap between
            // write and read (see diagrams above).
            ptr::write(self.scan.base.add(self.scan.write), value);
            // Advance the position without changing the width of the gap.
            self.scan.read += 1;
            self.scan.write += 1;
            // Do not run the `VecMutScanItem`'s drop, as it handles the case for a non-replaced
            // item and would perform a now invalid update of the `VecMutScan`.
            mem::forget(self);
            result
        }
    }

    /// Access the whole vector.
    ///
    /// This provides access to the whole vector at any point during the scan. In general while
    /// scanning, the vector content is not contiguous, thus it is returned as two slices, a prefix
    /// and a suffix. The prefix contains all elements already visited while the suffix contains the
    /// remaining elements starting with this element.
    ///
    /// This method is also present on the [`VecMutScan`] borrowed by this reference wrapper,
    /// allowing access without an active `VecMutScanItem`.
    pub fn slices(&self) -> (&[T], &[T]) {
        self.scan.slices()
    }

    /// Access and mutate the whole vector.
    ///
    /// This provides mutable access to the whole vector at any point during the scan. In general
    /// while scanning, the vector content is not contiguous, thus it is returned as two slices, a
    /// prefix and a suffix. The prefix contains all elements already visited while the suffix
    /// contains the remaining elements starting with this element.
    ///
    /// This method is also present on the [`VecMutScan`] borrowed by this reference wrapper,
    /// allowing access without an active `VecMutScanItem`.
    pub fn slices_mut(&mut self) -> (&mut [T], &mut [T]) {
        self.scan.slices_mut()
    }
}

impl<'s, 'a, T: 'a> Deref for VecMutScanItem<'s, 'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        // Within a `VecMutScanItem` the offset `scan.read` contains valid data owned by the
        // `VecMutScan` on which we have a mutable borrow, thus we are allowed to reference it.
        unsafe { &*self.scan.base.add(self.scan.read) }
    }
}

impl<'s, 'a, T: 'a> DerefMut for VecMutScanItem<'s, 'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // Within a `VecMutScanItem` the offset `scan.read` contains valid data owned by the
        // `VecMutScan` on which we have a mutable borrow, thus we are allowed to mutably reference
        // it.
        unsafe { &mut *self.scan.base.add(self.scan.read) }
    }
}

impl<'s, 'a, T: 'a> Drop for VecMutScanItem<'s, 'a, T> {
    fn drop(&mut self) {
        unsafe {
            // Move the item at `scan.read` to `scan.write` i.e. move it over the gap (see diagrams
            // above).
            ptr::copy(
                self.scan.base.add(self.scan.read),
                self.scan.base.add(self.scan.write),
                1,
            );
            // Advance the position without changing the width of the gap.
            self.scan.read += 1;
            self.scan.write += 1;
        }
    }
}

/// Forward scan over a vector with mutation, item insertion and removal.
///
/// Provides an iterator like interface over a vector which allows mutation,
/// inserting new items before or after the current items, and removal of items.
///
/// If you do not need to insert new items, use [`VecMutScan`] instead.
///
/// Internally, the items are kept in the vector in order. When items are removed, a gap of
/// uninitialized memory is created, and the items get moved as the iteration continues. When
/// additional items are inserted, and there is no gap to be filled, the excess is stored in a
/// [`VecDeque`].
///
/// Overall, a linear number of moves is performed, but the exact number varies due to potential
/// reallocations. If no items are inserted, every item is moved at most once.
///
/// Dropping the `VecGrowScan` mid-iteration keeps remaining items in the vector.
///
/// This does not implement the iterator trait, as the returned items borrow from this (i.e. this is
/// a streaming iterator).
///
/// The [`next`](VecGrowScan::next) method returns [`VecGrowScanItem`] values, which auto dereference
/// to the vector's item type but also provide a [`remove`](VecGrowScanItem::remove) and
/// [`replace`](VecGrowScanItem::replace) method.
pub struct VecGrowScan<'a, T: 'a> {
    vec: &'a mut Vec<T>,
    base: *mut T,
    write: usize,
    read: usize,
    end: usize,
    queue: VecDeque<T>,
}

// invariant: if there's a gap in the vector, then the queue is empty.
// corollary: if there are items in the queue, then there is no gap in the vector.

impl<'a, T: 'a> VecGrowScan<'a, T> {
    /// Begin a scan over a vector with mutation, insertion and removal.
    pub fn new(vec: &mut Vec<T>) -> VecGrowScan<T> {
        let base = vec.as_mut_ptr();
        let write = 0;
        let read = 0;
        let end = vec.len();
        let queue = VecDeque::new();

        // Make sure `vec` is in a consistent state should this `VecMutScan` be leaked. In that case
        // all items within `vec` are also leaked, which is safe. This strategy is also called leak
        // amplification. This can be seen as the `VecMustScan` taking ownership over `vec`'s items,
        // while still keeping them in `vec`'s buffer. As we keep a mutable reference to the `vec`
        // we stop others from messing with its items.
        unsafe {
            vec.set_len(0);
        }

        VecGrowScan {
            vec,
            base,
            write,
            read,
            end,
            queue,
        }
    }

    /// Advance to the next item of the vector.
    ///
    /// This returns a reference wrapper that enables item removal (see [`VecGrowScanItem`]).
    #[allow(clippy::should_implement_trait)] // can't be an iterator due to lifetimes
    pub fn next<'s>(&'s mut self) -> Option<VecGrowScanItem<'s, 'a, T>> {
        // This just constructs a VecGrowScanItem without updating any state. The read and write
        // offsets are adjusted by `VecGrowScanItem` whenever it is dropped or one of its
        // self-consuming methods are called.
        if self.read != self.end {
            Some(VecGrowScanItem { scan: self })
        } else {
            None
        }
    }

    /// Insert an item between the items that have been visited, and the items that haven't been
    /// visited yet. Inserted items are not returned during iteration.
    ///
    /// ```
    /// # use vec_mut_scan::VecGrowScan;
    /// let mut numbers = vec![1, 2, 4, 5];
    /// let mut scan = VecGrowScan::new(&mut numbers);
    ///
    /// assert_eq!(*scan.next().unwrap(), 1);
    /// assert_eq!(*scan.next().unwrap(), 2);
    /// scan.insert(3);
    /// assert_eq!(*scan.next().unwrap(), 4);
    /// assert_eq!(*scan.next().unwrap(), 5);
    /// drop(scan);
    ///
    /// assert_eq!(numbers, [1, 2, 3, 4, 5]);
    /// ```
    pub fn insert(&mut self, item: T) {
        if self.write < self.read {
            // The queue is empty by invariant, so this is the right place.
            unsafe {
                ptr::write(self.base.add(self.write), item);
                self.write += 1;
            }
        } else {
            self.queue.push_back(item);
        }
    }

    /// Insert a sequence of items between the items that have been visited, and the items that
    /// haven't been visited yet. Inserted items are not returned during iteration.
    ///
    /// Equivalent to repeatedly calling [`insert`][VecGrowScan::insert], except that reallocations
    /// will be minimized with iterator size hints.
    pub fn insert_many(&mut self, iter: impl IntoIterator<Item = T>) {
        let mut iter = iter.into_iter();
        while self.write < self.read {
            if let Some(item) = iter.next() {
                self.insert(item);
            } else {
                return;
            }
        }

        self.queue.extend(iter);
    }

    /// Access the whole vector.
    ///
    /// This provides access to the whole vector at any point during the scan.
    /// In general while scanning, the vector content is not contiguous, and some of the contents
    /// may be kept out-of-place in a [`VecDeque`]. Thus the content is returned as
    /// four slices. The first three slices, in order, contain all elements already visited, while
    /// the fourth slice contains the remaining elements starting with the element that will be
    /// returned by the following [`next`][VecGrowScan::next] call.
    ///
    /// This method is also present on the [`VecGrowScanItem`] reference wrapper returned by
    /// [`next`][VecGrowScan::next], allowing access while that wrapper borrows this `VecGrowScan`.
    pub fn slices(&self) -> (&[T], &[T], &[T], &[T]) {
        let (mid_l, mid_r) = self.queue.as_slices();
        unsafe {
            // These slices cover the two disjoint parts 0..write and read..end which contain the
            // currently valid data.
            (
                core::slice::from_raw_parts(self.base, self.write),
                mid_l,
                mid_r,
                core::slice::from_raw_parts(self.base.add(self.read), self.end - self.read),
            )
        }
    }

    /// Access and mutate the whole vector.
    ///
    /// This provides mutable access to the whole vector at any point during the scan.
    /// In general while scanning, the vector content is not contiguous, and some of the contents
    /// may be kept out-of-place in a [`VecDeque`]. Thus the content is returned as
    /// four slices. The first three slices, in order, contain all elements already visited, while
    /// the fourth slice contains the remaining elements starting with the element that will be
    /// returned by the following [`next`][VecGrowScan::next] call.
    ///
    /// This method is also present on the [`VecGrowScanItem`] reference wrapper returned by
    /// [`next`][VecGrowScan::next], allowing access while that wrapper borrows this `VecGrowScan`.
    pub fn slices_mut(&mut self) -> (&mut [T], &mut [T], &mut [T], &mut [T]) {
        let (mid_l, mid_r) = self.queue.as_mut_slices();
        unsafe {
            // These slices cover the two disjoint parts 0..write and read..end which contain the
            // currently valid data.
            (
                core::slice::from_raw_parts_mut(self.base, self.write),
                mid_l,
                mid_r,
                core::slice::from_raw_parts_mut(self.base.add(self.read), self.end - self.read),
            )
        }
    }
}

impl<'a, T: 'a> Drop for VecGrowScan<'a, T> {
    fn drop(&mut self) {
        // When we are dropped, there might be a gap of uninitialized (after dropping) memory
        // between a prefix of non-removed items we iterated over and a suffix of items we did not
        // iterate over. We need to move the suffix to close the gap, so we have a consecutive
        // buffer of items. Then we can safely set `vec`'s length to the total number of remaining
        // items.

        if self.queue.is_empty() {
            unsafe {
                // The read performed by copy is safe as `self.read..self.end` contains valid data and
                // is within `vec`'s buffer.

                // The write performed by copy is safe as `self.write <= self.read` so
                // `self.write..self.write + suffix_len` also stays within `vec`'s buffer.
                let suffix_len = self.end - self.read;
                // This is required to handle overlapping copies.
                ptr::copy(
                    self.base.add(self.read),
                    self.base.add(self.write),
                    suffix_len,
                );
                // `0..self.write` contained valid data before the copy and the copy also moved valid
                // data to `self.write..self.write + suffix_len`. We took ownership of that data and can
                // safely pass that ownership to `vec` here.
                self.vec.set_len(self.write + suffix_len);
            }
        } else {
            // By invariant, there is no gap to fix up.
            unsafe {
                self.vec.set_len(self.end);
            }

            self.vec.splice(
                self.write..self.write,
                mem::replace(&mut self.queue, VecDeque::new()).into_iter(),
            );
        }
    }
}

/// Reference wrapper that enables item insertion and removal for [`VecGrowScan`].
#[repr(transparent)]
pub struct VecGrowScanItem<'s, 'a, T: 'a> {
    scan: &'s mut VecGrowScan<'a, T>,
}

// When a `VecGrowScanItem` is created, there must be valid data at `scan.read` i.e. `scan.read` must
// not have reached `scan.end` yet.

impl<'s, 'a, T: 'a> VecGrowScanItem<'s, 'a, T> {
    /// [`remove`][VecGrowScanItem::remove], but without the `mem::forget` at the end. Used to
    /// reduce code duplication.
    unsafe fn remove_deferring_forget(&mut self) -> T {
        // Read the next item, taking local ownership of the data to return it.
        let result = ptr::read(self.scan.base.add(self.scan.read));
        // Adjust the read pointer but keep the write pointer to create or widen the gap (see
        // diagrams above).
        self.scan.read += 1;
        // Attempt to fill the gap with an element from the queue.
        if let Some(dequeued) = self.scan.queue.pop_front() {
            ptr::write(self.scan.base.add(self.scan.write), dequeued);
            self.scan.write += 1;
        }
        result
    }

    /// The action of drop, but without actually consuming the item, so that the underlying
    /// reference may still get used.
    unsafe fn advance_deferring_forget(&mut self) {
        if self.scan.read != self.scan.write {
            // Move the item at `scan.read` to `scan.write` i.e. move it over the gap (see diagrams
            // above).
            // Copy is nonoverlapping by if condition.
            ptr::copy_nonoverlapping(
                self.scan.base.add(self.scan.read),
                self.scan.base.add(self.scan.write),
                1,
            );
            // Advance the position without changing the width of the gap.
            self.scan.read += 1;
            self.scan.write += 1;
        } else if let Some(dequeued) = self.scan.queue.pop_front() {
            // Move the item at `scan.read` into the queue.
            self.scan
                .queue
                .push_back(ptr::read(self.scan.base.add(self.scan.read)));
            // Move the dequeued item into that same slot.
            ptr::write(self.scan.base.add(self.scan.write), dequeued);
            // Advance the position of the (zero-sized) gap.
            self.scan.read += 1;
            self.scan.write += 1;
        } else {
            self.scan.read += 1;
            self.scan.write += 1;
        }
    }

    fn into_inner_forget(self) -> &'s mut VecGrowScan<'a, T> {
        // You'd think this is possible without unsafe, or at least using less of it. However, as
        // you cannot destructure structs implementing Drop, I don't see any way to do it.
        // cf. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d1fbed6f3a28bd7983f62ea3b67c9822
        unsafe {
            // This is safe, as `VecGrowScanItem` is annotated with #[repr(transparent)]
            mem::transmute(self)
        }
    }

    fn into_inner(mut self) -> &'s mut VecGrowScan<'a, T> {
        unsafe {
            self.advance_deferring_forget();
            self.into_inner_forget()
        }
    }

    /// Removes and returns this item from the vector.
    pub fn remove(mut self) -> T {
        unsafe {
            let result = self.remove_deferring_forget();
            mem::forget(self);
            result
        }
    }

    /// Replaces this item with a new value, returns the old value.
    ///
    /// This is equivalent to assigning a new value or calling [`mem::replace`] on the mutable
    /// reference obtained by using [`DerefMut`], but can avoid an intermediate move within the
    /// vector's buffer.
    pub fn replace(mut self, value: T) -> T {
        unsafe {
            let result = self.remove_deferring_forget();
            self.scan.insert(value);
            mem::forget(self);
            result
        }
    }

    // NOTE: in the following functions, take special care to behave properly when a callback
    // (including the iterator) panics.

    /// Replace the current item with a sequence of items. Returns the replaced item.
    pub fn replace_with_many(mut self, values: impl IntoIterator<Item = T>) -> T {
        let result = unsafe { self.remove_deferring_forget() };
        let scan = self.into_inner_forget();

        scan.insert_many(values);
        result
    }

    /// Like [`replace`][VecGrowScanItem::replace], but compute the replacement value with
    /// ownership of the removed item.
    pub fn replace_with(mut self, f: impl FnOnce(T) -> T) {
        let removed = unsafe { self.remove_deferring_forget() };
        let scan = self.into_inner_forget();

        scan.insert(f(removed));
    }

    /// Like [`replace_with_many`][VecGrowScanItem::replace_with_many], but compute the replacement
    /// sequence with ownership of the removed item.
    pub fn replace_with_many_with<F, I>(mut self, f: F)
    where
        F: FnOnce(T) -> I,
        I: IntoIterator<Item = T>,
    {
        let removed = unsafe { self.remove_deferring_forget() };
        let scan = self.into_inner_forget();

        scan.insert_many(f(removed));
    }

    /// Insert an item before the current item.
    pub fn insert_before(&mut self, value: T) {
        self.scan.insert(value);
    }

    /// Insert a sequence of items before the current item.
    ///
    /// Equivalent to repeatedly calling [`insert_before`][VecGrowScanItem::insert_before], except
    /// that reallocations will be minimized with iterator size hints.
    pub fn insert_many_before(&mut self, values: impl IntoIterator<Item = T>) {
        self.scan.insert_many(values);
    }

    /// Insert an item after the current item. Inserted items are not returned during iteration.
    ///
    /// Note that this consumes the `VecGrowScanItem`, as it is necessary to commit that the
    /// current item will not be removed. If you need to insert multiple elements, you can either
    /// use [`insert_many_after`][VecGrowScanItem::insert_many_after], or use
    /// [`VecGrowScan::insert`] after you drop this `VecGrowScanItem`.
    pub fn insert_after(self, value: T) {
        self.into_inner().insert(value);
    }

    /// Insert a sequence of items after the current item. Inserted items are not returned during iteration.
    ///
    /// Note that this consumes the `VecGrowScanItem`, as it is necessary to commit that the
    /// current item will not be removed. If you need to insert more elements, you can use
    /// [`VecGrowScan::insert`] (or [`insert_many`][VecGrowScan::insert_many]) after you drop this
    /// `VecGrowScanItem`.
    pub fn insert_many_after(self, values: impl IntoIterator<Item = T>) {
        self.into_inner().insert_many(values)
    }

    /// Access the whole vector.
    ///
    /// This provides access to the whole vector at any point during the scan.
    /// In general while scanning, the vector content is not contiguous, and some of the contents
    /// may be kept out-of-place in a [`VecDeque`]. Thus the content is returned as
    /// four slices. The first three slices, in order, contain all elements already visited, while
    /// the fourth slice contains the remaining elements starting with this element.
    ///
    /// This method is also present on the [`VecGrowScan`] borrowed by this reference wrapper,
    /// allowing access without an active `VecGrowScanItem`.
    pub fn slices(&self) -> (&[T], &[T], &[T], &[T]) {
        self.scan.slices()
    }

    /// Access and mutate the whole vector.
    ///
    /// This provides mutable access to the whole vector at any point during the scan.
    /// In general while scanning, the vector content is not contiguous, and some of the contents
    /// may be kept out-of-place in a [`VecDeque`]. Thus the content is returned as
    /// four slices. The first three slices, in order, contain all elements already visited, while
    /// the fourth slice contains the remaining elements starting with this element.
    ///
    /// This method is also present on the [`VecGrowScan`] borrowed by this reference wrapper,
    /// allowing access without an active `VecGrowScanItem`.
    pub fn slices_mut(&mut self) -> (&mut [T], &mut [T], &mut [T], &mut [T]) {
        self.scan.slices_mut()
    }
}

impl<'s, 'a, T: 'a> Deref for VecGrowScanItem<'s, 'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        // Within a `VecGrowScanItem` the offset `scan.read` contains valid data owned by the
        // `VecGrowScan` on which we have a mutable borrow, thus we are allowed to reference it.
        unsafe { &*self.scan.base.add(self.scan.read) }
    }
}

impl<'s, 'a, T: 'a> DerefMut for VecGrowScanItem<'s, 'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // Within a `VecGrowScanItem` the offset `scan.read` contains valid data owned by the
        // `VecGrowScan` on which we have a mutable borrow, thus we are allowed to mutably reference
        // it.
        unsafe { &mut *self.scan.base.add(self.scan.read) }
    }
}

impl<'s, 'a, T: 'a> Drop for VecGrowScanItem<'s, 'a, T> {
    fn drop(&mut self) {
        unsafe {
            self.advance_deferring_forget();
        }
    }
}

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

    use alloc::{boxed::Box, rc::Rc, vec};

    #[test]
    fn check_item_drops() {
        let mut input: Vec<_> = vec![0, 1, 2, 3, 4, 5, 6, 7]
            .into_iter()
            .map(Rc::new)
            .collect();
        let input_copy = input.clone();

        let mut scan = VecMutScan::new(&mut input);

        let mut keep = None;
        let mut also_keep = None;

        while let Some(item) = scan.next() {
            if **item == 2 {
                item.replace(Rc::new(10));
            } else if **item == 3 {
                keep = Some(item.remove());
            } else if **item == 4 {
                item.remove();
            } else if **item == 5 {
                also_keep = Some(item.replace(Rc::new(20)));
            } else if **item == 6 {
                break;
            }
        }

        let _keep_copy = keep.clone();
        let _also_keep_copy_1 = also_keep.clone();
        let _also_keep_copy_2 = also_keep.clone();

        let ref_counts: Vec<_> = input_copy.iter().map(Rc::strong_count).collect();

        assert_eq!(ref_counts, vec![2, 2, 1, 3, 1, 4, 2, 2]);
        assert_eq!(keep.map(|rc| Rc::strong_count(&rc)), Some(3));
        assert_eq!(also_keep.map(|rc| Rc::strong_count(&rc)), Some(4));
    }

    #[test]
    fn check_slices() {
        let mut input: Vec<_> = (0..16).collect();

        let mut scan = VecMutScan::new(&mut input);

        loop {
            let value;
            match scan.next() {
                None => break,
                Some(item) => {
                    value = *item;
                    let (a, b) = item.slices();
                    assert!(a.iter().all(|i| *i < value && *i % 2 != 0));
                    assert!(b.iter().all(|i| *i >= value));

                    if value % 2 == 0 {
                        item.remove();
                    } else {
                        drop(item);
                    }
                }
            }
            if value % 2 != 0 {
                assert_eq!(scan.slices().0.last().unwrap(), &value);
            }
            if let Some(&first) = scan.slices().1.first() {
                assert_eq!(first, value + 1);
            }
        }
    }

    #[test]
    fn check_slices_mut() {
        let mut input = b"foo bar baz".to_vec();

        let mut scan = VecMutScan::new(&mut input);

        while let Some(mut value) = scan.next() {
            if *value == b' ' {
                let suffix = value.slices_mut().1;
                if suffix.len() > 1 {
                    suffix[1] = suffix[1].to_ascii_uppercase();
                }
                value.remove();
            }
        }

        drop(scan);

        assert_eq!(input, b"fooBarBaz");
    }

    #[test]
    fn grow_check_item_drops() {
        let mut input: Vec<_> = vec![0, 1, 2, 3, 4, 5, 6, 7]
            .into_iter()
            .map(Rc::new)
            .collect();
        let input_copy = input.clone();

        let mut scan = VecGrowScan::new(&mut input);

        let mut keep = None;
        let mut also_keep = None;

        while let Some(item) = scan.next() {
            if **item == 2 {
                item.replace(Rc::new(10));
            } else if **item == 3 {
                keep = Some(item.remove());
            } else if **item == 4 {
                item.remove();
            } else if **item == 5 {
                also_keep = Some(item.replace(Rc::new(20)));
            } else if **item == 6 {
                break;
            }
        }

        let _keep_copy = keep.clone();
        let _also_keep_copy_1 = also_keep.clone();
        let _also_keep_copy_2 = also_keep.clone();

        let ref_counts: Vec<_> = input_copy.iter().map(Rc::strong_count).collect();

        assert_eq!(ref_counts, vec![2, 2, 1, 3, 1, 4, 2, 2]);
        assert_eq!(keep.map(|rc| Rc::strong_count(&rc)), Some(3));
        assert_eq!(also_keep.map(|rc| Rc::strong_count(&rc)), Some(4));
    }

    #[test]
    fn grow_check_slices_no_insert() {
        let mut input: Vec<_> = (0..16).collect();

        let mut scan = VecGrowScan::new(&mut input);

        loop {
            let value;
            match scan.next() {
                None => break,
                Some(item) => {
                    value = *item;
                    let (a, .., b) = item.slices();
                    assert!(a.iter().all(|i| *i < value && *i % 2 != 0));
                    assert!(b.iter().all(|i| *i >= value));

                    if value % 2 == 0 {
                        item.remove();
                    } else {
                        drop(item);
                    }
                }
            }
            if value % 2 != 0 {
                assert_eq!(scan.slices().0.last().unwrap(), &value);
            }
            if let Some(&first) = scan.slices().3.first() {
                assert_eq!(first, value + 1);
            }
        }
    }

    #[test]
    fn grow_check_slices_mut() {
        let mut input = b"foo bar baz".to_vec();

        let mut scan = VecGrowScan::new(&mut input);

        while let Some(mut value) = scan.next() {
            if *value == b' ' {
                let suffix = value.slices_mut().3;
                if suffix.len() > 1 {
                    suffix[1] = suffix[1].to_ascii_uppercase();
                }
                value.remove();
            }
        }

        drop(scan);

        assert_eq!(input, b"fooBarBaz");
    }

    #[test]
    fn insert_after() {
        let mut nums = vec![1, 2, 4, 5];
        let mut scan = VecGrowScan::new(&mut nums);

        while let Some(value) = scan.next() {
            if *value == 2 {
                value.insert_after(3);
            }
        }

        drop(scan);

        assert_eq!(nums, [1, 2, 3, 4, 5]);
    }

    #[test]
    fn insert_before() {
        let mut nums = vec![1, 3, 4, 5];
        let mut scan = VecGrowScan::new(&mut nums);

        while let Some(mut value) = scan.next() {
            if *value == 3 {
                value.insert_before(2);
            }
        }

        drop(scan);

        assert_eq!(nums, [1, 2, 3, 4, 5]);
    }

    #[test]
    fn drop_after_partial_scan_with_inserts() {
        let mut nums = vec![1, 2, 5, 6];
        let mut scan = VecGrowScan::new(&mut nums);

        while let Some(mut value) = scan.next() {
            if *value > 2 {
                value.insert_many_before([3, 4].iter().copied());
                break;
            }
        }

        drop(scan);

        assert_eq!(nums, [1, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn replace_with() {
        let mut vec = (1..=5).map(Box::new).collect();
        let mut scan = VecGrowScan::new(&mut vec);

        while let Some(value) = scan.next() {
            if **value % 2 == 0 {
                value.replace_with(|x| Box::new(*x * 2));
            }
        }

        drop(scan);

        assert_eq!(
            vec,
            [
                Box::new(1),
                Box::new(4),
                Box::new(3),
                Box::new(8),
                Box::new(5),
            ]
        );
    }

    #[test]
    fn replace_with_many_with() {
        let mut vec = vec![3, 6, 9, 12];
        let mut scan = VecGrowScan::new(&mut vec);

        while let Some(value) = scan.next() {
            value.replace_with_many_with(|x| vec![x - 1, x, x + 1]);
        }

        drop(scan);

        assert_eq!(vec, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
    }
}