intid_core/lib.rs
1//! Defines the [`IntegerId`] trait, for types that can be identified by an integer value.
2//!
3//! This contains all the same types that the [`intid`] crate does,
4//! but has no dependency on [`intid_derive`] (even when the `intid/derive` feature is enabled).
5//! This reduces compile times, similar to the separation between `serde_core` and `serde` introduced in [serde-rs/serde#2608].
6//!
7//! It may be convenient to rename the `intid_core` dependency to `intid` using [dependency renaming].
8//! ```toml
9//! intid = { version = "0.3", package = "intid_core" }
10//! ```
11//! This renaming comes at no loss of clarity,
12//! since the items in `intid_core` are simply a subset of the items in the `intid` crate.
13//! If for some reason you decide to use `intid_derive` directly without depending on `intid`,
14//! then you will need to do this renaming since the derived code references the `intid` crate.
15//!
16//! [dependency renaming]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
17//! [serde-rs/serde#2608]: https://github.com/serde-rs/serde/pull/2608
18//! [`intid`]: https://docs.rs/intid/latest/intid
19//! [`intid_derive`]: https://docs.rs/intid-derive/latest/intid_derive
20#![no_std]
21#![cfg_attr(feature = "nightly", feature(never_type,))]
22extern crate alloc;
23
24use core::fmt::Debug;
25
26#[macro_use]
27mod macros;
28#[doc(hidden)]
29pub mod array;
30mod impls;
31pub mod trusted;
32pub mod uint;
33pub mod utils;
34
35pub use uint::UnsignedPrimInt;
36
37/// An identifier which can be sensibly converted to/from an unsigned integer value.
38///
39///
40/// The type should not carry any information beyond that of the integer index,
41/// and be able to losslessly convert back and forth from [`Self::Int`].
42/// It is possible that not all values of the underlying integer type are valid,
43/// allowing [`core::num::NonZero`] and C-like enums to implement this trait.
44///
45///
46/// This is intended mostly for newtype wrappers around integer indexes,
47/// and the primitive integer types themselves.
48///
49/// The value of the underlying integer must be consistent.
50/// It cannot change over the course of the program's lifetime.
51///
52/// ## Safety
53/// With one exception, this trait is safe to implement and cannot be relied upon by memory safety.
54///
55/// If the implementation of [`IntegerId::from_int_unchecked`] makes any sort of unsafe assumptions
56/// about the validity of the input, then the rest of the trait must be implemented correctly.
57/// This means that implementations of this trait fall into two categories:
58/// 1. Potentially incorrect implemented entirely using safe code, where `from_int_unchecked(x)`
59/// is equivalent to calling `from_int_checked(x).unwrap()`;
60/// 2. Traits where `from_int_unchecked` could trigger undefined behavior on an invalid value,
61/// but every other part of this trait can be trusted to be implemented correctly.
62///
63/// In both these cases, the following code is always safe:
64/// ```no_run
65/// # use intid_core::IntegerId;
66/// fn foo<T: IntegerId>(x: T) -> T {
67/// let y = x.to_int();
68/// let z = unsafe { T::from_int_unchecked(y) };
69/// z
70/// }
71/// ```
72/// In case 1, it doesn't matter if [`x.to_int()`](Self::to_int) produces garbage data,
73/// because `T::from_int_unchecked` method is safe to call.
74/// In case 2, the `to_int` method can be trusted to produce a valid value `y` that cannot fail
75/// when passed to `T::from_int_unchecked`.
76///
77/// The requirement for correctness in this case also apply to all sub-traits in this crate,
78/// including [`IntegerIdContiguous`] and [`IntegerIdCounter`].
79/// So an unsafe implementation of `from_int_unchecked` can be similarly trusted to accept
80/// all integer values between [`IntegerId::MIN_ID`] and [`IntegerId::MAX_ID`].
81///
82/// This restriction allows avoiding unnecessary checks when ids are stored to/from another data structure.
83/// Despite this requirement, I still consider this trait safe to implement,
84/// because safety can only be violated by an unsafe implementation of`from_int_unchecked`.
85///
86/// This type should not have interior mutability.
87/// This is guaranteed by the `Copy` bound.
88pub trait IntegerId: Copy + Eq + Debug + Send + Sync + 'static {
89 /// The underlying integer type.
90 ///
91 /// Every valid instance of `Self` should correspond to a valid `Self::Int`.
92 /// However, the other direction may not always be true.
93 type Int: uint::UnsignedPrimInt;
94 /// The value of this type with the smallest integer value,
95 /// or `None` if this type is uninhabited.
96 const MIN_ID: Option<Self>;
97 /// The value of this type with the largest integer value,
98 /// or `None` if this type is uninhabited.
99 const MAX_ID: Option<Self>;
100 /// The value of [`Self::MIN_ID`] a primitive integer,
101 /// or `None` if this type is uninhabited.
102 ///
103 /// This is necessary because trait methods cannot be marked `const`.
104 const MIN_ID_INT: Option<Self::Int>;
105 /// The value of [`Self::MAX_ID`] a primitive integer,
106 /// or `None` if this type is uninhabited.
107 ///
108 /// This is necessary because trait methods cannot be marked `const`.
109 const MAX_ID_INT: Option<Self::Int>;
110
111 /// Indicates that the type's implementation of [`IntegerId::to_int`] can be trusted
112 /// to only return values in the range `MIN_ID_INT..=MAX_ID_INT`.
113 ///
114 /// Creating this token means that all of these guarantees can be relied upon for memory safety.
115 /// This allows unsafe code to avoid bounds checks,
116 /// but turns a correctness invariant into a soundness invariant.
117 ///
118 /// # Safety
119 /// The result of [`Self::to_int`] must always fall in the range `MIN_ID_INT..=MAX_ID_INT`.
120 ///
121 /// If [`EnumId`] is implemented,
122 /// then the requirements of the [`EnumId`] trait must be met as well.
123 /// In particular, the index must always fit in a `u32`
124 /// and have the appropriately `Array` and `BitSet` items.
125 const TRUSTED_RANGE: Option<trusted::TrustedRangeToken<Self>> = None;
126
127 /// Create an id from the underlying integer value,
128 /// panicking if the value is invalid.
129 ///
130 /// ## Correctness
131 /// A value returned by this method should never trigger
132 /// an error if passed to [`Self::from_int_checked`].
133 /// This means the validity of certain ids can't change over the course of the program.
134 #[inline]
135 #[track_caller]
136 fn from_int(id: Self::Int) -> Self {
137 match Self::from_int_checked(id) {
138 Some(success) => success,
139 None => uint::invalid_id(id),
140 }
141 }
142
143 /// Create an id from the underlying integer value,
144 /// returning `None` if the value is invalid.
145 fn from_int_checked(id: Self::Int) -> Option<Self>;
146
147 /// Create an id from the underlying integer value,
148 /// triggering undefined behavior if the value is invalid.
149 ///
150 /// ## Safety
151 /// If the corresponding [`Self::from_int_checked`] method would fail,
152 /// this triggers undefined behavior.
153 /// The default implementation just invokes [`Self::from_int`].
154 #[inline]
155 unsafe fn from_int_unchecked(id: Self::Int) -> Self {
156 Self::from_int(id)
157 }
158
159 /// Convert this id into an underlying integer type.
160 ///
161 /// This method can never fail,
162 /// since valid instances `Self` always correspond to valid instances of `Self::Int`.
163 fn to_int(self) -> Self::Int;
164}
165
166/// Indicates that an id occupies contiguous range of contiguous values,
167/// and all values between [`IntegerId::MIN_ID`] and [`IntegerId::MAX_ID`] are valid.
168///
169/// This is similar to [`bytemuck::Contiguous`].
170/// However, since it is safe to implement,
171/// it must not be relied upon for correctness.
172///
173/// ## Safety
174/// This trait is safe to implement, so may not usually be relied upon for memory safety.
175///
176/// However, if [`Self::from_int_unchecked`](IntegerId::from_int_unchecked) makes unsafe assumptions (satisfying the condition set forth in the [`IntegerId`] safety docs),
177/// then this trait must also be implemented correctly.
178/// More specifically, all integers between [`IntegerId::MIN_ID`] and [`IntegerId::MAX_ID`] must be valid
179/// and cannot fail when passed to [`IntegerId::from_int_checked`].
180pub trait IntegerIdContiguous: IntegerId {}
181
182/// An [`IntegerId`] that can be sensibly used as a counter,
183/// starting at a [`Self::START`] value and being incremented from there.
184///
185/// This is used by the `intid-allocator` crate to provide an atomic counter to allocate new ids.
186/// It also provides more complex allocators that can reuse ids that have been freed.
187///
188/// This type cannot be implemented for uninhabited types like [`core::convert::Infallible`] or `!`,
189/// as there is no valid implementation of [`Self::START`].
190pub trait IntegerIdCounter: IntegerId + IntegerIdContiguous {
191 /// Where a counter a should start from.
192 ///
193 /// This should be the [`Default`] value if one is defined.
194 /// It is usually equal to the [`IntegerId::MIN_ID`],
195 /// but this is not required.
196 const START: Self;
197 /// The value of [`Self::START`] as a [`T::Int`](IntegerId::Int).
198 ///
199 /// This is necessary because trait methods ([`IntegerId::to_int`])
200 /// can not currently be const methods.
201 const START_INT: Self::Int;
202
203 /// Increment this value by the specified offset,
204 /// returning `None` if the value overflows or is invalid.
205 ///
206 /// This should behave consistently with [`IntegerIdContiguous`]
207 /// and [`IntegerId::from_int_checked`].
208 /// However, that can not be relied upon for memory safety.
209 ///
210 /// This is implemented as an associated method to avoid namespace pollution.
211 #[inline]
212 fn checked_add(this: Self, offset: Self::Int) -> Option<Self> {
213 uint::checked_add(this.to_int(), offset).and_then(Self::from_int_checked)
214 }
215
216 /// Increment this value by the specified offset,
217 /// returning `None` if the value overflows or is invalid.
218 ///
219 /// This should behave consistently with [`IntegerIdContiguous`]
220 /// and [`IntegerId::from_int_checked`].
221 /// However, that can not be relied upon for memory safety.
222 ///
223 /// This is implemented as an associated method to avoid namespace pollution.
224 #[inline]
225 fn checked_sub(this: Self, offset: Self::Int) -> Option<Self> {
226 uint::checked_sub(this.to_int(), offset).and_then(Self::from_int_checked)
227 }
228}
229
230/// An [`IntegerId`] which are limited to small set of indexes.
231///
232/// As the name suggests, it is most useful for C-style enums,
233/// allowing use with [`EnumMap`] and [`EnumSet`] collections which do not do any allocation.
234/// Is not implemented for types like `u32` where inline storage
235/// would require inordinate amounts of space (an `EnumMap<u32, u8>`would take 4GiB).
236///
237/// For this reason, all valid indexes and the value [`Self::MAX_ID_INT + 1`](IntegerId::MAX_ID_INT)
238/// must fit into both a [`u32`] and a [`usize`].
239/// This requirement prevents a `u32` from implementing `EnumId`,
240/// as `u32::MAX + 1` exceeds 32-bits. Implementing `EnumId for u32` would also be invalid on
241/// 16-bit architectures, as then a `u32` index would not fit into a `usize`.
242///
243/// Note that this trait does not imply [`IntegerIdContiguous`].
244/// This means that while [`x <= Self::MAX_ID_INT`](IntegerId::MAX_ID_INT) is a necessary condition
245/// for [`Self::from_int(x)`](IntegerId::from_int) to succeed, it is not always a sufficient condition.
246///
247/// [`EnumMap`]: https://docs.rs/idmap/latest/idmap/enums/map/struct.EnumMap.html
248/// [`EnumSet`]: https://docs.rs/idmap/latest/idmap/enums/set/struct.EnumSet.html
249pub trait EnumId: IntegerId {
250 /// The total number of valid values.
251 ///
252 /// This value is at most equal to [`Self::MAX_ID_INT`](IntegerId::MAX_ID_INT),
253 /// which must fit in a [`u32`] due to the trait requirements.
254 const COUNT: u32;
255 /// A builtin array of `[T; {Self::MAX_ID_INT + 1}]`.
256 ///
257 /// Necessary to work around the current (Rust 1.90) restrictions on const generics
258 ///
259 /// # Safety
260 /// Since the [`array::Array`] trait is sealed,
261 /// this is guaranteed to be a builtin array of type `T`.
262 /// Since this is a safe trait, the length could be any value.
263 /// However, that is easily checked using a const assertion.
264 type Array<T>: array::Array<T>;
265 /// An array of words, whose bits can store all valid ids.
266 ///
267 /// Necessary to work around the current (Rust 1.90) restrictions on const generics.
268 ///
269 /// # Safety
270 /// Has similar safety guarantees as [`Self::Array`].
271 /// The type is correct, but the length must be checked with a const assertion.
272 type BitSet: array::Array<array::BitsetLimb>;
273}
274impl EnumId for u8 {
275 const COUNT: u32 = {
276 assert!(u8::MAX as u32 + 1 == 256);
277 256
278 };
279 type Array<T> = [T; 256];
280 type BitSet = [u64; 4];
281}
282
283/// A type that can be for lookup as an [`IntegerId`].
284///
285/// Used for key lookup in maps, similar to [`core::borrow::Borrow`] or [`equivalent::Equivalent`].
286/// These traits are not suitable for id maps,
287/// which need conversion to integers rather than hashing/equality.
288///
289/// [`equivalent::Equivalent`]: https://docs.rs/equivalent/latest/equivalent/trait.Equivalent.html
290pub trait EquivalentId<K: IntegerId> {
291 /// Convert this type to an id `K`.
292 fn as_id(&self) -> K;
293}
294impl<K: IntegerId> EquivalentId<K> for K {
295 #[inline]
296 fn as_id(&self) -> K {
297 *self
298 }
299}
300impl<K: IntegerId> EquivalentId<K> for &'_ K {
301 #[inline]
302 fn as_id(&self) -> K {
303 **self
304 }
305}
306impl<K: IntegerId> EquivalentId<K> for &'_ mut K {
307 #[inline]
308 fn as_id(&self) -> K {
309 **self
310 }
311}