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

Upgrade length/capacity-related functions to const-fn #194

Merged
merged 2 commits into from Oct 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/array_string.rs
Expand Up @@ -82,11 +82,11 @@ impl<const CAP: usize> ArrayString<CAP>

/// Return the length of the string.
#[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.
#[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`.
///
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<const CAP: usize> ArrayString<CAP>
/// assert_eq!(string.capacity(), 3);
/// ```
#[inline(always)]
pub fn capacity(&self) -> usize { CAP }
pub const fn capacity(&self) -> usize { CAP }

/// Return if the `ArrayString` is completely filled.
///
Expand All @@ -150,7 +150,20 @@ impl<const CAP: usize> ArrayString<CAP>
/// string.push_str("A");
/// 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.
///
Expand Down
10 changes: 5 additions & 5 deletions src/arrayvec.rs
Expand Up @@ -108,7 +108,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.len(), 2);
/// ```
#[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.
///
Expand All @@ -120,7 +120,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.is_empty(), true);
/// ```
#[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`.
///
Expand All @@ -131,7 +131,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(array.capacity(), 3);
/// ```
#[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.
///
Expand All @@ -143,7 +143,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// array.push(1);
/// 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`.
///
Expand All @@ -154,7 +154,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// array.pop();
/// assert_eq!(array.remaining_capacity(), 1);
/// ```
pub fn remaining_capacity(&self) -> usize {
pub const fn remaining_capacity(&self) -> usize {
self.capacity() - self.len()
}

Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Expand Up @@ -12,7 +12,7 @@ pub struct CapacityError<T = ()> {

impl<T> CapacityError<T> {
/// Create a new `CapacityError` from `element`.
pub fn new(element: T) -> CapacityError<T> {
pub const fn new(element: T) -> CapacityError<T> {
CapacityError {
element: element,
}
Expand Down