@@ -429,6 +429,7 @@ impl<T, A: Allocator> RawVec<T, A> {
|
429 | 429 | /// |
430 | 430 | /// Aborts on OOM. |
431 | 431 | #[cfg(not(no_global_oom_handling))] |
| 432 | +#[inline] |
432 | 433 | pub fn shrink_to_fit(&mut self, cap: usize) { |
433 | 434 | if let Err(err) = self.shrink(cap) { |
434 | 435 | handle_error(err); |
@@ -511,9 +512,25 @@ impl<T, A: Allocator> RawVec<T, A> {
|
511 | 512 | } |
512 | 513 | |
513 | 514 | #[cfg(not(no_global_oom_handling))] |
| 515 | +#[inline] |
514 | 516 | fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> { |
515 | 517 | assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity"); |
| 518 | +// SAFETY: Just checked this isn't trying to grow |
| 519 | +unsafe { self.shrink_unchecked(cap) } |
| 520 | +} |
516 | 521 | |
| 522 | +/// `shrink`, but without the capacity check. |
| 523 | + /// |
| 524 | + /// This is split out so that `shrink` can inline the check, since it |
| 525 | + /// optimizes out in things like `shrink_to_fit`, without needing to |
| 526 | + /// also inline all this code, as doing that ends up failing the |
| 527 | + /// `vec-shrink-panic` codegen test when `shrink_to_fit` ends up being too |
| 528 | + /// big for LLVM to be willing to inline. |
| 529 | + /// |
| 530 | + /// # Safety |
| 531 | + /// `cap <= self.capacity()` |
| 532 | + #[cfg(not(no_global_oom_handling))] |
| 533 | +unsafe fn shrink_unchecked(&mut self, cap: usize) -> Result<(), TryReserveError> { |
517 | 534 | let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) }; |
518 | 535 | // See current_memory() why this assert is here |
519 | 536 | const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) }; |
|