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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::borrow::{Borrow, Cow};
use std::cmp::{Ordering, Reverse};
use std::convert::{TryFrom, TryInto};
use std::fmt;

#[cfg(test)]
mod tests;

/// A trait with convenience methods for storing an element into a stable structure.
pub trait Storable {
    /// Converts an element into bytes.
    ///
    /// NOTE: `Cow` is used here to avoid unnecessary cloning.
    fn to_bytes(&self) -> Cow<[u8]>;

    /// Converts bytes into an element.
    fn from_bytes(bytes: Cow<[u8]>) -> Self;
}

/// A trait indicating that a `Storable` element is bounded in size.
pub trait BoundedStorable: Storable {
    /// The maximum size, in bytes, of the type when serialized.
    const MAX_SIZE: u32;

    /// True if all the values of this type have fixed-width encoding.
    /// Some data structures, such as stable vector, can take
    /// advantage of fixed size to avoid storing an explicit entry
    /// size.
    ///
    /// Examples: little-/big-endian encoding of u16/u32/u64, tuples
    /// and arrays of fixed-size types.
    const IS_FIXED_SIZE: bool;
}

/// Variable-size, but limited in capacity byte array.
#[derive(Eq, Copy, Clone)]
pub struct Blob<const N: usize> {
    storage: [u8; N],
    size: u32,
}

impl<const N: usize> Blob<N> {
    /// Returns the contents of this array as a byte slice.
    pub fn as_slice(&self) -> &[u8] {
        &self.storage[0..self.len()]
    }

    /// Returns true if the array is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the actual length of this array.
    pub fn len(&self) -> usize {
        self.size as usize
    }
}

impl<const N: usize> Default for Blob<N> {
    fn default() -> Self {
        Self {
            storage: [0; N],
            size: 0,
        }
    }
}

#[derive(Debug)]
pub struct TryFromSliceError;

impl<const N: usize> TryFrom<&[u8]> for Blob<N> {
    type Error = TryFromSliceError;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        if value.len() > N {
            return Err(TryFromSliceError);
        }
        let mut result = Self::default();
        result.storage[0..value.len()].copy_from_slice(value);
        result.size = value.len() as u32;
        Ok(result)
    }
}

impl<const N: usize> AsRef<[u8]> for Blob<N> {
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl<const N: usize> PartialEq for Blob<N> {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice().eq(other.as_slice())
    }
}

impl<const N: usize> PartialOrd for Blob<N> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.as_slice().partial_cmp(other.as_slice())
    }
}

impl<const N: usize> Ord for Blob<N> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}

impl<const N: usize> fmt::Debug for Blob<N> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_slice().fmt(fmt)
    }
}

impl<const N: usize> BoundedStorable for Blob<N> {
    const MAX_SIZE: u32 = N as u32;
    const IS_FIXED_SIZE: bool = false;
}

impl<const N: usize> Storable for Blob<N> {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Borrowed(self.as_slice())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::try_from(bytes.borrow()).unwrap()
    }
}

// NOTE: Below are a few implementations of `Storable` for common types.
// Some of these implementations use `unwrap`, as opposed to returning a `Result`
// with a possible error. The reason behind this decision is that these
// `unwrap`s would be triggered in one of the following cases:
//
// 1) The implementation of `Storable` has a bug.
// 2) The data being stored in the stable structure is corrupt.
//
// Both of these errors are irrecoverable at runtime, and given the additional
// complexity of exposing these errors in the API of stable structures, an `unwrap`
// in case of a detected error is preferable and safer.

impl Storable for () {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Borrowed(&[])
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        assert!(bytes.is_empty());
    }
}

impl BoundedStorable for () {
    const MAX_SIZE: u32 = 0;
    const IS_FIXED_SIZE: bool = false;
}

impl Storable for Vec<u8> {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Borrowed(self)
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        bytes.to_vec()
    }
}

impl Storable for String {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Borrowed(self.as_bytes())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        String::from_utf8(bytes.to_vec()).unwrap()
    }
}

impl Storable for u128 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for u128 {
    const MAX_SIZE: u32 = 16;
    const IS_FIXED_SIZE: bool = true;
}

impl Storable for u64 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for u64 {
    const MAX_SIZE: u32 = 8;
    const IS_FIXED_SIZE: bool = true;
}

impl Storable for f64 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for f64 {
    const MAX_SIZE: u32 = 8;
    const IS_FIXED_SIZE: bool = true;
}

impl Storable for u32 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for u32 {
    const MAX_SIZE: u32 = 4;
    const IS_FIXED_SIZE: bool = true;
}

impl Storable for f32 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for f32 {
    const MAX_SIZE: u32 = 4;
    const IS_FIXED_SIZE: bool = true;
}

impl Storable for u16 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for u16 {
    const MAX_SIZE: u32 = 2;
    const IS_FIXED_SIZE: bool = true;
}

impl Storable for u8 {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Owned(self.to_be_bytes().to_vec())
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self::from_be_bytes(bytes.as_ref().try_into().unwrap())
    }
}

impl BoundedStorable for u8 {
    const MAX_SIZE: u32 = 1;
    const IS_FIXED_SIZE: bool = true;
}

