cpu/
changelog.rs

1//! A collection of orderable values which the caller can efficiently
2//! drain in one go.
3use serde::Serialize;
4use std::collections::BTreeSet;
5
6/// Efficiently accumulate as set of things and then return all the
7/// members of the set.
8#[derive(Debug, Serialize)]
9pub(crate) struct ChangeIndex<K: Ord + Serialize> {
10    changes: BTreeSet<K>,
11}
12
13impl<K: Ord + Serialize> Default for ChangeIndex<K> {
14    // Cannot use derive for Default because that would require K to
15    // implement Derive, while in reality it doesn't need to.
16    fn default() -> Self {
17        Self {
18            changes: BTreeSet::new(),
19        }
20    }
21}
22
23impl<K: Ord + Serialize> ChangeIndex<K> {
24    pub(crate) fn add(&mut self, k: K) {
25        self.changes.insert(k);
26    }
27
28    pub(crate) fn drain(&mut self) -> BTreeSet<K> {
29        let mut result: BTreeSet<_> = BTreeSet::new();
30        result.append(&mut self.changes);
31        result
32    }
33}