Skip to content

Commit

Permalink
Keep Vec::capacity const
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sosthene-nitrokey committed Jul 2, 2024
1 parent a9ed238 commit 94b0ea5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/linear_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
/// assert_eq!(map.capacity(), 8);
/// ```
pub fn capacity(&self) -> usize {
self.buffer.borrow().capacity()
self.buffer.storage_capacity()
}

/// Clears the map, removing all key-value pairs.
Expand Down
2 changes: 1 addition & 1 deletion src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl<S: Storage> StringInner<S> {
/// ```
#[inline]
pub fn capacity(&self) -> usize {
self.vec.capacity()
self.vec.storage_capacity()
}

/// Appends the given [`char`] to the end of this `String`.
Expand Down
17 changes: 12 additions & 5 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ impl<T, const N: usize> Vec<T, N> {
{
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<T> VecView<T> {
Expand Down Expand Up @@ -407,7 +414,7 @@ impl<T, S: Storage> VecInner<T, S> {
}

/// 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()
}

Expand Down Expand Up @@ -486,7 +493,7 @@ impl<T, S: Storage> VecInner<T, S> {
///
/// 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 {
Expand Down Expand Up @@ -560,7 +567,7 @@ impl<T, S: Storage> VecInner<T, S> {
where
T: Clone,
{
if new_len > self.capacity() {
if new_len > self.storage_capacity() {
return Err(());
}

Expand Down Expand Up @@ -680,7 +687,7 @@ impl<T, S: Storage> VecInner<T, S> {
/// 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
}
Expand Down Expand Up @@ -756,7 +763,7 @@ impl<T, S: Storage> VecInner<T, S> {

/// 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
Expand Down

0 comments on commit 94b0ea5

Please sign in to comment.