dioxus-const-vec 0.8.0-alpha.3

A fixed-capacity Vec usable in const contexts
Documentation
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std::{fmt::Debug, hash::Hash, mem::MaybeUninit};

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

/// A buffer that can be read from at compile time.
///
/// This is very similar to [`std::io::Cursor`], but it is designed to be used
/// in const contexts.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ConstReadBuffer<'a> {
    location: usize,
    memory: &'a [u8],
}

impl<'a> ConstReadBuffer<'a> {
    /// Create a new buffer from a byte slice.
    pub const fn new(memory: &'a [u8]) -> Self {
        Self {
            location: 0,
            memory,
        }
    }

    /// Get the next byte from the buffer.
    ///
    /// Returns `None` if the buffer is empty. Otherwise this returns the new
    /// buffer state along with the byte that was read.
    pub const fn get(mut self) -> Option<(Self, u8)> {
        if self.location >= self.memory.len() {
            return None;
        }
        let value = self.memory[self.location];
        self.location += 1;
        Some((self, value))
    }

    /// Get a reference to the underlying byte slice.
    pub const fn as_ref(&self) -> &[u8] {
        self.memory
    }

    /// Get a slice of the buffer from the current location to the end.
    pub const fn remaining(&self) -> &[u8] {
        self.memory.split_at(self.location).1
    }
}

/// A version of [`Vec`] that is usable in const contexts.
///
/// `ConstVec` has a fixed maximum size, but it can grow and shrink within that
/// size limit as needed.
///
/// # Example
///
/// ```rust
/// # use dioxus_const_vec::ConstVec;
/// const VEC: ConstVec<u8> = {
///     let mut vec = ConstVec::new();
///     vec.push(1);
///     vec.push(2);
///     vec.push(3);
///     vec.push(4);
///     assert!(vec.pop().unwrap() == 4);
///     vec
/// };
///
/// assert_eq!(VEC.as_ref(), &[1, 2, 3]);
/// ```
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.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 dioxus_const_vec::ConstVec;
    /// const EMPTY: ConstVec<u8, 10> = ConstVec::new_with_max_size();
    /// assert!(EMPTY.is_empty());
    /// ```
    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 dioxus_const_vec::ConstVec;
    /// const ONE: ConstVec<u8> = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec
    /// };
    /// assert_eq!(ONE.as_ref(), &[1]);
    /// ```
    pub const fn push(&mut self, value: T) {
        if self.len as usize >= MAX_SIZE {
            panic!("const vec capacity exceeded");
        }
        self.memory[self.len as usize] = MaybeUninit::new(value);
        self.len += 1;
    }

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

    /// Get a reference to the value at the given index.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::ConstVec;
    /// const ONE: ConstVec<u8> = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec
    /// };
    /// 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 a copy of the value at the given index.
    ///
    /// This panics if `index` is out of bounds.
    pub const fn at(&self, index: usize) -> T
    where
        T: Copy,
    {
        if index >= self.len as usize {
            panic!("const vec index out of bounds");
        }
        unsafe { self.memory[index].assume_init() }
    }

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

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

    /// Get a reference to the underlying slice.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::ConstVec;
    /// const ONE: ConstVec<u8> = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec
    /// };
    /// 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])
        }
    }

    /// Get a reference to the underlying slice.
    pub const fn as_slice(&self) -> &[T] {
        self.as_ref()
    }

    /// Swap the values at the given indices.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::ConstVec;
    /// const THREE: ConstVec<u8> = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec.push(2);
    ///     vec.swap(0, 1);
    ///     vec
    /// };
    /// assert_eq!(THREE.as_ref(), &[2, 1]);
    /// ```
    pub const fn swap(&mut self, first: usize, second: usize)
    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;
    }

    /// Pop a value off the end of the [`ConstVec`].
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::ConstVec;
    /// const POPPED: (ConstVec<u8>, Option<u8>) = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec.push(2);
    ///     vec.push(3);
    ///     let popped = vec.pop();
    ///     (vec, popped)
    /// };
    /// assert_eq!(POPPED.0.as_ref(), &[1, 2]);
    /// assert_eq!(POPPED.1.unwrap(), 3);
    /// ```
    pub const fn pop(&mut self) -> Option<T>
    where
        T: Copy,
    {
        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
        }
    }

    /// Remove the value at the given index.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::ConstVec;
    /// const REMOVED: (ConstVec<u8>, Option<u8>) = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec.push(2);
    ///     vec.push(3);
    ///     let removed = vec.remove(1);
    ///     (vec, removed)
    /// };
    /// assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
    /// assert_eq!(REMOVED.1.unwrap(), 2);
    /// ```
    pub const fn remove(&mut self, index: usize) -> Option<T>
    where
        T: Copy,
    {
        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
        }
    }

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

    /// Split the [`ConstVec`] into two at the given index.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::ConstVec;
    /// const SPLIT: (ConstVec<u8>, ConstVec<u8>) = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec.push(2);
    ///     vec.push(3);
    ///     vec.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.push(left[i]);
            i += 1;
        }
        let mut right_vec = Self::new_with_max_size();
        i = 0;
        while i < right.len() {
            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`].
    ///
    /// # Example
    ///
    /// ```rust
    /// # use dioxus_const_vec::{ConstReadBuffer, ConstVec};
    /// const TWO: ConstVec<u8> = {
    ///     let mut vec = ConstVec::new();
    ///     vec.push(1);
    ///     vec.push(2);
    ///     vec
    /// };
    /// const READ: ConstReadBuffer = TWO.read();
    /// assert_eq!(READ.as_ref(), &[1, 2]);
    /// ```
    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.push(1234);
        vec.push(5678);
        vec
    };
    assert_eq!(VEC.as_ref(), &[1234, 5678]);
    let mut vec = VEC;
    let value = vec.pop();
    assert_eq!(value, Some(5678));
    let value = vec.pop();
    assert_eq!(value, Some(1234));
    let 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.push(1234);
        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.push(1234);
        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_at() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec.push(1234);
        vec.push(5678);
        vec
    };
    assert_eq!(VEC.at(0), 1234);
    assert_eq!(VEC.at(1), 5678);
}

#[test]
fn test_const_vec_swap() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec.push(1234);
        vec.push(5678);
        vec
    };
    let mut vec = VEC;
    assert_eq!(vec.as_ref(), &[1234, 5678]);
    vec.swap(0, 1);
    assert_eq!(vec.as_ref(), &[5678, 1234]);
    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.push(1234);
        vec.push(5678);
        vec
    };
    let mut vec = VEC;
    assert_eq!(vec.as_ref(), &[1234, 5678]);
    let value = vec.remove(0);
    assert_eq!(value, Some(1234));
    assert_eq!(vec.as_ref(), &[5678]);
    let value = vec.remove(0);
    assert_eq!(value, Some(5678));
    assert_eq!(vec.as_ref(), &[]);
}

#[test]
fn test_const_vec_extend() {
    const VEC: ConstVec<u32> = {
        let mut vec = ConstVec::new();
        vec.push(1234);
        vec.push(5678);
        vec.extend(&[91011, 1213]);
        vec
    };
    let vec = VEC;
    assert_eq!(vec.as_ref(), &[1234, 5678, 91011, 1213]);
}