nami-core 0.5.0

A powerful, lightweight reactive framework.
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
//! What a collection notification changed.

use core::ops::Range;

use alloc::{boxed::Box, vec::Vec};

use super::diff::diff;

/// One step of a [`CollectionChange`].
///
/// Indices follow batch-update semantics, the model `UIKit`'s
/// `performBatchUpdates` and diffable data sources use: removals and the
/// source of a move name positions in the *previous* snapshot; insertions,
/// updates and the destination of a move name positions in the *new*
/// snapshot. Edits are not applied one after another; every index refers to
/// one of the two snapshots as a whole.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Edit {
    /// The items at this range of the previous snapshot no longer exist.
    Remove(Range<usize>),
    /// The items at this range of the new snapshot did not exist before.
    Insert(Range<usize>),
    /// The item at `from` in the previous snapshot is now at `to` in the new
    /// snapshot, and its order relative to the other surviving items changed.
    ///
    /// Items that only shift because of insertions or removals around them
    /// are not reported as moves.
    Move {
        /// Position in the previous snapshot.
        from: usize,
        /// Position in the new snapshot.
        to: usize,
    },
    /// The items at this range of the new snapshot kept their identity but
    /// their content may differ.
    ///
    /// An item can be both moved and updated; it then appears in a
    /// [`Edit::Move`] and in an [`Edit::Update`] covering its new position.
    Update(Range<usize>),
}

impl Edit {
    /// Sort key of the canonical edit order: removals, insertions, moves,
    /// updates, each ascending by position.
    const fn order(&self) -> (u8, usize) {
        match self {
            Self::Remove(range) => (0, range.start),
            Self::Insert(range) => (1, range.start),
            Self::Move { to, .. } => (2, *to),
            Self::Update(range) => (3, range.start),
        }
    }

    const fn range_mut(&mut self) -> Option<&mut Range<usize>> {
        match self {
            Self::Remove(range) | Self::Insert(range) | Self::Update(range) => Some(range),
            Self::Move { .. } => None,
        }
    }

    const fn is_empty(&self) -> bool {
        match self {
            Self::Remove(range) | Self::Insert(range) | Self::Update(range) => {
                range.start >= range.end
            }
            Self::Move { .. } => false,
        }
    }
}

/// What a collection notification changed, aligned by item identity.
///
/// A notification carries the new snapshot together with the edits that turn
/// the previous snapshot into it; see [`Edit`] for which snapshot each index
/// refers to. Indices are collection-wide: a watcher subscribed to a
/// sub-range still receives positions relative to index 0 of the whole
/// collection and intersects them with its range itself.
///
/// Collections compute the change by item id (see
/// [`Identifiable::Id`](super::Identifiable::Id)): a reordered or front-inserted
/// replacement reports moves and insertions instead of rewriting every
/// position.
///
/// A change made of a single edit is stored inline, so the common mutations
/// (`push`, `insert`, `remove`, `set`) do not allocate for it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct CollectionChange {
    repr: Repr,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
enum Repr {
    #[default]
    Empty,
    One(Edit),
    /// Two or more edits in canonical order.
    Many(Box<[Edit]>),
}

impl CollectionChange {
    /// A notification that touched nothing.
    #[must_use]
    pub const fn unchanged() -> Self {
        Self { repr: Repr::Empty }
    }

    /// `range` of the new snapshot was inserted.
    #[must_use]
    pub fn inserted(range: Range<usize>) -> Self {
        Self::from(Edit::Insert(range))
    }

    /// `range` of the previous snapshot was removed.
    #[must_use]
    pub fn removed(range: Range<usize>) -> Self {
        Self::from(Edit::Remove(range))
    }

    /// The items at `range` of the new snapshot were updated in place.
    #[must_use]
    pub fn updated(range: Range<usize>) -> Self {
        Self::from(Edit::Update(range))
    }

    /// The item at `from` (previous snapshot) moved to `to` (new snapshot).
    #[must_use]
    pub fn moved(from: usize, to: usize) -> Self {
        Self::from(Edit::Move { from, to })
    }

    /// `len` items appear from nothing starting at `start`, for example to
    /// seed a view with contents read through [`Collection::get_item`](crate::collection::Collection::get_item).
    #[must_use]
    pub fn populated(start: usize, len: usize) -> Self {
        Self::inserted(start..start + len)
    }

