From 481f93084eecebda0445cb7f81b40b9789dd1559 Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Sun, 1 Aug 2021 13:53:21 -0400 Subject: [PATCH 1/2] add remaining_capacity to ArrayString --- src/array_string.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/array_string.rs b/src/array_string.rs index a044cb5..257c26a 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -152,6 +152,19 @@ impl ArrayString /// ``` pub 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. /// /// ***Panics*** if the backing array is not large enough to fit the additional char. From 17c5dd051e64aab1fd6037a2b35c319e9169c9e6 Mon Sep 17 00:00:00 2001 From: "Brandon H. Gomes" Date: Sun, 1 Aug 2021 13:54:17 -0400 Subject: [PATCH 2/2] upgrade len/capacity-related functions to const-fn --- src/array_string.rs | 8 ++++---- src/arrayvec.rs | 10 +++++----- src/errors.rs | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index 257c26a..7d13bd4 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -82,11 +82,11 @@ impl ArrayString /// 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`. /// @@ -138,7 +138,7 @@ impl ArrayString /// 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. /// @@ -150,7 +150,7 @@ impl ArrayString /// 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`. /// diff --git a/src/arrayvec.rs b/src/arrayvec.rs index 6b4faf8..acd509d 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -108,7 +108,7 @@ impl ArrayVec { /// 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. /// @@ -120,7 +120,7 @@ impl ArrayVec { /// 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`. /// @@ -131,7 +131,7 @@ impl ArrayVec { /// 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. /// @@ -143,7 +143,7 @@ impl ArrayVec { /// 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`. /// @@ -154,7 +154,7 @@ impl ArrayVec { /// 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() } diff --git a/src/errors.rs b/src/errors.rs index 380742a..7ca3ebc 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -12,7 +12,7 @@ pub struct CapacityError { impl CapacityError { /// Create a new `CapacityError` from `element`. - pub fn new(element: T) -> CapacityError { + pub const fn new(element: T) -> CapacityError { CapacityError { element: element, }