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
//! Since [`IntegerId`] and [`IntegerIdContiguous`] are safe traits,
//! it is not possible for unsafe code to rely on them for correctness.
//!
//! There is an exception for [`IntegerId::from_int_unchecked`],
//! which is required to accept any value returned from [`IntegerId::to_int`].
//! If [`IntegerIdContiguous`] is implemented,
//! the `from_int_unchecked` function must also accept all values in the range `Self::MIN_ID..=Self::MAX_ID`.
//!
//! These assumptions allow zero-cost conversions from `T::Int` to `T`,
//! but do not affect fully-safe implementations of `IntegerId`
//! where [`IntegerId::from_int_unchecked`] simply delegates to [`IntegerId::from_int`].
//!
//! Because it is a fully safe function in a fully safe trait,
//! there is no way for us to trust its results.
//!
//! In order to work around this, we provide special "trust tokens".
//! These tokens are associated constants on the trait,
//! so that they are always resolved at compile time.
//! These require invoking an `unsafe` code to construct the token,
//! to prevent fully safe code from being able to trigger undefined behavior.
//!
//! [`IntegerIdContiguous`]: crate::IntegerIdContiguous
use PhantomData;
use crateIntegerId;
/// Indicates that an [`IntegerId`] unsafely guarantees that the result of [`IntegerId::to_int`]
/// will always fall in the range `IntegerId::MIN_INT..=IntegerId::MAX_ID`.
///
/// Also guarantees that the [`IntegerId::to_int`] is implemented in the expected manner.
///
/// Just because the type implements something like [`bytemuck::Contiguous`] does not mean
/// that it is valid to create the token, as [`IntegerId::to_int`] could still be implemented incorrectly.