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