    /// Builds a change from edits in any order.
    ///
    /// Empty ranges are dropped, the edits are sorted into canonical order
    /// (removals, insertions, moves, updates, each ascending) and adjacent or
    /// overlapping ranges of the same kind are merged, so two changes that
    /// describe the same edits compare equal.
    #[must_use]
    pub fn from_edits(edits: impl IntoIterator<Item = Edit>) -> Self {
        let mut edits: Vec<Edit> = edits.into_iter().filter(|edit| !edit.is_empty()).collect();
        edits.sort_by_key(Edit::order);
        let mut merged: Vec<Edit> = Vec::with_capacity(edits.len());
        for mut edit in edits {
            if let Some(last) = merged.last_mut()
                && last.order().0 == edit.order().0
                && let (Some(previous), Some(next)) = (last.range_mut(), edit.range_mut())
                && next.start <= previous.end
            {
                previous.end = previous.end.max(next.end);
                continue;
            }
            merged.push(edit);
        }
        if merged.len() > 1 {
            return Self {
                repr: Repr::Many(merged.into_boxed_slice()),
            };
        }
        merged.pop().map_or_else(Self::unchanged, |edit| Self {
            repr: Repr::One(edit),
        })
    }

    /// The edits in canonical order: removals, insertions, moves, updates,
    /// each ascending by position.
    #[must_use]
    pub fn edits(&self) -> &[Edit] {
        match &self.repr {
            Repr::Empty => &[],
            Repr::One(edit) => core::slice::from_ref(edit),
            Repr::Many(edits) => edits,
        }
    }

    /// True when the notification touched nothing.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        matches!(self.repr, Repr::Empty)
    }

    /// Ranges of the previous snapshot that were removed, ascending.
    pub fn removals(&self) -> impl Iterator<Item = Range<usize>> + '_ {
        self.edits().iter().filter_map(|edit| match edit {
            Edit::Remove(range) => Some(range.clone()),
            _ => None,
        })
    }

    /// Ranges of the new snapshot that were inserted, ascending.
    pub fn insertions(&self) -> impl Iterator<Item = Range<usize>> + '_ {
        self.edits().iter().filter_map(|edit| match edit {
            Edit::Insert(range) => Some(range.clone()),
            _ => None,
        })
    }

    /// Moves as `(from, to)` pairs, ascending by destination.
    pub fn moves(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        self.edits().iter().filter_map(|edit| match edit {
            Edit::Move { from, to } => Some((*from, *to)),
            _ => None,
        })
    }

    /// Ranges of the new snapshot updated in place, ascending.
    pub fn updates(&self) -> impl Iterator<Item = Range<usize>> + '_ {
        self.edits().iter().filter_map(|edit| match edit {
            Edit::Update(range) => Some(range.clone()),
            _ => None,
        })
    }

    /// Whether the item at `index` of the new snapshot was updated in place.
    #[must_use]
    pub fn is_updated(&self, index: usize) -> bool {
        self.updates().any(|range| range.contains(&index))
    }

    /// Applies the structural part of this change to `items`, a vector
    /// parallel to the previous snapshot, leaving it parallel to the new
    /// one.
    ///
    /// Removed elements are dropped, moved and untouched elements are carried
    /// to their new positions, and every inserted position is filled by
    /// `insert(new_index)`. Updates are not applied: the caller refreshes the
    /// positions [`Self::updates`] names.
    ///
    /// This is how a consumer that keeps per-item state (views, native
    /// cells) follows a collection without diffing it.
    ///
    /// # Panics
    ///
    /// Panics if the change names positions `items` does not have, which
    /// means it was not computed against a snapshot of `items`' length.
    pub fn apply<U>(&self, items: &mut Vec<U>, mut insert: impl FnMut(usize) -> U) {
        if self.is_empty() {
            return;
        }
        let removed: usize = self.removals().map(|range| range.len()).sum();
        let inserted: usize = self.insertions().map(|range| range.len()).sum();
        let new_len = (items.len() + inserted)
            .checked_sub(removed)
            .expect("the change removes more items than the collection had");

        let mut slots: Vec<Option<U>> = items.drain(..).map(Some).collect();
        for range in self.removals() {
            for slot in &mut slots[range] {
                *slot = None;
            }
        }
        // Moves come out in ascending destination order.
        let mut moved = self
            .moves()
            .map(|(from, to)| {
                let item = slots[from]
                    .take()
                    .expect("a moved item must exist in the previous snapshot");
                (to, item)
            })
            .collect::<Vec<_>>()
            .into_iter()
            .peekable();
        let mut survivors = slots.into_iter().flatten();
        let mut insertions = self.insertions().peekable();

        items.reserve(new_len);
        for index in 0..new_len {
            while insertions.next_if(|range| range.end <= index).is_some() {}
            if insertions
                .peek()
                .is_some_and(|range| range.contains(&index))
            {
                items.push(insert(index));
            } else if let Some((_, item)) = moved.next_if(|(to, _)| *to == index) {
                items.push(item);
            } else {
                items.push(
                    survivors
                        .next()
                        .expect("the change does not match the collection it describes"),
                );
            }
        }
    }
}

