const_serialize/
const_vec.rs

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
#![allow(dead_code)]
use std::{fmt::Debug, hash::Hash, mem::MaybeUninit};

use crate::ConstReadBuffer;

const DEFAULT_MAX_SIZE: usize = 2usize.pow(10);

/// [`ConstVec`] is a version of [`Vec`] that is usable in const contexts. It has
/// a fixed maximum size, but it can can grow and shrink within that size limit
/// as needed.
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// // Methods that mutate the vector will return a new vector
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const THREE: ConstVec<u8> = TWO.push(3);
/// const FOUR: ConstVec<u8> = THREE.push(4);
/// // If a value is also returned, that will be placed in a tuple in the return value
/// // along with the new vector
/// const POPPED: (ConstVec<u8>, Option<u8>) = FOUR.pop();
/// assert_eq!(POPPED.0, THREE);
/// assert_eq!(POPPED.1.unwrap(), 4);
/// ```
pub struct ConstVec<T, const MAX_SIZE: usize = DEFAULT_MAX_SIZE> {
    memory: [MaybeUninit<T>; MAX_SIZE],
    len: u32,
}

impl<T: Clone, const MAX_SIZE: usize> Clone for ConstVec<T, MAX_SIZE> {
    fn clone(&self) -> Self {
        let mut cloned = Self::new_with_max_size();
        for i in 0..self.len as usize {
            cloned = cloned.push(self.get(i).unwrap().clone());
        }
        cloned
    }
}

impl<T: Copy, const MAX_SIZE: usize> Copy for ConstVec<T, MAX_SIZE> {}

impl<T: PartialEq, const MAX_SIZE: usize> PartialEq for ConstVec<T, MAX_SIZE> {
    fn eq(&self, other: &Self) -> bool {
        self.as_ref() == other.as_ref()
    }
}

impl<T: Hash, const MAX_SIZE: usize> Hash for ConstVec<T, MAX_SIZE> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.as_ref().hash(state)
    }
}

impl<T, const MAX_SIZE: usize> Default for ConstVec<T, MAX_SIZE> {
    fn default() -> Self {
        Self::new_with_max_size()
    }
}

impl<T: Debug, const MAX_SIZE: usize> Debug for ConstVec<T, MAX_SIZE> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConstVec")
            .field("len", &self.len)
            .field("memory", &self.as_ref())
            .finish()
    }
}

impl<T> ConstVec<T> {
    /// Create a new empty [`ConstVec`]
    pub const fn new() -> Self {
        Self::new_with_max_size()
    }
}

impl<T, const MAX_SIZE: usize> ConstVec<T, MAX_SIZE> {
    /// Create a new empty [`ConstVec`] with a custom maximum size
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8, 10> = ConstVec::new_with_max_size();
    /// ```
    pub const fn new_with_max_size() -> Self {
        Self {
            memory: [const { MaybeUninit::uninit() }; MAX_SIZE],
            len: 0,
        }
    }

    /// Push a value onto the end of the [`ConstVec`]
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// assert_eq!(ONE.as_ref(), &[1]);
    /// ```
    pub const fn push(mut self, value: T) -> Self {
        self.memory[self.len as usize] = MaybeUninit::new(value);
        self.len += 1;
        self
    }

    /// Extend the [`ConstVec`] with the contents of a slice
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.extend(&[1, 2, 3]);
    /// assert_eq!(ONE.as_ref(), &[1, 2, 3]);
    /// ```
    pub const fn extend(mut self, other: &[T]) -> Self
    where
        T: Copy,
    {
        let mut i = 0;
        while i < other.len() {
            self = self.push(other[i]);
            i += 1;
        }
        self
    }

    /// Get a reference to the value at the given index
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// assert_eq!(ONE.get(0), Some(&1));
    /// ```
    pub const fn get(&self, index: usize) -> Option<&T> {
        if index < self.len as usize {
            Some(unsafe { &*self.memory[index].as_ptr() })
        } else {
            None
        }
    }

    /// Get the length of the [`ConstVec`]
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// assert_eq!(ONE.len(), 1);
    /// ```
    pub const fn len(&self) -> usize {
        self.len as usize
    }

    /// Check if the [`ConstVec`] is empty
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// assert!(EMPTY.is_empty());
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// assert!(!ONE.is_empty());
    /// ```
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get a reference to the underlying slice
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// assert_eq!(ONE.as_ref(), &[1]);
    /// ```
    pub const fn as_ref(&self) -> &[T] {
        unsafe {
            &*(self.memory.split_at(self.len as usize).0 as *const [MaybeUninit<T>] as *const [T])
        }
    }

    /// Swap the values at the given indices
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// const TWO: ConstVec<u8> = ONE.push(2);
    /// const THREE: ConstVec<u8> = TWO.swap(0, 1);
    /// assert_eq!(THREE.as_ref(), &[2, 1]);
    /// ```
    pub const fn swap(mut self, first: usize, second: usize) -> Self
    where
        T: Copy,
    {
        assert!(first < self.len as usize);
        assert!(second < self.len as usize);
        let temp = self.memory[first];
        self.memory[first] = self.memory[second];
        self.memory[second] = temp;
        self
    }

    /// Pop a value off the end of the [`ConstVec`]
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// const TWO: ConstVec<u8> = ONE.push(2);
    /// const THREE: ConstVec<u8> = TWO.push(3);
    /// const POPPED: (ConstVec<u8>, Option<u8>) = THREE.pop();
    /// assert_eq!(POPPED.0, TWO);
    /// assert_eq!(POPPED.1.unwrap(), 3);
    /// ```
    pub const fn pop(mut self) -> (Self, Option<T>)
    where
        T: Copy,
    {
        let value = if self.len > 0 {
            self.len -= 1;
            let last = self.len as usize;
            let last_value = unsafe { self.memory[last].assume_init() };
            Some(last_value)
        } else {
            None
        };
        (self, value)
    }

