Skip to main content

ic_storage/adapter/
set.rs

1//! Governed set collection implementation.
2use super::{Memory, SetIter, Storable};
3use crate::substrate::{self, DefaultMemoryImpl};
4use ic_memory::RuntimeMemory;
5use std::ops::RangeBounds;
6
7/// Governed upstream `BTreeSet`; initialization and failures follow upstream contracts.
8pub struct StableBTreeSet<K: Storable + Ord + Clone, M: Memory = DefaultMemoryImpl> {
9    inner: substrate::BTreeSet<K, RuntimeMemory<M>>,
10}
11impl<K: Storable + Ord + Clone, M: Memory> StableBTreeSet<K, M> {
12    /// Create a new collection, replacing any prior collection in the selected memory.
13    /// This is destructive even when a valid collection already exists.
14    ///
15    /// # Panics
16    /// Panics on insufficient memory or codec/backing failure.
17    pub fn new(memory: RuntimeMemory<M>) -> Self {
18        Self {
19            inner: substrate::BTreeSet::new(memory),
20        }
21    }
22    /// Open a recognized retained collection; initialize a new one when memory
23    /// is empty or has foreign magic. Foreign contents may be overwritten.
24    /// This is not a corruption-recovery or schema-admission operation.
25    ///
26    /// # Panics
27    /// Panics for unsupported recognized layouts, invalid retained contents,
28    /// insufficient memory or codec/backing failure.
29    pub fn init(memory: RuntimeMemory<M>) -> Self {
30        Self {
31            inner: substrate::BTreeSet::init(memory),
32        }
33    }
34    /// Load an existing tree without initializing a replacement.
35    ///
36    /// # Panics
37    /// Panics for missing/foreign headers, unsupported layouts or backing failure.
38    /// Record decoding can also panic when records are subsequently accessed.
39    pub fn load(memory: RuntimeMemory<M>) -> Self {
40        Self {
41            inner: substrate::BTreeSet::load(memory),
42        }
43    }
44    /// Delegates to upstream `BTreeSet::insert` with the governed allocation.
45    pub fn insert(&mut self, key: K) -> bool {
46        self.inner.insert(key)
47    }
48    /// Delegates to upstream `BTreeSet::contains` with the governed allocation.
49    pub fn contains(&self, key: &K) -> bool {
50        self.inner.contains(key)
51    }
52    /// Delegates to upstream `BTreeSet::is_empty` with the governed allocation.
53    pub fn is_empty(&self) -> bool {
54        self.inner.is_empty()
55    }
56    /// Delegates to upstream `BTreeSet::len` with the governed allocation.
57    pub fn len(&self) -> u64 {
58        self.inner.len()
59    }
60    /// Delegates to upstream `BTreeSet::remove` with the governed allocation.
61    pub fn remove(&mut self, key: &K) -> bool {
62        self.inner.remove(key)
63    }
64    /// Delegates to upstream `BTreeSet::first` with the governed allocation.
65    pub fn first(&self) -> Option<K> {
66        self.inner.first()
67    }
68    /// Delegates to upstream `BTreeSet::last` with the governed allocation.
69    pub fn last(&self) -> Option<K> {
70        self.inner.last()
71    }
72    /// Delegates to upstream `BTreeSet::pop_first` with the governed allocation.
73    pub fn pop_first(&mut self) -> Option<K> {
74        self.inner.pop_first()
75    }
76    /// Delegates to upstream `BTreeSet::pop_last` with the governed allocation.
77    pub fn pop_last(&mut self) -> Option<K> {
78        self.inner.pop_last()
79    }
80    /// Delegates to upstream `BTreeSet::iter` with the governed allocation.
81    pub fn iter(&self) -> SetIter<'_, K, M> {
82        self.inner.iter()
83    }
84    /// Delegates to upstream `BTreeSet::range` with the governed allocation.
85    pub fn range(&self, key_range: impl RangeBounds<K>) -> SetIter<'_, K, M> {
86        self.inner.range(key_range)
87    }
88    /// Delegates to upstream `BTreeSet::clear` with the governed allocation.
89    pub fn clear(&mut self) {
90        self.inner.clear();
91    }
92}
93
94impl<'a, K: Storable + Ord + Clone, M: Memory> IntoIterator for &'a StableBTreeSet<K, M> {
95    type Item = K;
96    type IntoIter = SetIter<'a, K, M>;
97    fn into_iter(self) -> Self::IntoIter {
98        self.iter()
99    }
100}