1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Smart-pointers and [StableType] trait

use candid::{Int, Nat, Principal};
use serde_bytes::ByteBuf;
use std::collections::{BTreeSet, HashSet};

/// [SBox] smart-pointer that allows storing dynamically-sized data to stable memory
pub mod s_box;

/// Immutable reference to fixed size data on stable memory
pub mod s_ref;

/// Mutable reference to fixed size data on stable memory
pub mod s_ref_mut;

/// Anything that can be stored on stable memory should implement this trait.
///
/// *None of methods of this trait should be called manually, unless you're implementing your own
/// stable data structure!*
///
/// This trait defines behavior for stable drop function and stable drop flag interactions.
/// In order to implement this trait for a data type, there are two options:
/// 1. Use [derive::StableType](crate::derive::StableType) derive macro. This macro requires that any
/// field of your data type also implements [StableType] trait. For most cases consider this option.
/// Supports non-generic structs and enums.
/// 2. Implement it yourself. For data types which do not contain any stable structures inside, default
/// implementation will work fine. For data types which do contain stable structures, the implementation
/// should definitely override [StableType::stable_drop_flag_off] and [StableType::stable_drop::flag_on]
/// calling the same methods on the underlying stable structures. If you want to implement your own
/// stable data structure, you should definitely implement this trait completely, overriding all of
/// its methods.
///
/// # Examples
/// ```rust
/// # use ic_stable_memory::collections::SVec;
/// # use ic_stable_memory::SBox;
/// # use ic_stable_memory::derive::StableType;
///
/// // implement using derive macro
/// #[derive(StableType)]
/// struct A {
///     x: u64,
///     y: [u32; 4],
///     z: Option<SVec<SBox<String>>>,
/// }
/// ```
///
/// ```rust
/// // provide default implementation, if the type does not contain any stable structures
/// # use ic_stable_memory::StableType;
/// struct A {
///     x: u64,
///     y: [u32; 4],
/// }
///
/// impl StableType for A {}
/// ```
///
/// ```rust
/// # use ic_stable_memory::StableType;
/// enum MyOption<T> {
///     Some(T),
///     None,
/// }
///
/// // provide proxy implementation for generics or types which contain stable data structures inside
/// impl<T: StableType> StableType for MyOption<T> {
///     unsafe fn stable_drop_flag_on(&mut self) {
///         if let MyOption::Some(it) = self {
///             it.stable_drop_flag_on();
///         }
///     }
///
///     unsafe fn stable_drop_flag_off(&mut self) {
///         if let MyOption::Some(it) = self {
///             it.stable_drop_flag_off();
///         }
///     }
/// }
/// ```
///
/// ```rust
/// // provide full implementation, if you're building new stable data structure
/// # use ic_stable_memory::mem::StablePtr;
/// # use ic_stable_memory::StableType;
/// struct ExampleStableDataStructure {
///     drop_flag: bool,
///     ptr: StablePtr,
/// }
///
/// impl StableType for ExampleStableDataStructure {
///     fn should_stable_drop(&self) -> bool {
///         self.drop_flag
///     }
///
///     unsafe fn stable_drop_flag_on(&mut self) {
///        self.drop_flag = true;
///     }
///
///     unsafe fn stable_drop_flag_off(&mut self) {
///         self.drop_flag = false;
///     }
///
///     unsafe fn stable_drop(&mut self) {
///         // deallocate any stable memory managed by this data structure
///     }
/// }
/// ```
pub trait StableType {
    /// Sets stable drop flag to `off` position, if it is applicable
    ///
    /// # Safety
    /// Setting stable drop flag to an invalid position will lead to memory leaks and unexpected panics
    /// and considered undefined behavior.
    #[inline]
    unsafe fn stable_drop_flag_off(&mut self) {}

    /// Should set stable drop flag to `on` position, if it is applicable
    ///
    /// # Safety
    /// Setting stable drop flag to an invalid position will lead to memory leaks and unexpected panics
    /// and considered undefined behavior.
    #[inline]
    unsafe fn stable_drop_flag_on(&mut self) {}

    /// Should return the value of the stable drop flag
    #[inline]
    fn should_stable_drop(&self) -> bool {
        false
    }

    /// Performs stable drop, releasing all underlying stable memory of this data structure
    ///
    /// You only want to implement this trait method, if you're building your own stable data structure.
    /// In that case you may want to call this method during [Drop], so your data structure will clean
    /// itself automatically. You may also want to call this method during some dropping or transforming
    /// methods of your data structure, like `into_inner()` or `into_<type_name>().
    ///
    /// # Safety
    /// Performing stable drop twice is undefined behavior.
    ///
    /// # Example
    /// Implementation of [Drop] for your stable data structure should look something like this
    /// ```rust
    /// # use ic_stable_memory::StableType;
    /// struct A {}
    ///
    /// impl StableType for A {
    ///     // implement as usual
    /// }
    ///
    /// impl Drop for A {
    ///     fn drop(&mut self) {
    ///         if self.should_stable_drop() {
    ///             unsafe { self.stable_drop(); }
    ///         }    
    ///     }
    /// }
    /// ```
    #[inline]
    unsafe fn stable_drop(&mut self) {}
}

