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
use core::mem::MaybeUninit;
/// `Vec` that can be used in a constant context.
#[doc(hidden)]
pub struct ComptimeVec<T: Copy> {
// FIXME: Waiting for <https://github.com/rust-lang/const-eval/issues/20>
storage: [MaybeUninit<T>; MAX_LEN],
len: usize,
}
const MAX_LEN: usize = 256;
impl<T: Copy> ComptimeVec<T> {
pub const fn new() -> Self {
Self {
storage: [MaybeUninit::uninit(); MAX_LEN],
len: 0,
}
}
pub const fn push(&mut self, x: T) {
self.storage[self.len] = MaybeUninit::new(x);
self.len += 1;
}
pub const fn len(&self) -> usize {
self.len
}
pub const fn is_empty(&self) -> bool {
self.len == 0
}
// FIXME: Waiting for <https://github.com/rust-lang/rust/issues/67792>
pub const fn get(&self, i: usize) -> &T {
assert!(i < self.len(), "out of bounds");
// Safety: `self.storage[0..self.len]` is initialized, and `i < self.len`
// FIXME: Waiting for `MaybeUninit::as_ptr` to be stabilized
unsafe { &*(&self.storage[i] as *const _ as *const T) }
}
// FIXME: Waiting for <https://github.com/rust-lang/rust/issues/67792>
pub const fn get_mut(&mut self, i: usize) -> &mut T {
assert!(i < self.len(), "out of bounds");
// Safety: `self.storage[0..self.len]` is initialized, and `i < self.len`
// FIXME: Waiting for `MaybeUninit::as_ptr` to be stabilized
unsafe { &mut *(&mut self.storage[i] as *mut _ as *mut T) }
}
}
impl<T: Copy> ComptimeVec<T> {
pub const fn to_array<const LEN: usize>(&self) -> [T; LEN] {
// FIXME: Work-around for `assert_eq!` being unsupported in `const fn`
assert!(self.len() == LEN);
// Safety: This is equivalent to `transmute_copy(&self.storage)`. The
// memory layout of `[MaybeUninit<T>; LEN]` is identical to `[T; LEN]`.
// We initialized all elements in `storage[0..LEN]`, so it's safe to
// reinterpret that range as `[T; LEN]`.
unsafe { *(&self.storage as *const _ as *const [T; LEN]) }
}
}
// FIXME: Waiting for <https://github.com/rust-lang/rust/issues/67792>
// FIXME: Waiting for `Iterator` to be usable in `const fn`
// FIXME: Waiting for `FnMut` to be usable in `const fn`
/// An implementation of `$vec.iter().position(|$item| $predicate)` that is
/// compatible with a const context.
macro_rules! vec_position {
($vec:expr, |$item:ident| $predicate:expr) => {{
let mut i = 0;
loop {
if i >= $vec.len() {
break None;
}
let $item = $vec.get(i);
if $predicate {
break Some(i);
}
i += 1;
}
}};
}
#[cfg(test)]
mod tests {
use super::*;
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
#[test]
fn new() {
const _VEC: ComptimeVec<u32> = ComptimeVec::new();
}
#[test]
fn push() {
const fn vec() -> ComptimeVec<u32> {
// FIXME: Unable to do this inside a `const` item because of
// <https://github.com/rust-lang/rust/pull/72934>
let mut v = ComptimeVec::new();
v.push(42);
v
}
const VEC: ComptimeVec<u32> = vec();
const VEC_LEN: usize = VEC.len();
assert_eq!(VEC_LEN, 1);
const VEC_VAL: u32 = *VEC.get(0);
assert_eq!(VEC_VAL, 42);
}
#[test]
fn to_array() {
const fn array() -> [u32; 3] {
let mut v = ComptimeVec::new();
v.push(1);
v.push(2);
v.push(3);
v.to_array()
}
assert_eq!(array(), [1, 2, 3]);
}
#[test]
fn get_mut() {
const fn val() -> u32 {
let mut v = ComptimeVec::new();
v.push(1);
v.push(2);
v.push(3);
*v.get_mut(1) += 2;
*v.get(1)
}
assert_eq!(val(), 4);
}
#[test]
fn const_vec_position() {
const fn pos() -> [Option<usize>; 2] {
let mut v = ComptimeVec::new();
v.push(42);
v.push(43);
v.push(44);
[
vec_position!(v, |i| *i == 43),
vec_position!(v, |i| *i == 50),
]
}
assert_eq!(pos(), [Some(1), None]);
}
#[quickcheck]
fn vec_position(values: Vec<u8>, expected_index: usize) -> TestResult {
if values.len() > MAX_LEN {
return TestResult::discard();
}
let needle = if values.is_empty() {
42
} else {
values[expected_index % values.len()]
};
// Convert `values` into `ComptimeVec`
let mut vec = ComptimeVec::new();
for &e in values.iter() {
vec.push(e);
}
let got = vec_position!(vec, |i| *i == needle);
let expected = values.iter().position(|i| *i == needle);
assert_eq!(got, expected);
TestResult::passed()
}
}