@@ -189,15 +189,10 @@ macro_rules! impl_packed_array {
189
189
///
190
190
/// If `index` is out of bounds.
191
191
pub fn get( & self , index: usize ) -> Option <$Element> {
192
- if index >= self . len( ) {
193
- return None ;
194
- }
192
+ let ptr = self . ptr_or_none( index) ?;
195
193
196
- // SAFETY: bounds check above.
197
- unsafe {
198
- let ptr = self . ptr_unchecked( index) ;
199
- Some ( ( * ptr) . clone( ) )
200
- }
194
+ // SAFETY: if index was out of bounds, `ptr` would be `None` and return early.
195
+ unsafe { Some ( ( * ptr) . clone( ) ) }
201
196
}
202
197
203
198
/// Finds the index of an existing value in a sorted array using binary search.
@@ -278,10 +273,11 @@ macro_rules! impl_packed_array {
278
273
/// the array's elements after the inserted element. The larger the array, the slower
279
274
/// `insert` will be.
280
275
pub fn insert( & mut self , index: usize , value: $Element) {
281
- let len = self . len( ) ;
282
- assert!(
283
- index <= len,
284
- "Array insertion index {index} is out of bounds: length is {len}" ) ;
276
+ // Intentional > and not >=.
277
+ if index > self . len( ) {
278
+ self . panic_out_of_bounds( index) ;
279
+ }
280
+
285
281
self . as_inner( ) . insert( to_i64( index) , Self :: into_arg( value) ) ;
286
282
}
287
283
@@ -332,14 +328,12 @@ macro_rules! impl_packed_array {
332
328
// Include specific functions in the code only if the Packed*Array provides the function.
333
329
impl_specific_packed_array_functions!( $PackedArray) ;
334
330
335
- /// Asserts that the given index refers to an existing element.
336
- ///
331
+
337
332
/// # Panics
338
333
///
339
- /// If `index` is out of bounds.
340
- fn check_bounds( & self , index: usize ) {
341
- let len = self . len( ) ;
342
- assert!( index < len, "Array index {index} is out of bounds: length is {len}" ) ;
334
+ /// Always.
335
+ fn panic_out_of_bounds( & self , index: usize ) -> ! {
336
+ panic!( "Array index {index} is out of bounds: length is {}" , self . len( ) ) ;
343
337
}
344
338
345
339
/// Returns a pointer to the element at the given index.
@@ -348,28 +342,21 @@ macro_rules! impl_packed_array {
348
342
///
349
343
/// If `index` is out of bounds.
350
344
fn ptr( & self , index: usize ) -> * const $Element {
351
- self . check_bounds( index) ;
352
-
353
- // SAFETY: We just checked that the index is not out of bounds.
354
- let ptr = unsafe {
355
- let item_ptr: * const $IndexRetType =
356
- ( interface_fn!( $operator_index_const) ) ( self . sys( ) , to_i64( index) ) ;
357
- item_ptr as * const $Element
358
- } ;
359
- assert!( !ptr. is_null( ) ) ;
360
- ptr
345
+ self . ptr_or_none( index) . unwrap_or_else( || self . panic_out_of_bounds( index) )
361
346
}
362
347
363
- /// Returns a pointer to the element at the given index, or `None` if `index` is out of bounds.
364
- ///
365
- /// # Safety
366
- /// Bound checking is responsibility of the caller.
367
- unsafe fn ptr_unchecked( & self , index: usize ) -> * const $Element {
368
- let item_ptr: * const $IndexRetType = interface_fn!( $operator_index_const) ( self . sys( ) , to_i64( index) ) ;
369
- let ptr = item_ptr as * const $Element;
348
+ /// Returns a pointer to the element at the given index, or `None` if out of bounds.
349
+ fn ptr_or_none( & self , index: usize ) -> Option <* const $Element> {
350
+ // SAFETY: The packed array index operators return a null pointer on out-of-bounds.
351
+ let item_ptr: * const $IndexRetType = unsafe {
352
+ interface_fn!( $operator_index_const) ( self . sys( ) , to_i64( index) )
353
+ } ;
370
354
371
- assert!( !ptr. is_null( ) ) ;
372
- ptr
355
+ if item_ptr. is_null( ) {
356
+ None
357
+ } else {
358
+ Some ( item_ptr as * const $Element)
359
+ }
373
360
}
374
361
375
362
/// Returns a mutable pointer to the element at the given index.
@@ -378,16 +365,16 @@ macro_rules! impl_packed_array {
378
365
///
379
366
/// If `index` is out of bounds.
380
367
fn ptr_mut( & mut self , index: usize ) -> * mut $Element {
381
- self . check_bounds( index) ;
368
+ // SAFETY: The packed array index operators return a null pointer on out-of-bounds.
369
+ let item_ptr: * mut $IndexRetType = unsafe {
370
+ interface_fn!( $operator_index) ( self . sys_mut( ) , to_i64( index) )
371
+ } ;
382
372
383
- // SAFETY: We just checked that the index is not out of bounds.
384
- let ptr = unsafe {
385
- let item_ptr: * mut $IndexRetType =
386
- ( interface_fn!( $operator_index) ) ( self . sys_mut( ) , to_i64( index) ) ;
373
+ if item_ptr. is_null( ) {
374
+ self . panic_out_of_bounds( index)
375
+ } else {
387
376
item_ptr as * mut $Element
388
- } ;
389
- assert!( !ptr. is_null( ) ) ;
390
- ptr
377
+ }
391
378
}
392
379
393
380
#[ doc = concat!( "Converts a `" , stringify!( $Element) , "` into a value that can be" ) ]
0 commit comments