impl From<Edit> for CollectionChange {
    fn from(edit: Edit) -> Self {
        if edit.is_empty() {
            Self::unchanged()
        } else {
            Self {
                repr: Repr::One(edit),
            }
        }
    }
}

/// Where the item at a tracked position came from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Origin {
    /// The item was at this position when the log started.
    Old(usize),
    /// The item was inserted by a recorded change; a fresh counter keeps it
    /// distinct from initial items and from other inserts.
    New(usize),
}

/// One position of the collection a [`ChangeLog`] tracks.
#[derive(Clone, Copy, Debug)]
struct Tag {
    origin: Origin,
    /// Whether a recorded change reported the item at this position as
    /// updated.
    updated: bool,
}

/// The tags of a collection of `len` items that no change has touched yet.
fn initial_tags(len: usize) -> Vec<Tag> {
    (0..len)
        .map(|index| Tag {
            origin: Origin::Old(index),
            updated: false,
        })
        .collect()
}

/// Accumulates the [`CollectionChange`]s made to one collection and
/// composes them into the single change from the first snapshot to the
/// last.
///
/// A collection that holds back its notifications — batched updates, for
/// example — pushes each change it makes and, when it finally notifies,
/// sends [`Self::finish`] instead of the individual changes.
///
/// Items are tracked by where they came from, never by value, so no bound
/// on the item type is involved and a remove followed by an insert of an
/// equal value stays a remove plus an insert. Moves are composed through
/// [`diff`](crate::collection::diff)'s longest increasing subsequence, so
/// the result reports as few as possible.
///
/// Each pushed change is in the indices of the collection as it was right
/// before that change; see [`Edit`] for which snapshot each index of an
/// edit refers to.
///
/// The tracker is only allocated when the first non-empty change is
/// pushed: a log that records nothing allocates nothing.
#[derive(Clone, Debug)]
pub struct ChangeLog {
    /// The collection's length when the log started.
    len: usize,
    /// One tag per position of the current snapshot; `None` until the
    /// first push.
    current: Option<Vec<Tag>>,
    /// The next fresh [`Origin::New`] tag.
    fresh: usize,
}

impl ChangeLog {
    /// Starts a log for a collection of `len` items.
    #[must_use]
    pub const fn new(len: usize) -> Self {
        Self {
            len,
            current: None,
            fresh: 0,
        }
    }

    /// Whether no change has been recorded yet.
    ///
    /// This is about pushes, not the outcome: changes that later ones undo
    /// still count as recorded, and [`Self::finish`] then returns an empty
    /// [`CollectionChange`].
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.current.is_none()
    }

    /// Records `change`, expressed in the indices of the collection as it
    /// is after the previously recorded changes.
    ///
    /// # Panics
    ///
    /// Panics if `change` names positions the collection does not have,
    /// like [`CollectionChange::apply`].
    pub fn push(&mut self, change: &CollectionChange) {
        if change.is_empty() {
            return;
        }
        let current = self.current.get_or_insert_with(|| initial_tags(self.len));
        change.apply(current, |_| {
            let tag = Tag {
                origin: Origin::New(self.fresh),
                updated: false,
            };
            self.fresh += 1;
            tag
        });
        // `updates()` names positions of the new snapshot, which `apply`
        // has just left `current` parallel to, moved items included.
        for range in change.updates() {
            for tag in &mut current[range] {
                tag.updated = true;
            }
        }
    }

    /// The single change from the snapshot the log started on to the
    /// current one.
    #[must_use]
    pub fn finish(self) -> CollectionChange {
        let Some(current) = self.current else {
            return CollectionChange::unchanged();
        };
        let initial = initial_tags(self.len);
        diff(
            &initial,
            &current,
            |_, tag| tag.origin,
            |_, new| !new.updated,
        )
    }
}

