Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep Vec::capacity const #498

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@ -459,7 +459,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 @@ -291,6 +291,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 @@ -408,7 +415,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sosthene-nitrokey, why can't this use the same name?

Copy link
Member

@Dirbaio Dirbaio Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storage_capacity applies to VecInner<T, S>, for any storage
capacity applies to VecInner<T, OwnedStorage<N>> aka Vec<T, N> only.

so for Vec<T, N> both apply, so it'd give ambiguity errors if the user wrote vec.capacity()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so for Vec<T, N> both apply, so it'd give ambiguity errors if the user wrote vec.capacity()

It wouldn't even allow getting to that point. The compiler would complain about duplicate definitions with the same name.

We could implement fn capacity on Vec and VecView independently, but then it would make it unusable in a generic context over the storage.

self.buffer.borrow().len()
}

Expand Down Expand Up @@ -487,7 +494,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 @@ -561,7 +568,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 @@ -681,7 +688,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 @@ -757,7 +764,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
Loading