File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -118,32 +118,54 @@ macro_rules! newtype_index {
118118 }
119119
120120 impl $type {
121+ /// Maximum value the index can take, as a `u32`.
121122 $v const MAX_AS_U32 : u32 = $max;
122123
124+ /// Maximum value the index can take.
123125 $v const MAX : Self = Self :: from_u32( $max) ;
124126
127+ /// Creates a new index from a given `usize`.
128+ ///
129+ /// # Panics
130+ ///
131+ /// Will panic if `value` exceeds `MAX`.
125132 #[ inline]
126133 $v const fn from_usize( value: usize ) -> Self {
127134 assert!( value <= ( $max as usize ) ) ;
135+ // SAFETY: We just checked that `value <= max`.
128136 unsafe {
129137 Self :: from_u32_unchecked( value as u32 )
130138 }
131139 }
132140
141+ /// Creates a new index from a given `u32`.
142+ ///
143+ /// # Panics
144+ ///
145+ /// Will panic if `value` exceeds `MAX`.
133146 #[ inline]
134147 $v const fn from_u32( value: u32 ) -> Self {
135148 assert!( value <= $max) ;
149+ // SAFETY: We just checked that `value <= max`.
136150 unsafe {
137151 Self :: from_u32_unchecked( value)
138152 }
139153 }
140154
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`.
141163 #[ inline]
142164 $v const unsafe fn from_u32_unchecked( value: u32 ) -> Self {
143165 Self { private: value }
144166 }
145167
146- /// Extracts the value of this index as an integer .
168+ /// Extracts the value of this index as a `usize` .
147169 #[ inline]
148170 $v const fn index( self ) -> usize {
149171 self . as_usize( )
You can’t perform that action at this time.
0 commit comments