Skip to main content

ic_storage/adapter/
set.rs

1//! Governed set collection implementation.
2use super::{Memory, SetIter, Storable};
3use crate::RuntimeMemory;
4use crate::{DefaultMemoryImpl, substrate};
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 collection only in zero-page memory.
13    ///
14    /// # Panics
15    /// Refuses nonempty memory before writing. Growth and codec failures retain
16    /// upstream behavior.
17    #[must_use]
18    pub fn create(memory: RuntimeMemory<M>) -> Self {
19        assert_eq!(memory.size(), 0, "collection memory is not empty");
20        Self::reset(memory)
21    }
22
23    /// Deliberately replace any contents in this admitted allocation.
24    ///
25    /// # Panics
26    /// Growth and codec failures retain upstream behavior.
27    #[must_use]
28    pub fn reset(memory: RuntimeMemory<M>) -> Self {
29        Self {
30            inner: substrate::BTreeSet::new(memory),
31        }
32    }
33
34    /// Load retained collection state without initializing a replacement.
35    ///
36    /// # Panics
37    /// Refuses empty or foreign memory and incompatible layouts/bounds without
38    /// writing. Record decoding and backing failures retain upstream behavior.
39    #[must_use]
40    pub fn load(memory: RuntimeMemory<M>) -> Self {
41        Self {
42            inner: substrate::BTreeSet::load(memory),
43        }
44    }
45    /// Delegates to upstream `BTreeSet::insert` with the governed allocation.
46    pub fn insert(&mut self, key: K) -> bool {
47        self.inner.insert(key)
48    }
49    /// Delegates to upstream `BTreeSet::contains` with the governed allocation.
50    pub fn contains(&self, key: &K) -> bool {
51        self.inner.contains(key)
52    }
53    /// Delegates to upstream `BTreeSet::is_empty` with the governed allocation.
54    #[must_use]
55    pub fn is_empty(&self) -> bool {
56        self.inner.is_empty()
57    }
58    /// Delegates to upstream `BTreeSet::len` with the governed allocation.
59    #[must_use]
60    pub fn len(&self) -> u64 {
61        self.inner.len()
62    }
63    /// Delegates to upstream `BTreeSet::remove` with the governed allocation.
64    pub fn remove(&mut self, key: &K) -> bool {
65        self.inner.remove(key)
66    }
67    /// Delegates to upstream `BTreeSet::first` with the governed allocation.
68    #[must_use]
69    pub fn first(&self) -> Option<K> {
70        self.inner.first()
71    }
72    /// Delegates to upstream `BTreeSet::last` with the governed allocation.
73    #[must_use]
74    pub fn last(&self) -> Option<K> {
75        self.inner.last()
76    }
77    /// Delegates to upstream `BTreeSet::pop_first` with the governed allocation.
78    pub fn pop_first(&mut self) -> Option<K> {
79        self.inner.pop_first()
80    }
81    /// Delegates to upstream `BTreeSet::pop_last` with the governed allocation.
82    pub fn pop_last(&mut self) -> Option<K> {
83        self.inner.pop_last()
84    }
85    /// Delegates to upstream `BTreeSet::iter` with the governed allocation.
86    #[must_use]
87    pub fn iter(&self) -> SetIter<'_, K, M> {
88        self.inner.iter()
89    }
90    /// Delegates to upstream `BTreeSet::range` with the governed allocation.
91    pub fn range(&self, key_range: impl RangeBounds<K>) -> SetIter<'_, K, M> {
92        self.inner.range(key_range)
93    }
94    /// Delegates to upstream `BTreeSet::clear` with the governed allocation.
95    pub fn clear(&mut self) {
96        self.inner.clear();
97    }
98}
99
100impl<'a, K: Storable + Ord + Clone, M: Memory> IntoIterator for &'a StableBTreeSet<K, M> {
101    type Item = K;
102    type IntoIter = SetIter<'a, K, M>;
103    fn into_iter(self) -> Self::IntoIter {
104        self.iter()
105    }
106}