impl<const N: usize> Storable for [u8; N] {
    fn to_bytes(&self) -> Cow<[u8]> {
        Cow::Borrowed(&self[..])
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        assert_eq!(bytes.len(), N);
        let mut arr = [0; N];
        arr[0..N].copy_from_slice(&bytes);
        arr
    }
}

impl<const N: usize> BoundedStorable for [u8; N] {
    const MAX_SIZE: u32 = N as u32;
    const IS_FIXED_SIZE: bool = true;
}

impl<T: Storable> Storable for Reverse<T> {
    fn to_bytes(&self) -> Cow<[u8]> {
        self.0.to_bytes()
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        Self(T::from_bytes(bytes))
    }
}

impl<T: BoundedStorable> BoundedStorable for Reverse<T> {
    const MAX_SIZE: u32 = T::MAX_SIZE;
    const IS_FIXED_SIZE: bool = T::IS_FIXED_SIZE;
}

impl<A, B> Storable for (A, B)
where
    A: BoundedStorable + Default,
    B: BoundedStorable + Default,
{
    fn to_bytes(&self) -> Cow<[u8]> {
        let mut bytes = vec![0; Self::MAX_SIZE as usize];
        let a_bytes = self.0.to_bytes();
        let b_bytes = self.1.to_bytes();

        let a_max_size = max_size(&self.0);
        let b_max_size = max_size(&self.1);

        debug_assert!(a_bytes.len() <= a_max_size);
        debug_assert!(b_bytes.len() <= b_max_size);

        bytes[0..a_bytes.len()].copy_from_slice(a_bytes.borrow());
        bytes[a_max_size..a_max_size + b_bytes.len()].copy_from_slice(b_bytes.borrow());

        let a_size_len = bytes_to_store_size::<A>() as usize;
        let b_size_len = bytes_to_store_size::<B>() as usize;

        let sizes_offset: usize = a_max_size + b_max_size;

        encode_size::<A>(
            &mut bytes[sizes_offset..sizes_offset + a_size_len],
            a_bytes.len(),
        );
        encode_size::<B>(
            &mut bytes[sizes_offset + a_size_len..sizes_offset + a_size_len + b_size_len],
            b_bytes.len(),
        );

        Cow::Owned(bytes)
    }

    fn from_bytes(bytes: Cow<[u8]>) -> Self {
        assert_eq!(bytes.len(), Self::MAX_SIZE as usize);

        // Rust doesn't allow us to access A::MAX_SIZE here but type
        // deduction from values seems to do the trick. Hence the
        // Default bound on the Storable impl.
        let (a, b) = Self::default();
        let a_max_size = max_size(&a);
        let b_max_size = max_size(&b);
        let sizes_offset = a_max_size + b_max_size;

        let a_size_len = bytes_to_store_size::<A>() as usize;
        let b_size_len = bytes_to_store_size::<B>() as usize;
        let a_len = decode_size::<A>(&bytes[sizes_offset..sizes_offset + a_size_len]);
        let b_len = decode_size::<B>(
            &bytes[sizes_offset + a_size_len..sizes_offset + a_size_len + b_size_len],
        );

        let a = A::from_bytes(Cow::Borrowed(&bytes[0..a_len]));
        let b = B::from_bytes(Cow::Borrowed(&bytes[a_max_size..a_max_size + b_len]));

        (a, b)
    }
}

impl<A, B> BoundedStorable for (A, B)
where
    A: BoundedStorable + Default,
    B: BoundedStorable + Default,
{
    const MAX_SIZE: u32 =
        A::MAX_SIZE + B::MAX_SIZE + bytes_to_store_size::<A>() + bytes_to_store_size::<B>();
    const IS_FIXED_SIZE: bool = A::IS_FIXED_SIZE && B::IS_FIXED_SIZE;
}

const fn max_size<A: BoundedStorable>(_: &A) -> usize {
    A::MAX_SIZE as usize
}

fn decode_size<A: BoundedStorable>(src: &[u8]) -> usize {
    if A::IS_FIXED_SIZE {
        A::MAX_SIZE as usize
    } else if A::MAX_SIZE <= u8::MAX as u32 {
        src[0] as usize
    } else if A::MAX_SIZE <= u16::MAX as u32 {
        u16::from_be_bytes([src[0], src[1]]) as usize
    } else {
        u32::from_be_bytes([src[0], src[1], src[2], src[3]]) as usize
    }
}

fn encode_size<A: BoundedStorable>(dst: &mut [u8], n: usize) {
    if A::IS_FIXED_SIZE {
        return;
    }

    if A::MAX_SIZE <= u8::MAX as u32 {
        dst[0] = n as u8;
    } else if A::MAX_SIZE <= u16::MAX as u32 {
        dst[0..2].copy_from_slice(&(n as u16).to_be_bytes());
    } else {
        dst[0..4].copy_from_slice(&(n as u32).to_be_bytes());
    }
}

pub(crate) const fn bytes_to_store_size<A: BoundedStorable>() -> u32 {
    if A::IS_FIXED_SIZE {
        0
    } else if A::MAX_SIZE <= u8::MAX as u32 {
        1
    } else if A::MAX_SIZE <= u16::MAX as u32 {
        2
    } else {
        4
    }
}