Skip to content

Commit 27bff4a

Browse files
Keep Vec::capacity const
In 0.8.0 it was const, but this was removed in #486 The other container types did not make the `capacity` method const, and therefore can kept with the normal name and the generic implementation.
1 parent 68cc4d1 commit 27bff4a

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/binary_heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ where
206206
/* Public API */
207207
/// Returns the capacity of the binary heap.
208208
pub fn capacity(&self) -> usize {
209-
self.data.capacity()
209+
self.data.storage_capacity()
210210
}
211211

212212
/// Drops all items from the binary heap.

src/linear_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
/// assert_eq!(map.capacity(), 8);
7272
/// ```
7373
pub fn capacity(&self) -> usize {
74-
self.buffer.capacity()
74+
self.buffer.storage_capacity()
7575
}
7676

7777
/// Clears the map, removing all key-value pairs.

src/string/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<S: Storage> StringInner<S> {
459459
/// ```
460460
#[inline]
461461
pub fn capacity(&self) -> usize {
462-
self.vec.capacity()
462+
self.vec.storage_capacity()
463463
}
464464

465465
/// Appends the given [`char`] to the end of this `String`.

src/vec/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ impl<T, const N: usize> Vec<T, N> {
291291
{
292292
self.as_mut_view().drain(range)
293293
}
294+
295+
/// Returns the maximum number of elements the vector can hold.
296+
///
297+
/// This method is not available on a `VecView`, use [`storage_len`](VecInner::storage_capacity) instead
298+
pub const fn capacity(&self) -> usize {
299+
self.buffer.len()
300+
}
294301
}
295302

296303
impl<T> VecView<T> {
@@ -408,7 +415,7 @@ impl<T, S: Storage> VecInner<T, S> {
408415
}
409416

410417
/// Returns the maximum number of elements the vector can hold.
411-
pub fn capacity(&self) -> usize {
418+
pub fn storage_capacity(&self) -> usize {
412419
self.buffer.borrow().len()
413420
}
414421

@@ -487,7 +494,7 @@ impl<T, S: Storage> VecInner<T, S> {
487494
///
488495
/// Returns back the `item` if the vector is full.
489496
pub fn push(&mut self, item: T) -> Result<(), T> {
490-
if self.len < self.capacity() {
497+
if self.len < self.storage_capacity() {
491498
unsafe { self.push_unchecked(item) }
492499
Ok(())
493500
} else {
@@ -561,7 +568,7 @@ impl<T, S: Storage> VecInner<T, S> {
561568
where
562569
T: Clone,
563570
{
564-
if new_len > self.capacity() {
571+
if new_len > self.storage_capacity() {
565572
return Err(());
566573
}
567574

@@ -681,7 +688,7 @@ impl<T, S: Storage> VecInner<T, S> {
681688
/// Normally, here, one would use [`clear`] instead to correctly drop
682689
/// the contents and thus not leak memory.
683690
pub unsafe fn set_len(&mut self, new_len: usize) {
684-
debug_assert!(new_len <= self.capacity());
691+
debug_assert!(new_len <= self.storage_capacity());
685692

686693
self.len = new_len
687694
}
@@ -757,7 +764,7 @@ impl<T, S: Storage> VecInner<T, S> {
757764

758765
/// Returns true if the vec is full
759766
pub fn is_full(&self) -> bool {
760-
self.len == self.capacity()
767+
self.len == self.storage_capacity()
761768
}
762769

763770
/// Returns true if the vec is empty

0 commit comments

Comments
 (0)