diff --git a/src/binary_heap.rs b/src/binary_heap.rs index e137bdd67d..7e676a3851 100644 --- a/src/binary_heap.rs +++ b/src/binary_heap.rs @@ -206,7 +206,7 @@ where /* Public API */ /// Returns the capacity of the binary heap. pub fn capacity(&self) -> usize { - self.data.capacity() + self.data.storage_capacity() } /// Drops all items from the binary heap. diff --git a/src/linear_map.rs b/src/linear_map.rs index a7893d9046..28f4e83d42 100644 --- a/src/linear_map.rs +++ b/src/linear_map.rs @@ -71,7 +71,7 @@ where /// assert_eq!(map.capacity(), 8); /// ``` pub fn capacity(&self) -> usize { - self.buffer.capacity() + self.buffer.storage_capacity() } /// Clears the map, removing all key-value pairs. diff --git a/src/string/mod.rs b/src/string/mod.rs index 765454123e..d88dbf0fd0 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -459,7 +459,7 @@ impl StringInner { /// ``` #[inline] pub fn capacity(&self) -> usize { - self.vec.capacity() + self.vec.storage_capacity() } /// Appends the given [`char`] to the end of this `String`. diff --git a/src/vec/mod.rs b/src/vec/mod.rs index eafb46b007..1e1583a43e 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -291,6 +291,13 @@ 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.len() + } } impl VecView { @@ -408,7 +415,7 @@ impl VecInner { } /// Returns the maximum number of elements the vector can hold. - pub fn capacity(&self) -> usize { + pub fn storage_capacity(&self) -> usize { self.buffer.borrow().len() } @@ -487,7 +494,7 @@ impl VecInner { /// /// Returns back the `item` if the vector is full. pub fn push(&mut self, item: T) -> Result<(), T> { - if self.len < self.capacity() { + if self.len < self.storage_capacity() { unsafe { self.push_unchecked(item) } Ok(()) } else { @@ -561,7 +568,7 @@ impl VecInner { where T: Clone, { - if new_len > self.capacity() { + if new_len > self.storage_capacity() { return Err(()); } @@ -681,7 +688,7 @@ impl 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.capacity()); + debug_assert!(new_len <= self.storage_capacity()); self.len = new_len } @@ -757,7 +764,7 @@ impl VecInner { /// Returns true if the vec is full pub fn is_full(&self) -> bool { - self.len == self.capacity() + self.len == self.storage_capacity() } /// Returns true if the vec is empty