#[cfg(test)]
mod tests {
    use alloc::{vec, vec::Vec};

    use super::{ChangeLog, CollectionChange, Edit};

    #[test]
    fn from_edits_sorts_and_merges() {
        let change = CollectionChange::from_edits([
            Edit::Update(4..5),
            Edit::Insert(1..2),
            Edit::Remove(3..4),
            Edit::Insert(0..1),
            Edit::Remove(2..3),
            Edit::Insert(5..5),
        ]);
        assert_eq!(
            change.edits(),
            [Edit::Remove(2..4), Edit::Insert(0..2), Edit::Update(4..5)]
        );
    }

    #[test]
    fn single_edits_and_empty_ranges() {
        assert!(CollectionChange::inserted(3..3).is_empty());
        assert_eq!(
            CollectionChange::from_edits([Edit::Insert(0..1)]),
            CollectionChange::inserted(0..1)
        );
    }

    #[test]
    fn apply_follows_removals_insertions_and_moves() {
        // previous: a b c d e  ->  new: x d a c e   (b removed, x inserted, d moved)
        let change = CollectionChange::from_edits([
            Edit::Remove(1..2),
            Edit::Insert(0..1),
            Edit::Move { from: 3, to: 1 },
        ]);
        let mut items = vec!['a', 'b', 'c', 'd', 'e'];
        change.apply(&mut items, |_| 'x');
        assert_eq!(items, vec!['x', 'd', 'a', 'c', 'e']);
    }

    #[test]
    fn apply_on_empty_change_is_a_no_op() {
        let mut items: Vec<u8> = vec![1, 2, 3];
        CollectionChange::unchanged().apply(&mut items, |_| unreachable!());
        assert_eq!(items, vec![1, 2, 3]);
    }

    /// A test item: `tag` identifies where the item came from — below the
    /// initial length an initial position, at or above it an inserted one —
    /// and `updates` counts the updates it lived through.
    #[derive(Clone, Copy, Debug, PartialEq)]
    struct Item {
        tag: usize,
        updates: usize,
    }

    /// Every structurally valid single-edit change on a collection of
    /// `len` items — insert, remove, update and move at every valid
    /// position or range — plus a few multi-edit changes as `from_edits`
    /// merges them.
    fn changes_on(len: usize) -> Vec<CollectionChange> {
        let mut changes = Vec::new();
        for start in 0..=len {
            for size in 1..=2 {
                changes.push(CollectionChange::inserted(start..start + size));
            }
        }
        for start in 0..len {
            for end in start + 1..=len {
                changes.push(CollectionChange::removed(start..end));
                changes.push(CollectionChange::updated(start..end));
            }
        }
        for from in 0..len {
            for to in 0..len {
                if from != to {
                    changes.push(CollectionChange::moved(from, to));
                }
            }
        }
        changes.push(CollectionChange::from_edits([
            Edit::Insert(0..1),
            Edit::Update(0..1),
        ]));
        if len >= 1 {
            changes.push(CollectionChange::from_edits([
                Edit::Remove(0..1),
                Edit::Insert(len - 1..len),
            ]));
            changes.push(CollectionChange::from_edits([
                Edit::Remove(len - 1..len),
                Edit::Insert(0..1),
            ]));
        }
        if len >= 2 {
            changes.push(CollectionChange::from_edits([
                Edit::Remove(0..1),
                Edit::Update(0..1),
            ]));
            changes.push(CollectionChange::from_edits([
                Edit::Move {
                    from: 0,
                    to: len - 1,
                },
                Edit::Update(len - 1..len),
            ]));
            changes.push(CollectionChange::from_edits([
                Edit::Insert(0..1),
                Edit::Remove(len - 1..len),
            ]));
        }
        changes
    }

    /// Applies `change` to `items`: inserted positions get a fresh tag,
    /// updated positions count up.
    fn apply_one(items: &mut Vec<Item>, change: &CollectionChange, next_tag: &mut usize) {
        change.apply(items, |_| {
            let tag = *next_tag;
            *next_tag += 1;
            Item { tag, updates: 0 }
        });
        for range in change.updates() {
            for item in &mut items[range] {
                item.updates += 1;
            }
        }
    }

