array_list 0.5.0

A chunked ordered collection with iterators and cursors
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
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
use crate::position::Position;
use crate::{ArrayList, Cursor};

/// A mutable cursor over an [`ArrayList`].
///
/// A cursor is like an iterator, except that it can move back and forth without
/// consuming elements. It points either at an element or at the ghost position
/// after the back of the list.
///
/// The ghost position is the cursor equivalent of "past the end": moving next
/// from the ghost wraps to the front, and moving previous from the front moves
/// back to the ghost. Mutation methods keep the cursor on the same logical
/// element when possible.
///
/// # Example
/// ```rust
/// use array_list::ArrayList;
///
/// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 3]);
/// let mut cursor = list.cursor_front_mut();
///
/// cursor.insert_after(2);
/// cursor.move_next();
///
/// assert_eq!(cursor.current(), Some(&mut 2));
/// assert_eq!(cursor.as_list(), &[1, 2, 3]);
/// ```
pub struct CursorMut<'a, T, const N: usize> {
    position: Position,
    list: &'a mut ArrayList<T, N>,
}

const _: [(); core::mem::size_of::<usize>() * 4] =
    [(); core::mem::size_of::<CursorMut<usize, 4>>()];

impl<'a, T, const N: usize> CursorMut<'a, T, N> {
    pub(crate) fn from_front(list: &'a mut ArrayList<T, N>) -> Self {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        Self {
            position: Position::front(list),
            list,
        }
    }

    pub(crate) fn from_back(list: &'a mut ArrayList<T, N>) -> Self {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        Self {
            position: Position::back(list),
            list,
        }
    }

    /// Returns an immutable cursor at the same position.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let cursor = list.cursor_front_mut();
    ///
    /// assert_eq!(cursor.as_cursor().current(), Some(&1));
    /// ```
    pub fn as_cursor(&self) -> Cursor<'_, T, N> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        Cursor::from_position(self.list, self.position)
    }

    /// Returns an immutable reference to the list this cursor was created from.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let cursor = list.cursor_front_mut();
    ///
    /// assert_eq!(cursor.as_list().len(), 2);
    /// ```
    pub fn as_list(&self) -> &ArrayList<T, N> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.list
    }

    /// Returns the last element of the list, if any.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let cursor = list.cursor_front_mut();
    ///
    /// assert_eq!(cursor.back(), Some(&2));
    /// ```
    pub fn back(&self) -> Option<&T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.list.back()
    }

    /// Returns a mutable reference to the last element of the list, if any.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// *cursor.back_mut().unwrap() = 3;
    ///
    /// assert_eq!(cursor.back(), Some(&3));
    /// ```
    pub fn back_mut(&mut self) -> Option<&mut T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.list.back_mut()
    }

    /// Returns a mutable reference to the current element.
    ///
    /// Returns `None` when the cursor is at the ghost position.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// *cursor.current().unwrap() = 10;
    ///
    /// assert_eq!(cursor.front(), Some(&10));
    /// ```
    pub fn current(&mut self) -> Option<&mut T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.position.current_mut(self.list)
    }

    /// Returns the first element of the list, if any.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let cursor = list.cursor_back_mut();
    ///
    /// assert_eq!(cursor.front(), Some(&1));
    /// ```
    pub fn front(&self) -> Option<&T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.list.front()
    }

    /// Returns a mutable reference to the first element of the list, if any.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_back_mut();
    ///
    /// *cursor.front_mut().unwrap() = 0;
    ///
    /// assert_eq!(cursor.front(), Some(&0));
    /// ```
    pub fn front_mut(&mut self) -> Option<&mut T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.list.front_mut()
    }

    /// Returns the current element index.
    ///
    /// Returns `None` when the cursor is at the ghost position.
    ///
    /// The index is the logical element index in the list, not the internal
    /// chunk or slot position.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// assert_eq!(cursor.index(), Some(0));
    /// cursor.move_next();
    /// assert_eq!(cursor.index(), Some(1));
    /// ```
    pub fn index(&self) -> Option<usize> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.position.index(self.list)
    }

    /// Inserts `value` after the current cursor position.
    ///
    /// If the cursor is at the ghost position, the value is inserted at the
    /// front of the list and the cursor remains at the ghost position.
    /// Otherwise, the cursor remains on the same logical element.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 3]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// cursor.insert_after(2);
    ///
    /// assert_eq!(cursor.current(), Some(&mut 1));
    /// assert_eq!(cursor.peek_next(), Some(&mut 2));
    /// ```
    pub fn insert_after(&mut self, value: T) {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        if self.position.is_ghost(self.list) {
            self.push_front(value);
            return;
        }

        self.list.insert_after_position(&mut self.position, value);
    }

    /// Inserts `value` before the current cursor position.
    ///
    /// If the cursor is at the ghost position, the value is inserted at the
    /// back of the list and the cursor remains at the ghost position.
    /// Otherwise, the cursor remains on the same logical element, now shifted
    /// one logical index to the right.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([2, 3]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// cursor.insert_before(1);
    ///
    /// assert_eq!(cursor.current(), Some(&mut 2));
    /// assert_eq!(cursor.peek_prev(), Some(&mut 1));
    /// ```
    pub fn insert_before(&mut self, value: T) {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        if self.position.is_ghost(self.list) {
            self.push_back(value);
            return;
        }

        self.list.insert_before_position(&mut self.position, value);
    }

    /// Moves the cursor to the next position.
    ///
    /// Moving next from the last element moves to the ghost position. Moving
    /// next from the ghost position wraps to the front of the list.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// cursor.move_next();
    ///
    /// assert_eq!(cursor.current(), Some(&mut 2));
    /// ```
    pub fn move_next(&mut self) {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.position.move_next(self.list);
    }

    /// Moves the cursor to the previous position.
    ///
    /// Moving previous from the front element moves to the ghost position.
    /// Moving previous from the ghost position moves to the back of the list.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// cursor.move_prev();
    /// assert_eq!(cursor.index(), None);
    ///
    /// cursor.move_prev();
    /// assert_eq!(cursor.current(), Some(&mut 2));
    /// ```
    pub fn move_prev(&mut self) {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.position.move_prev(self.list);
    }

    /// Returns the next element mutably without moving the cursor.
    ///
    /// When the cursor is at the ghost position, this returns the front
    /// element. When the cursor is at the back element, this returns `None`.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// assert_eq!(cursor.peek_next(), Some(&mut 2));
    /// assert_eq!(cursor.current(), Some(&mut 1));
    /// ```
    pub fn peek_next(&mut self) -> Option<&mut T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.position.peek_next_mut(self.list)
    }

    /// Returns the previous element mutably without moving the cursor.
    ///
    /// When the cursor is at the front element, this returns `None`. When the
    /// cursor is at the ghost position, this returns the back element.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_back_mut();
    ///
    /// assert_eq!(cursor.peek_prev(), Some(&mut 1));
    /// assert_eq!(cursor.current(), Some(&mut 2));
    /// ```
    pub fn peek_prev(&mut self) -> Option<&mut T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        self.position.peek_prev_mut(self.list)
    }

    /// Pushes `value` to the front of the list.
    ///
    /// If the cursor is pointing at an existing element, it continues to point
    /// at that same element after the insertion. If the cursor is at the ghost
    /// position, it remains at the ghost position.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([2]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// cursor.push_front(1);
    ///
    /// assert_eq!(cursor.current(), Some(&mut 2));
    /// assert_eq!(cursor.front(), Some(&1));
    /// ```
    pub fn push_front(&mut self, value: T) {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        let was_ghost = self.position.is_ghost(self.list);
        let chunks_len_backup = self.list.chunks.len();

        self.list.push_front(value);

        if was_ghost {
            self.position = Position::ghost(self.list);
            return;
        }

        self.position.element += 1;

        if self.list.chunks.len() > chunks_len_backup {
            self.position.chunk += 1;
        }

        if self.position.chunk == 0 {
            self.position.slot += 1;
        }
    }

    /// Pushes `value` to the back of the list.
    ///
    /// If the cursor is at the ghost position, it remains at the ghost position.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1]);
    /// let mut cursor = list.cursor_front_mut();
    ///
    /// cursor.push_back(2);
    ///
    /// assert_eq!(cursor.current(), Some(&mut 1));
    /// assert_eq!(cursor.back(), Some(&2));
    /// ```
    pub fn push_back(&mut self, value: T) {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        let was_ghost = self.position.is_ghost(self.list);

        self.list.push_back(value);

        if was_ghost {
            self.position = Position::ghost(self.list);
        }
    }

    /// Removes and returns the front element of the list.
    ///
    /// The cursor is adjusted to keep pointing at the same logical element when
    /// possible. If the removed element is before the cursor, the cursor index
    /// decreases by one.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_back_mut();
    ///
    /// assert_eq!(cursor.pop_front(), Some(1));
    /// assert_eq!(cursor.current(), Some(&mut 2));
    /// assert_eq!(cursor.index(), Some(0));
    /// ```
    pub fn pop_front(&mut self) -> Option<T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        let chunks_len_backup = self.list.chunks.len();

        let out = self.list.pop_front();

        self.position.element = self.position.element.saturating_sub(1);

        if self.position.chunk == 0 {
            self.position.slot = self.position.slot.saturating_sub(1);
        }

        if self.list.chunks.len() < chunks_len_backup {
            self.position.chunk = self.position.chunk.saturating_sub(1);
        }

        out
    }

    /// Removes and returns the back element of the list.
    ///
    /// If the cursor becomes past-the-end, it is adjusted to the ghost position.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2]);
    /// let mut cursor = list.cursor_back_mut();
    ///
    /// assert_eq!(cursor.pop_back(), Some(2));
    /// assert_eq!(cursor.index(), None);
    /// ```
    pub fn pop_back(&mut self) -> Option<T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        let out = self.list.pop_back();

        if self.position.is_ghost(self.list) {
            self.position = Position::ghost(self.list);
        }

        out
    }

    /// Removes and returns the current element.
    ///
    /// Returns `None` when the cursor is at the ghost position. After removal,
    /// the cursor points to the next element, or to the ghost position if the
    /// removed element was the last one.
    ///
    /// # Example
    /// ```rust
    /// use array_list::ArrayList;
    ///
    /// let mut list: ArrayList<i32, 4> = ArrayList::from([1, 2, 3]);
    /// let mut cursor = list.cursor_front_mut();
    /// cursor.move_next();
    ///
    /// assert_eq!(cursor.remove_current(), Some(2));
    /// assert_eq!(cursor.current(), Some(&mut 3));
    /// ```
    pub fn remove_current(&mut self) -> Option<T> {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        if self.position.is_ghost(self.list) {
            return None;
        }

        self.list.remove_at_position(&mut self.position)
    }
}

