From 94b0ea53ded6ee3093be96a6b2e7562b9fd43218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 2 Jul 2024 14:50:48 +0200 Subject: [PATCH] Keep `Vec::capacity` const In 0.8.0 it was const, but this was removed in https://github.com/rust-embedded/heapless/pull/486 The other container types did not make the `capacity` method const, and therefore can kept with the normal name and the generic implementation. --- src/binary_heap.rs | 2 +- src/linear_map.rs | 2 +- src/string/mod.rs | 2 +- src/vec/mod.rs | 17 ++++++++++++----- 4 files changed, 15 insertions(+), 8 deletions(-) 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 48242f7416..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.borrow().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 cabc02e01f..33490b0145 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -458,7 +458,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 3fbfc65226..a65fa82d77 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -290,6 +290,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 { @@ -407,7 +414,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() } @@ -486,7 +493,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 { @@ -560,7 +567,7 @@ impl VecInner { where T: Clone, { - if new_len > self.capacity() { + if new_len > self.storage_capacity() { return Err(()); } @@ -680,7 +687,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 } @@ -756,7 +763,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