Merge pull request #194 from bhgomes/const-fns
Upgrade length/capacity-related functions to const-fn
This commit is contained in:
+17
-4
@@ -82,11 +82,11 @@ impl<const CAP: usize> ArrayString<CAP>
|
|||||||
|
|
||||||
/// Return the length of the string.
|
/// Return the length of the string.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn len(&self) -> usize { self.len as usize }
|
pub const fn len(&self) -> usize { self.len as usize }
|
||||||
|
|
||||||
/// Returns whether the string is empty.
|
/// Returns whether the string is empty.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
pub const fn is_empty(&self) -> bool { self.len() == 0 }
|
||||||
|
|
||||||
/// Create a new `ArrayString` from a `str`.
|
/// Create a new `ArrayString` from a `str`.
|
||||||
///
|
///
|
||||||
@@ -160,7 +160,7 @@ impl<const CAP: usize> ArrayString<CAP>
|
|||||||
/// assert_eq!(string.capacity(), 3);
|
/// assert_eq!(string.capacity(), 3);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn capacity(&self) -> usize { CAP }
|
pub const fn capacity(&self) -> usize { CAP }
|
||||||
|
|
||||||
/// Return if the `ArrayString` is completely filled.
|
/// Return if the `ArrayString` is completely filled.
|
||||||
///
|
///
|
||||||
@@ -172,7 +172,20 @@ impl<const CAP: usize> ArrayString<CAP>
|
|||||||
/// string.push_str("A");
|
/// string.push_str("A");
|
||||||
/// assert!(string.is_full());
|
/// assert!(string.is_full());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn is_full(&self) -> bool { self.len() == self.capacity() }
|
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
|
||||||
|
|
||||||
|
/// Returns the capacity left in the `ArrayString`.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use arrayvec::ArrayString;
|
||||||
|
///
|
||||||
|
/// let mut string = ArrayString::<3>::from("abc").unwrap();
|
||||||
|
/// string.pop();
|
||||||
|
/// assert_eq!(string.remaining_capacity(), 1);
|
||||||
|
/// ```
|
||||||
|
pub const fn remaining_capacity(&self) -> usize {
|
||||||
|
self.capacity() - self.len()
|
||||||
|
}
|
||||||
|
|
||||||
/// Adds the given char to the end of the string.
|
/// Adds the given char to the end of the string.
|
||||||
///
|
///
|
||||||
|
|||||||
+5
-5
@@ -108,7 +108,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
|
|||||||
/// assert_eq!(array.len(), 2);
|
/// assert_eq!(array.len(), 2);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn len(&self) -> usize { self.len as usize }
|
pub const fn len(&self) -> usize { self.len as usize }
|
||||||
|
|
||||||
/// Returns whether the `ArrayVec` is empty.
|
/// Returns whether the `ArrayVec` is empty.
|
||||||
///
|
///
|
||||||
@@ -120,7 +120,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
|
|||||||
/// assert_eq!(array.is_empty(), true);
|
/// assert_eq!(array.is_empty(), true);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
pub const fn is_empty(&self) -> bool { self.len() == 0 }
|
||||||
|
|
||||||
/// Return the capacity of the `ArrayVec`.
|
/// Return the capacity of the `ArrayVec`.
|
||||||
///
|
///
|
||||||
@@ -131,7 +131,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
|
|||||||
/// assert_eq!(array.capacity(), 3);
|
/// assert_eq!(array.capacity(), 3);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn capacity(&self) -> usize { CAP }
|
pub const fn capacity(&self) -> usize { CAP }
|
||||||
|
|
||||||
/// Return true if the `ArrayVec` is completely filled to its capacity, false otherwise.
|
/// Return true if the `ArrayVec` is completely filled to its capacity, false otherwise.
|
||||||
///
|
///
|
||||||
@@ -143,7 +143,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
|
|||||||
/// array.push(1);
|
/// array.push(1);
|
||||||
/// assert!(array.is_full());
|
/// assert!(array.is_full());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn is_full(&self) -> bool { self.len() == self.capacity() }
|
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
|
||||||
|
|
||||||
/// Returns the capacity left in the `ArrayVec`.
|
/// Returns the capacity left in the `ArrayVec`.
|
||||||
///
|
///
|
||||||
@@ -154,7 +154,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
|
|||||||
/// array.pop();
|
/// array.pop();
|
||||||
/// assert_eq!(array.remaining_capacity(), 1);
|
/// assert_eq!(array.remaining_capacity(), 1);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn remaining_capacity(&self) -> usize {
|
pub const fn remaining_capacity(&self) -> usize {
|
||||||
self.capacity() - self.len()
|
self.capacity() - self.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ pub struct CapacityError<T = ()> {
|
|||||||
|
|
||||||
impl<T> CapacityError<T> {
|
impl<T> CapacityError<T> {
|
||||||
/// Create a new `CapacityError` from `element`.
|
/// Create a new `CapacityError` from `element`.
|
||||||
pub fn new(element: T) -> CapacityError<T> {
|
pub const fn new(element: T) -> CapacityError<T> {
|
||||||
CapacityError {
|
CapacityError {
|
||||||
element: element,
|
element: element,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user