From 08f403fc3e79ad7ad9c00c7d8178b2ca70e6e160 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 20 Mar 2025 13:35:13 -0500 Subject: [PATCH 1/2] `Vec`: rename `storage_capacity` to `capacity` and remove the `const` version --- src/binary_heap.rs | 2 +- src/linear_map.rs | 2 +- src/string/mod.rs | 2 +- src/vec/mod.rs | 17 +++++------------ 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/binary_heap.rs b/src/binary_heap.rs index a67a967d16..7cfcc97d5f 100644 --- a/src/binary_heap.rs +++ b/src/binary_heap.rs @@ -203,7 +203,7 @@ where /* Public API */ /// Returns the capacity of the binary heap. pub fn capacity(&self) -> usize { - self.data.storage_capacity() + self.data.capacity() } /// Drops all items from the binary heap. diff --git a/src/linear_map.rs b/src/linear_map.rs index b805096acc..718eeff8f7 100644 --- a/src/linear_map.rs +++ b/src/linear_map.rs @@ -67,7 +67,7 @@ where /// assert_eq!(map.capacity(), 8); /// ``` pub fn capacity(&self) -> usize { - self.buffer.storage_capacity() + self.buffer.capacity() } /// Clears the map, removing all key-value pairs. diff --git a/src/string/mod.rs b/src/string/mod.rs index 962d8f56e6..6aee8f270f 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -455,7 +455,7 @@ impl + ?Sized> StringInner { /// ``` #[inline] pub fn capacity(&self) -> usize { - self.vec.storage_capacity() + self.vec.capacity() } /// Appends the given [`char`] to the end of this `String`. diff --git a/src/vec/mod.rs b/src/vec/mod.rs index d9b3926860..89fae0acf5 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -362,13 +362,6 @@ impl Vec { { self.as_mut_view().drain(range) } - - /// Returns the maximum number of elements the vector can hold. - /// - /// This method is not available on a `VecView`, use [`storage_len`](VecInner::storage_capacity) instead - pub const fn capacity(&self) -> usize { - self.buffer.buffer.len() - } } impl VecView { @@ -486,7 +479,7 @@ impl + ?Sized> VecInner { } /// Returns the maximum number of elements the vector can hold. - pub fn storage_capacity(&self) -> usize { + pub fn capacity(&self) -> usize { self.buffer.borrow().len() } @@ -565,7 +558,7 @@ impl + ?Sized> VecInner { /// /// Returns back the `item` if the vector is full. pub fn push(&mut self, item: T) -> Result<(), T> { - if self.len < self.storage_capacity() { + if self.len < self.capacity() { unsafe { self.push_unchecked(item) } Ok(()) } else { @@ -639,7 +632,7 @@ impl + ?Sized> VecInner { where T: Clone, { - if new_len > self.storage_capacity() { + if new_len > self.capacity() { return Err(()); } @@ -759,7 +752,7 @@ impl + ?Sized> VecInner { /// Normally, here, one would use [`clear`] instead to correctly drop /// the contents and thus not leak memory. pub unsafe fn set_len(&mut self, new_len: usize) { - debug_assert!(new_len <= self.storage_capacity()); + debug_assert!(new_len <= self.capacity()); self.len = new_len } @@ -835,7 +828,7 @@ impl + ?Sized> VecInner { /// Returns true if the vec is full pub fn is_full(&self) -> bool { - self.len == self.storage_capacity() + self.len == self.capacity() } /// Returns true if the vec is empty From fde68472b3573d402b57154fcd17c96085787ffd Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 20 Mar 2025 13:38:58 -0500 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcfc3d015b..03c4e1c7bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Changed `stable_deref_trait` to a platform-dependent dependency. - Changed `SortedLinkedList::pop` return type from `Result` to `Option` to match `std::vec::pop`. +- `Vec::capacity` is no longer a `const` function. ### Fixed @@ -56,6 +57,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed `MpMcQueue` with `mpmc_large` feature. - Fix missing `Drop` for `MpMcQueue` +### Removed + +- `Vec::storage_capacity` has been removed and `Vec::capacity` must be used instead. + ## [v0.8.0] - 2023-11-07 ### Added