ifc_lite_diff/
sparse_collection.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4//! Fingerprint collection slots. Absent array indices are content (#8042).
5use serde::{Deserialize, Deserializer};
6
7/// A projected array slot; a hole is distinct from every present record.
8#[derive(Debug, Clone, PartialEq)]
9pub enum Slot<T> { Hole, Present(T) }
10
11/// Present records in input order plus a hole COUNT (#8042, #8114).
12///
13/// Canonical sorting moves every hole after the records, and the stable sort
14/// keeps the present records' relative order whatever the hole positions, so
15/// positions are not content: equality ignores them. Counting instead of
16/// storing keeps memory O(present) for any array length, including a valid
17/// 2^32 - 1 slot array, while cardinality stays exact.
18#[derive(Debug, Clone, PartialEq)]
19pub struct SparseCollection<T> { present: Vec<T>, holes: usize }
20impl<T> Default for SparseCollection<T> {
21    fn default() -> Self { Self { present: Vec::new(), holes: 0 } }
22}
23impl<T> SparseCollection<T> {
24    pub fn present(&self) -> impl Iterator<Item = &T> { self.present.iter() }
25    pub fn holes(&self) -> usize { self.holes }
26    pub fn push(&mut self, value: T) { self.present.push(value); }
27    /// Add `count` holes in O(1).
28    pub fn push_holes(&mut self, count: usize) {
29        self.holes = self.holes.checked_add(count).expect("sparse hole count exceeds usize");
30    }
31    pub fn map<U>(self, project: impl FnMut(T) -> U) -> SparseCollection<U> {
32        SparseCollection { present: self.present.into_iter().map(project).collect(), holes: self.holes }
33    }
34}
35impl<T> FromIterator<T> for SparseCollection<T> {
36    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
37        Self { present: iter.into_iter().collect(), holes: 0 }
38    }
39}
40impl<T> FromIterator<Slot<T>> for SparseCollection<T> {
41    fn from_iter<I: IntoIterator<Item = Slot<T>>>(iter: I) -> Self {
42        let mut collection = Self::default();
43        for slot in iter {
44            match slot { Slot::Hole => collection.push_holes(1), Slot::Present(value) => collection.push(value) }
45        }
46        collection
47    }
48}
49impl<T> From<Vec<T>> for SparseCollection<T> {
50    fn from(values: Vec<T>) -> Self { values.into_iter().collect() }
51}
52// Serialized forms (JSON, document DTOs) cannot express an absent array index:
53// a `null` element is a PRESENT value, which the TypeScript oracle rejects when
54// it reads `.name` (#8114). So decoding is dense and `null` fails in the record
55// decoder; only the raw host decoder, which can see the array, constructs holes.
56impl<'de, T: Deserialize<'de>> Deserialize<'de> for SparseCollection<T> {
57    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
58        Vec::<T>::deserialize(deserializer).map(Self::from)
59    }
60}
61pub(crate) fn optional_sparse<'de, D, T>(deserializer: D) -> Result<SparseCollection<T>, D::Error>
62where D: Deserializer<'de>, T: Deserialize<'de> {
63    Option::<SparseCollection<T>>::deserialize(deserializer).map(Option::unwrap_or_default)
64}