@@ -212,7 +212,6 @@ where
212
212
/// # Safety
213
213
///
214
214
/// - `count` must be less than or equal to `self.len`.
215
- #[ expect( unused) ]
216
215
unsafe fn dec_len ( & mut self , count : usize ) -> & mut [ T ] {
217
216
debug_assert ! ( count <= self . len( ) ) ;
218
217
// INVARIANT: We relinquish ownership of the elements within the range `[self.len - count,
@@ -492,23 +491,15 @@ where
492
491
/// # Ok::<(), Error>(())
493
492
/// ```
494
493
pub fn truncate ( & mut self , len : usize ) {
495
- if len >= self . len ( ) {
496
- return ;
494
+ if let Some ( count) = self . len ( ) . checked_sub ( len) {
495
+ // SAFETY: `count` is `self.len() - len` so it is guaranteed to be less than or
496
+ // equal to `self.len()`.
497
+ let ptr: * mut [ T ] = unsafe { self . dec_len ( count) } ;
498
+
499
+ // SAFETY: the contract of `dec_len` guarantees that the elements in `ptr` are
500
+ // valid elements whose ownership has been transferred to the caller.
501
+ unsafe { ptr:: drop_in_place ( ptr) } ;
497
502
}
498
-
499
- let drop_range = len..self . len ( ) ;
500
-
501
- // SAFETY: `drop_range` is a subrange of `[0, len)` by the bounds check above.
502
- let ptr: * mut [ T ] = unsafe { self . get_unchecked_mut ( drop_range) } ;
503
-
504
- // SAFETY: By the above bounds check, it is guaranteed that `len < self.capacity()`.
505
- unsafe { self . set_len ( len) } ;
506
-
507
- // SAFETY:
508
- // - the dropped values are valid `T`s by the type invariant
509
- // - we are allowed to invalidate [`new_len`, `old_len`) because we just changed the
510
- // len, therefore we have exclusive access to [`new_len`, `old_len`)
511
- unsafe { ptr:: drop_in_place ( ptr) } ;
512
503
}
513
504
}
514
505
0 commit comments