    /// The composed change applied to `initial` must reproduce `current`,
    /// with updates reported for exactly the surviving items the sequence
    /// updated — an inserted item is reported as an insert, never an
    /// update.
    fn check(initial: &[Item], current: &[Item], sequence: &[CollectionChange]) {
        let mut log = ChangeLog::new(initial.len());
        for change in sequence {
            log.push(change);
        }
        assert_eq!(log.is_empty(), sequence.is_empty());
        let result = log.finish();

        // `apply` fills inserted positions in ascending order, and the
        // surviving inserted items are exactly the fresh-tag items of the
        // final snapshot, in order.
        let mut inserted = current
            .iter()
            .filter(|item| item.tag >= initial.len())
            .copied();
        let mut composed = initial.to_vec();
        result.apply(&mut composed, |_| {
            inserted
                .next()
                .expect("the composed change inserts an item the sequence never made")
        });
        assert!(inserted.next().is_none(), "{result:?}");
        for range in result.updates() {
            composed[range.clone()].copy_from_slice(&current[range]);
        }
        assert_eq!(composed, current, "{sequence:?} composed to {result:?}");
        for (index, item) in current.iter().enumerate() {
            assert_eq!(
                result.is_updated(index),
                item.tag < initial.len() && item.updates > 0,
                "position {index} of {sequence:?} composed to {result:?}"
            );
        }
        assert_eq!(result.is_empty(), current == initial, "{result:?}");
    }

    /// Runs `check` for every sequence of up to `depth` changes starting
    /// from `items`, returning how many sequences were checked.
    fn enumerate(
        initial: &[Item],
        items: &[Item],
        sequence: &mut Vec<CollectionChange>,
        next_tag: &mut usize,
        depth: usize,
    ) -> usize {
        let mut sequences = 0;
        for change in changes_on(items.len()) {
            let mut next_items = items.to_vec();
            apply_one(&mut next_items, &change, next_tag);
            sequence.push(change);
            sequences += 1;
            check(initial, &next_items, sequence);
            if depth > 1 {
                sequences += enumerate(initial, &next_items, sequence, next_tag, depth - 1);
            }
            sequence.pop();
        }
        sequences
    }

    #[test]
    fn exhaustive_composition_matches_change_by_change() {
        // Every sequence of up to three changes over initial lengths 0..=3.
        let mut sequences = 0;
        for len in 0..=3_usize {
            let initial: Vec<Item> = (0..len).map(|tag| Item { tag, updates: 0 }).collect();
            let mut sequence = Vec::new();
            let mut next_tag = len;
            sequences += 1;
            check(&initial, &initial, &sequence);
            sequences += enumerate(&initial, &initial, &mut sequence, &mut next_tag, 3);
        }
        assert_eq!(sequences, 73_373);
    }

    #[test]
    fn remove_then_insert_stays_a_remove_and_an_insert() {
        let mut log = ChangeLog::new(1);
        log.push(&CollectionChange::removed(0..1));
        log.push(&CollectionChange::inserted(0..1));
        assert_eq!(
            log.finish(),
            CollectionChange::from_edits([Edit::Remove(0..1), Edit::Insert(0..1)])
        );
    }

    #[test]
    fn insert_then_remove_composes_to_nothing() {
        let mut log = ChangeLog::new(1);
        log.push(&CollectionChange::inserted(0..1));
        log.push(&CollectionChange::removed(0..1));
        assert!(log.finish().is_empty());
    }

    #[test]
    fn update_then_move_reports_the_update_at_the_new_position() {
        let mut log = ChangeLog::new(3);
        log.push(&CollectionChange::updated(0..1));
        log.push(&CollectionChange::moved(0, 2));
        assert_eq!(
            log.finish(),
            CollectionChange::from_edits([Edit::Move { from: 0, to: 2 }, Edit::Update(2..3)])
        );
    }

    #[test]
    fn an_update_undone_by_removal_leaves_no_update() {
        let mut log = ChangeLog::new(2);
        log.push(&CollectionChange::updated(0..1));
        log.push(&CollectionChange::removed(0..1));
        assert_eq!(log.finish(), CollectionChange::removed(0..1));
    }

    #[test]
    fn a_log_without_changes_is_empty() {
        let log = ChangeLog::new(3);
        assert!(log.is_empty());
        assert!(log.finish().is_empty());

        // An empty change records nothing.
        let mut log = ChangeLog::new(3);
        log.push(&CollectionChange::unchanged());
        assert!(log.is_empty());
        assert!(log.finish().is_empty());
    }
}