mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-29 21:40:27 +00:00
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.
This commit is contained in:
parent
68cc4d11a0
commit
27bff4a055
@ -206,7 +206,7 @@ where
|
|||||||
/* Public API */
|
/* Public API */
|
||||||
/// Returns the capacity of the binary heap.
|
/// Returns the capacity of the binary heap.
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.data.capacity()
|
self.data.storage_capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drops all items from the binary heap.
|
/// Drops all items from the binary heap.
|
||||||
|
@ -71,7 +71,7 @@ where
|
|||||||
/// assert_eq!(map.capacity(), 8);
|
/// assert_eq!(map.capacity(), 8);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.buffer.capacity()
|
self.buffer.storage_capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears the map, removing all key-value pairs.
|
/// Clears the map, removing all key-value pairs.
|
||||||
|
@ -459,7 +459,7 @@ impl<S: Storage> StringInner<S> {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.vec.capacity()
|
self.vec.storage_capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Appends the given [`char`] to the end of this `String`.
|
/// Appends the given [`char`] to the end of this `String`.
|
||||||
|
@ -291,6 +291,13 @@ impl<T, const N: usize> Vec<T, N> {
|
|||||||
{
|
{
|
||||||
self.as_mut_view().drain(range)
|
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> {
|
impl<T> VecView<T> {
|
||||||
@ -408,7 +415,7 @@ impl<T, S: Storage> VecInner<T, S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the maximum number of elements the vector can hold.
|
/// 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()
|
self.buffer.borrow().len()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -487,7 +494,7 @@ impl<T, S: Storage> VecInner<T, S> {
|
|||||||
///
|
///
|
||||||
/// Returns back the `item` if the vector is full.
|
/// Returns back the `item` if the vector is full.
|
||||||
pub fn push(&mut self, item: T) -> Result<(), T> {
|
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) }
|
unsafe { self.push_unchecked(item) }
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@ -561,7 +568,7 @@ impl<T, S: Storage> VecInner<T, S> {
|
|||||||
where
|
where
|
||||||
T: Clone,
|
T: Clone,
|
||||||
{
|
{
|
||||||
if new_len > self.capacity() {
|
if new_len > self.storage_capacity() {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -681,7 +688,7 @@ impl<T, S: Storage> VecInner<T, S> {
|
|||||||
/// Normally, here, one would use [`clear`] instead to correctly drop
|
/// Normally, here, one would use [`clear`] instead to correctly drop
|
||||||
/// the contents and thus not leak memory.
|
/// the contents and thus not leak memory.
|
||||||
pub unsafe fn set_len(&mut self, new_len: usize) {
|
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
|
self.len = new_len
|
||||||
}
|
}
|
||||||
@ -757,7 +764,7 @@ impl<T, S: Storage> VecInner<T, S> {
|
|||||||
|
|
||||||
/// Returns true if the vec is full
|
/// Returns true if the vec is full
|
||||||
pub fn is_full(&self) -> bool {
|
pub fn is_full(&self) -> bool {
|
||||||
self.len == self.capacity()
|
self.len == self.storage_capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the vec is empty
|
/// Returns true if the vec is empty
|
||||||
|
Loading…
x
Reference in New Issue
Block a user