Skip to content

Commit a4690c9

Browse files
authored
Merge pull request bluss#194 from bhgomes/const-fns
Upgrade length/capacity-related functions to const-fn
2 parents 4c8149a + 17c5dd0 commit a4690c9

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/array_string.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ impl<const CAP: usize> ArrayString<CAP>
8282

8383
/// Return the length of the string.
8484
#[inline]
85-
pub fn len(&self) -> usize { self.len as usize }
85+
pub const fn len(&self) -> usize { self.len as usize }
8686

8787
/// Returns whether the string is empty.
8888
#[inline]
89-
pub fn is_empty(&self) -> bool { self.len() == 0 }
89+
pub const fn is_empty(&self) -> bool { self.len() == 0 }
9090

9191
/// Create a new `ArrayString` from a `str`.
9292
///
@@ -160,7 +160,7 @@ impl<const CAP: usize> ArrayString<CAP>
160160
/// assert_eq!(string.capacity(), 3);
161161
/// ```
162162
#[inline(always)]
163-
pub fn capacity(&self) -> usize { CAP }
163+
pub const fn capacity(&self) -> usize { CAP }
164164

165165
/// Return if the `ArrayString` is completely filled.
166166
///
@@ -172,7 +172,20 @@ impl<const CAP: usize> ArrayString<CAP>
172172
/// string.push_str("A");
173173
/// assert!(string.is_full());
174174
/// ```
175-
pub fn is_full(&self) -> bool { self.len() == self.capacity() }
175+
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
176+
177+
/// Returns the capacity left in the `ArrayString`.
178+
///
179+
/// ```
180+
/// use arrayvec::ArrayString;
181+
///
182+
/// let mut string = ArrayString::<3>::from("abc").unwrap();
183+
/// string.pop();
184+
/// assert_eq!(string.remaining_capacity(), 1);
185+
/// ```
186+
pub const fn remaining_capacity(&self) -> usize {
187+
self.capacity() - self.len()
188+
}
176189

177190
/// Adds the given char to the end of the string.
178191
///

src/arrayvec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
108108
/// assert_eq!(array.len(), 2);
109109
/// ```
110110
#[inline(always)]
111-
pub fn len(&self) -> usize { self.len as usize }
111+
pub const fn len(&self) -> usize { self.len as usize }
112112

113113
/// Returns whether the `ArrayVec` is empty.
114114
///
@@ -120,7 +120,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
120120
/// assert_eq!(array.is_empty(), true);
121121
/// ```
122122
#[inline]
123-
pub fn is_empty(&self) -> bool { self.len() == 0 }
123+
pub const fn is_empty(&self) -> bool { self.len() == 0 }
124124

125125
/// Return the capacity of the `ArrayVec`.
126126
///
@@ -131,7 +131,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
131131
/// assert_eq!(array.capacity(), 3);
132132
/// ```
133133
#[inline(always)]
134-
pub fn capacity(&self) -> usize { CAP }
134+
pub const fn capacity(&self) -> usize { CAP }
135135

136136
/// Return true if the `ArrayVec` is completely filled to its capacity, false otherwise.
137137
///
@@ -143,7 +143,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
143143
/// array.push(1);
144144
/// assert!(array.is_full());
145145
/// ```
146-
pub fn is_full(&self) -> bool { self.len() == self.capacity() }
146+
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
147147

148148
/// Returns the capacity left in the `ArrayVec`.
149149
///
@@ -154,7 +154,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
154154
/// array.pop();
155155
/// assert_eq!(array.remaining_capacity(), 1);
156156
/// ```
157-
pub fn remaining_capacity(&self) -> usize {
157+
pub const fn remaining_capacity(&self) -> usize {
158158
self.capacity() - self.len()
159159
}
160160

src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct CapacityError<T = ()> {
1212

1313
impl<T> CapacityError<T> {
1414
/// Create a new `CapacityError` from `element`.
15-
pub fn new(element: T) -> CapacityError<T> {
15+
pub const fn new(element: T) -> CapacityError<T> {
1616
CapacityError {
1717
element: element,
1818
}

0 commit comments

Comments
 (0)