    /// Remove the value at the given index
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// const TWO: ConstVec<u8> = ONE.push(2);
    /// const THREE: ConstVec<u8> = TWO.push(3);
    /// const REMOVED: (ConstVec<u8>, Option<u8>) = THREE.remove(1);
    /// assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
    /// assert_eq!(REMOVED.1.unwrap(), 2);
    /// ```
    pub const fn remove(mut self, index: usize) -> (Self, Option<T>)
    where
        T: Copy,
    {
        let value = if index < self.len as usize {
            let value = unsafe { self.memory[index].assume_init() };
            let mut swap_index = index;
            while swap_index + 1 < self.len as usize {
                self.memory[swap_index] = self.memory[swap_index + 1];
                swap_index += 1;
            }
            self.len -= 1;
            Some(value)
        } else {
            None
        };

        (self, value)
    }

    /// Set the value at the given index
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// const TWO: ConstVec<u8> = ONE.set(0, 2);
    /// assert_eq!(TWO.as_ref(), &[2]);
    /// ```
    pub const fn set(mut self, index: usize, value: T) -> Self {
        if index >= self.len as usize {
            panic!("Out of bounds")
        }
        self.memory[index] = MaybeUninit::new(value);
        self
    }

    pub(crate) const fn into_parts(self) -> ([MaybeUninit<T>; MAX_SIZE], usize) {
        (self.memory, self.len as usize)
    }

    /// Split the [`ConstVec`] into two at the given index
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::ConstVec;
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// const TWO: ConstVec<u8> = ONE.push(2);
    /// const THREE: ConstVec<u8> = TWO.push(3);
    /// const SPLIT: (ConstVec<u8>, ConstVec<u8>) = THREE.split_at(1);
    /// assert_eq!(SPLIT.0.as_ref(), &[1]);
    /// assert_eq!(SPLIT.1.as_ref(), &[2, 3]);
    /// ```
    pub const fn split_at(&self, index: usize) -> (Self, Self)
    where
        T: Copy,
    {
        assert!(index <= self.len as usize);
        let slice = self.as_ref();
        let (left, right) = slice.split_at(index);
        let mut left_vec = Self::new_with_max_size();
        let mut i = 0;
        while i < left.len() {
            left_vec = left_vec.push(left[i]);
            i += 1;
        }
        let mut right_vec = Self::new_with_max_size();
        i = 0;
        while i < right.len() {
            right_vec = right_vec.push(right[i]);
            i += 1;
        }
        (left_vec, right_vec)
    }
}

impl<const MAX_SIZE: usize> ConstVec<u8, MAX_SIZE> {
    /// Convert the [`ConstVec`] into a [`ConstReadBuffer`](crate::ConstReadBuffer)
    ///
    /// # Example
    /// ```rust
    /// # use const_serialize::{ConstVec, ConstReadBuffer};
    /// const EMPTY: ConstVec<u8> = ConstVec::new();
    /// const ONE: ConstVec<u8> = EMPTY.push(1);
    /// const TWO: ConstVec<u8> = ONE.push(2);
    /// const READ: ConstReadBuffer = TWO.read();
    /// ```
    pub const fn read(&self) -> ConstReadBuffer<'_> {
        ConstReadBuffer::new(self.as_ref())
    }
}

#[test]
fn test_const_vec() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec = vec.push(1234);
        vec = vec.push(5678);
        vec
    };
    assert_eq!(VEC.as_ref(), &[1234, 5678]);
    let vec = VEC;
    let (vec, value) = vec.pop();
    assert_eq!(value, Some(5678));
    let (vec, value) = vec.pop();
    assert_eq!(value, Some(1234));
    let (vec, value) = vec.pop();
    assert_eq!(value, None);
    assert_eq!(vec.as_ref(), &[]);
}

#[test]
fn test_const_vec_len() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec = vec.push(1234);
        vec = vec.push(5678);
        vec
    };
    assert_eq!(VEC.len(), 2);
}

#[test]
fn test_const_vec_get() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec = vec.push(1234);
        vec = vec.push(5678);
        vec
    };
    assert_eq!(VEC.get(0), Some(&1234));
    assert_eq!(VEC.get(1), Some(&5678));
    assert_eq!(VEC.get(2), None);
}

#[test]
fn test_const_vec_swap() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec = vec.push(1234);
        vec = vec.push(5678);
        vec
    };
    let mut vec = VEC;
    assert_eq!(vec.as_ref(), &[1234, 5678]);
    vec = vec.swap(0, 1);
    assert_eq!(vec.as_ref(), &[5678, 1234]);
    vec = vec.swap(0, 1);
    assert_eq!(vec.as_ref(), &[1234, 5678]);
}

#[test]
fn test_const_vec_remove() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec = vec.push(1234);
        vec = vec.push(5678);
        vec
    };
    let vec = VEC;
    println!("{:?}", vec);
    assert_eq!(vec.as_ref(), &[1234, 5678]);
    let (vec, value) = vec.remove(0);
    assert_eq!(value, Some(1234));
    assert_eq!(vec.as_ref(), &[5678]);
    let (vec, value) = vec.remove(0);
    assert_eq!(value, Some(5678));
    assert_eq!(vec.as_ref(), &[]);
}