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
//! Index types. See [`::index_vec`].
use std::fmt;
pub use index_vec::{
Idx, IdxRangeBounds, IdxSliceIndex, IndexBox, IndexSlice, IndexVec, index_box, index_vec,
};
/// Creates a new index to use with [`::index_vec`].
#[macro_export]
macro_rules! newtype_index {
() => {};
($(#[$attr:meta])* $vis:vis struct $name:ident; $($rest:tt)*) => {
$(#[$attr])*
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
$vis struct $name($crate::index::BaseIndex32);
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({:?})", stringify!($name), self.get())
}
}
impl $crate::index::Idx for $name {
#[inline(always)]
fn from_usize(value: usize) -> Self {
Self::from_usize(value)
}
#[inline(always)]
fn index(self) -> usize {
self.index()
}
}
impl $name {
/// The maximum index value.
$vis const MAX: Self = Self($crate::index::BaseIndex32::MAX);
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
$vis const fn new(value: u32) -> Self {
Self($crate::index::BaseIndex32::new(value))
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
#[must_use]
$vis const unsafe fn new_unchecked(value: u32) -> Self {
Self(unsafe { $crate::index::BaseIndex32::new_unchecked(value) })
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// Returns `None` if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
$vis const fn try_new(value: u32) -> Option<Self> {
match $crate::index::BaseIndex32::try_new(value) {
Some(value) => Some(Self(value)),
None => None,
}
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
$vis const fn from_usize(value: usize) -> Self {
Self($crate::index::BaseIndex32::from_usize(value))
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
#[must_use]
$vis const unsafe fn from_usize_unchecked(value: usize) -> Self {
Self(unsafe { $crate::index::BaseIndex32::from_usize_unchecked(value) })
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// Returns `None` if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
$vis const fn try_from_usize(value: usize) -> Option<Self> {
match $crate::index::BaseIndex32::try_from_usize(value) {
Some(value) => Some(Self(value)),
None => None,
}
}
/// Returns the underlying index value.
#[inline(always)]
#[must_use]
$vis const fn get(self) -> u32 {
self.0.get()
}
/// Returns the underlying index value.
#[inline(always)]
#[must_use]
$vis const fn index(self) -> usize {
self.0.index()
}
}
$crate::newtype_index!($($rest)*);
};
}
// NOTE: The max MUST be less than the maximum value of the underlying integer.
macro_rules! base_index {
($(#[$attr:meta])* $name:ident($primitive:ident <= $max:literal)) => {
/// A specialized wrapper around a primitive number.
///
$(#[$attr])*
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "nightly", rustc_layout_scalar_valid_range_end($max))]
#[cfg_attr(feature = "nightly", rustc_nonnull_optimization_guaranteed)]
#[cfg_attr(feature = "nightly", rustc_pass_by_value)]
#[repr(transparent)]
pub struct $name {
// NOTE: Use `value()` instead of projecting the field directly.
#[cfg(feature = "nightly")]
value: $primitive,
#[cfg(not(feature = "nightly"))]
value: std::num::NonZero<$primitive>,
}
impl fmt::Debug for $name {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
impl Idx for $name {
#[inline(always)]
fn from_usize(value: usize) -> Self {
Self::from_usize(value)
}
#[inline(always)]
fn index(self) -> usize {
self.index()
}
}
impl $name {
/// The maximum index value, as the underlying primitive type.
pub const MAX_AS: $primitive = $max;
/// The maximum index value.
pub const MAX: Self = Self::new(Self::MAX_AS);
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
pub const fn new(value: $primitive) -> Self {
match Self::try_new(value) {
Some(value) => value,
None => index_overflow(),
}
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// Returns `None` if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
pub const fn try_new(value: $primitive) -> Option<Self> {
if value > Self::MAX_AS {
None
} else {
// SAFETY: `value` is less than or equal to `MAX`.
Some(unsafe { Self::new_unchecked(value) })
}
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// # Panics
///
/// Panics if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
#[cfg_attr(debug_assertions, track_caller)]
pub const fn from_usize(value: usize) -> Self {
match Self::try_from_usize(value) {
Some(value) => value,
None => index_overflow(),
}
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`."]
///
/// Returns `None` if `value` exceeds `MAX`.
#[inline(always)]
#[must_use]
pub const fn try_from_usize(value: usize) -> Option<Self> {
if value > Self::MAX_AS as usize {
None
} else {
// SAFETY: `value` is less than or equal to `MAX`.
Some(unsafe { Self::new_unchecked(value as $primitive) })
}
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`, without checking for overflow."]
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
#[must_use]
pub const unsafe fn new_unchecked(value: $primitive) -> Self {
debug_assert!(value <= Self::MAX_AS);
// SAFETY: guaranteed by the caller.
#[cfg(feature = "nightly")]
return unsafe { std::intrinsics::transmute_unchecked(value) };
#[cfg(not(feature = "nightly"))]
return unsafe { Self { value: std::num::NonZero::new_unchecked(value.unchecked_add(1)) } };
}
#[doc = "Creates a new `"]
#[doc = stringify!($name)]
#[doc = "` from the given `value`, without checking for overflow."]
///
/// # Safety
///
/// The caller must ensure that `value` is less than or equal to `MAX`.
#[inline(always)]
#[must_use]
pub const unsafe fn from_usize_unchecked(value: usize) -> Self {
debug_assert!(value <= Self::MAX_AS as usize);
unsafe { Self::new_unchecked(value as $primitive) }
}
/// Returns the underlying index value.
#[inline(always)]
#[must_use]
pub const fn get(self) -> $primitive {
// SAFETY: Transmute instead of projecting the field directly.
//
// See:
// - https://github.com/rust-lang/rust/pull/133651
// - https://github.com/rust-lang/compiler-team/issues/807
#[cfg(feature = "nightly")]
return unsafe { std::intrinsics::transmute_unchecked(self) };
// SAFETY: non-zero minus one doesn't overflow.
#[cfg(not(feature = "nightly"))]
return unsafe { self.value.get().unchecked_sub(1) };
}
/// Returns the underlying index value.
#[inline(always)]
#[must_use]
pub const fn index(self) -> usize {
self.get() as usize
}
}
};
}
base_index!(BaseIndex32(u32 <= 0xFFFF_FF00));
#[inline(never)]
#[cold]
#[cfg_attr(debug_assertions, track_caller)]
const fn index_overflow() -> ! {
panic!("index overflowed")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn base_index() {
assert_eq!(BaseIndex32::new(0).get(), 0);
assert_eq!(BaseIndex32::new(1).get(), 1);
assert_eq!(BaseIndex32::MAX.get(), BaseIndex32::MAX_AS);
assert_eq!(BaseIndex32::MAX.get(), 0xFFFF_FF00);
assert_eq!(BaseIndex32::new(0xFFFF_FF00).get(), 0xFFFF_FF00);
}
}