@@ -118,32 +118,54 @@ macro_rules! newtype_index {
118
118
}
119
119
120
120
impl $type {
121
+ /// Maximum value the index can take, as a `u32`.
121
122
$v const MAX_AS_U32 : u32 = $max;
122
123
124
+ /// Maximum value the index can take.
123
125
$v const MAX : Self = Self :: from_u32( $max) ;
124
126
127
+ /// Creates a new index from a given `usize`.
128
+ ///
129
+ /// # Panics
130
+ ///
131
+ /// Will panic if `value` exceeds `MAX`.
125
132
#[ inline]
126
133
$v const fn from_usize( value: usize ) -> Self {
127
134
assert!( value <= ( $max as usize ) ) ;
135
+ // SAFETY: We just checked that `value <= max`.
128
136
unsafe {
129
137
Self :: from_u32_unchecked( value as u32 )
130
138
}
131
139
}
132
140
141
+ /// Creates a new index from a given `u32`.
142
+ ///
143
+ /// # Panics
144
+ ///
145
+ /// Will panic if `value` exceeds `MAX`.
133
146
#[ inline]
134
147
$v const fn from_u32( value: u32 ) -> Self {
135
148
assert!( value <= $max) ;
149
+ // SAFETY: We just checked that `value <= max`.
136
150
unsafe {
137
151
Self :: from_u32_unchecked( value)
138
152
}
139
153
}
140
154
155
+ /// Creates a new index from a given `u32`.
156
+ ///
157
+ /// # Safety
158
+ ///
159
+ /// The provided value must be less than or equal to the maximum value for the newtype.
160
+ /// Providing a value outside this range is undefined due to layout restrictions.
161
+ ///
162
+ /// Prefer using `from_u32`.
141
163
#[ inline]
142
164
$v const unsafe fn from_u32_unchecked( value: u32 ) -> Self {
143
165
Self { private: value }
144
166
}
145
167
146
- /// Extracts the value of this index as an integer .
168
+ /// Extracts the value of this index as a `usize` .
147
169
#[ inline]
148
170
$v const fn index( self ) -> usize {
149
171
self . as_usize( )
0 commit comments