impl StableType for () {}
impl StableType for bool {}
impl StableType for u8 {}
impl StableType for i8 {}
impl StableType for u16 {}
impl StableType for i16 {}
impl StableType for u32 {}
impl StableType for i32 {}
impl StableType for u64 {}
impl StableType for i64 {}
impl StableType for u128 {}
impl StableType for i128 {}
impl StableType for usize {}
impl StableType for isize {}
impl StableType for f32 {}
impl StableType for f64 {}
impl StableType for char {}

impl<const N: usize> StableType for [(); N] {}
impl<const N: usize> StableType for [bool; N] {}
impl<const N: usize> StableType for [u8; N] {}
impl<const N: usize> StableType for [i8; N] {}
impl<const N: usize> StableType for [u16; N] {}
impl<const N: usize> StableType for [i16; N] {}
impl<const N: usize> StableType for [u32; N] {}
impl<const N: usize> StableType for [i32; N] {}
impl<const N: usize> StableType for [u64; N] {}
impl<const N: usize> StableType for [i64; N] {}
impl<const N: usize> StableType for [u128; N] {}
impl<const N: usize> StableType for [i128; N] {}
impl<const N: usize> StableType for [usize; N] {}
impl<const N: usize> StableType for [isize; N] {}
impl<const N: usize> StableType for [f32; N] {}
impl<const N: usize> StableType for [f64; N] {}
impl<const N: usize> StableType for [char; N] {}

impl StableType for Principal {}
impl StableType for Nat {}
impl StableType for Int {}

impl StableType for ByteBuf {}
impl<T: StableType> StableType for Option<T> {
    #[inline]
    unsafe fn stable_drop_flag_on(&mut self) {
        if let Some(it) = self {
            it.stable_drop_flag_on();
        }
    }

    #[inline]
    unsafe fn stable_drop_flag_off(&mut self) {
        if let Some(it) = self {
            it.stable_drop_flag_off();
        }
    }
}

impl StableType for String {}
impl StableType for Vec<u8> {}
impl StableType for Vec<i8> {}
impl StableType for Vec<u16> {}
impl StableType for Vec<i16> {}
impl StableType for Vec<u32> {}
impl StableType for Vec<i32> {}
impl StableType for Vec<u64> {}
impl StableType for Vec<i64> {}
impl StableType for Vec<u128> {}
impl StableType for Vec<i128> {}
impl StableType for Vec<usize> {}
impl StableType for Vec<isize> {}
impl StableType for Vec<f32> {}
impl StableType for Vec<f64> {}
impl StableType for Vec<()> {}
impl StableType for Vec<bool> {}
impl StableType for Vec<char> {}

impl StableType for Vec<Principal> {}
impl StableType for Vec<Nat> {}
impl StableType for Vec<Int> {}

impl StableType for HashSet<u8> {}
impl StableType for HashSet<i8> {}
impl StableType for HashSet<u16> {}
impl StableType for HashSet<i16> {}
impl StableType for HashSet<u32> {}
impl StableType for HashSet<i32> {}
impl StableType for HashSet<u64> {}
impl StableType for HashSet<i64> {}
impl StableType for HashSet<u128> {}
impl StableType for HashSet<i128> {}
impl StableType for HashSet<usize> {}
impl StableType for HashSet<isize> {}
impl StableType for HashSet<f32> {}
impl StableType for HashSet<f64> {}
impl StableType for HashSet<()> {}
impl StableType for HashSet<bool> {}
impl StableType for HashSet<char> {}

impl StableType for BTreeSet<u8> {}
impl StableType for BTreeSet<i8> {}
impl StableType for BTreeSet<u16> {}
impl StableType for BTreeSet<i16> {}
impl StableType for BTreeSet<u32> {}
impl StableType for BTreeSet<i32> {}
impl StableType for BTreeSet<u64> {}
impl StableType for BTreeSet<i64> {}
impl StableType for BTreeSet<u128> {}
impl StableType for BTreeSet<i128> {}
impl StableType for BTreeSet<usize> {}
impl StableType for BTreeSet<isize> {}
impl StableType for BTreeSet<f32> {}
impl StableType for BTreeSet<f64> {}
impl StableType for BTreeSet<()> {}
impl StableType for BTreeSet<bool> {}
impl StableType for BTreeSet<char> {}