impl<T, const N: usize> core::fmt::Debug for CursorMut<'_, T, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        const { assert!(N >= crate::MIN_CHUNK_CAPACITY) };

        f.debug_struct("CursorMut")
            .field("position", &self.position)
            .finish()
    }
}

#[cfg(all(test, not(miri)))]
mod tests {
    use crate::ArrayList;
    use alloc::vec::Vec;

    fn values<const N: usize>(list: &ArrayList<i32, N>) -> Vec<i32> {
        list.iter().copied().collect()
    }

    macro_rules! check_capacities {
        ($check:ident) => {{
            $check::<4>();
            $check::<8>();
            $check::<16>();
        }};
    }

    #[test]
    fn current_front_back_and_peeks_can_mutate_neighbors() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([1, 2, 3]);
            let mut cursor = list.cursor_front_mut();

            *cursor.current().unwrap() = 10;
            *cursor.peek_next().unwrap() = 20;
            *cursor.back_mut().unwrap() = 30;
            *cursor.front_mut().unwrap() = 1;

            assert_eq!(cursor.front(), Some(&1));
            assert_eq!(cursor.back(), Some(&30));
            assert_eq!(cursor.as_cursor().current(), Some(&1));
            assert_eq!(values(cursor.as_list()), [1, 20, 30]);
        }

        check_capacities!(check);
    }

    #[test]
    fn insert_before_and_after_keep_cursor_on_current_element() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([2, 4]);
            let mut cursor = list.cursor_front_mut();

            cursor.insert_before(1);
            cursor.insert_after(3);

            assert_eq!(cursor.current(), Some(&mut 2));
            assert_eq!(cursor.peek_prev(), Some(&mut 1));
            assert_eq!(cursor.peek_next(), Some(&mut 3));
            assert_eq!(values(cursor.as_list()), [1, 2, 3, 4]);
        }

        check_capacities!(check);
    }

    #[test]
    fn insert_before_and_after_from_ghost_target_list_edges() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([1, 2]);
            let mut cursor = list.cursor_back_mut();
            cursor.move_next();

            assert_eq!(cursor.index(), None);

            cursor.insert_after(0);
            assert_eq!(cursor.index(), None);
            cursor.insert_before(3);
            assert_eq!(cursor.index(), None);

            assert_eq!(values(cursor.as_list()), [0, 1, 2, 3]);
        }

        check_capacities!(check);
    }

    #[test]
    fn push_front_and_back_adjust_cursor_position() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([2, 3]);
            let mut cursor = list.cursor_front_mut();

            cursor.push_front(1);
            cursor.push_back(4);

            assert_eq!(cursor.current(), Some(&mut 2));
            assert_eq!(cursor.index(), Some(1));
            assert_eq!(values(cursor.as_list()), [1, 2, 3, 4]);
        }

        check_capacities!(check);
    }

    #[test]
    fn pop_front_and_back_adjust_or_ghost_cursor() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([1, 2, 3]);
            let mut cursor = list.cursor_back_mut();

            assert_eq!(cursor.pop_front(), Some(1));
            assert_eq!(cursor.current(), Some(&mut 3));
            assert_eq!(cursor.index(), Some(1));
            assert_eq!(cursor.pop_back(), Some(3));
            assert_eq!(cursor.index(), None);
            assert_eq!(values(cursor.as_list()), [2]);
        }

        check_capacities!(check);
    }

    #[test]
    fn remove_current_moves_to_next_or_ghost() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([1, 2, 3]);
            let mut cursor = list.cursor_front_mut();

            cursor.move_next();
            assert_eq!(cursor.remove_current(), Some(2));
            assert_eq!(cursor.current(), Some(&mut 3));
            assert_eq!(cursor.remove_current(), Some(3));
            assert_eq!(cursor.current(), None);
            assert_eq!(cursor.remove_current(), None);
            assert_eq!(values(cursor.as_list()), [1]);
        }

        check_capacities!(check);
    }

    #[test]
    fn empty_cursor_mutation_edges() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::new();
            let mut cursor = list.cursor_front_mut();

            assert_eq!(cursor.current(), None);
            assert_eq!(cursor.pop_front(), None);
            assert_eq!(cursor.pop_back(), None);
            cursor.push_back(1);
            assert_eq!(cursor.index(), None);
            assert_eq!(cursor.peek_prev(), Some(&mut 1));
            cursor.move_prev();
            assert_eq!(cursor.current(), Some(&mut 1));
        }

        check_capacities!(check);
    }

    #[test]
    fn debug_includes_cursor_mut_position() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([1, 2]);
            let cursor = list.cursor_back_mut();

            let debug = alloc::format!("{cursor:?}");
            assert!(debug.starts_with("CursorMut { position: Position"));
            assert!(debug.contains("element: 1"));
        }

        check_capacities!(check);
    }

    #[test]
    fn mutable_cursor_edits_work_for_required_capacities() {
        fn check<const N: usize>() {
            let mut list = ArrayList::<i32, N>::from([1, 3, 5, 7]);
            let mut cursor = list.cursor_front_mut();

            cursor.insert_before(0);
            cursor.insert_after(2);
            cursor.move_next();
            cursor.move_next();
            cursor.insert_after(4);
            cursor.move_next();
            cursor.move_next();
            cursor.insert_after(6);
            cursor.push_back(8);
            cursor.push_front(-1);

            assert_eq!(cursor.remove_current(), Some(5));
            assert_eq!(cursor.current(), Some(&mut 6));
            assert_eq!(cursor.pop_front(), Some(-1));
            assert_eq!(cursor.pop_back(), Some(8));

            assert_eq!(values(cursor.as_list()), [0, 1, 2, 3, 4, 6, 7]);
        }

        check_capacities!(check);
    }
}