Skip to content

Commit 17c5dd0

Browse files
committed
upgrade len/capacity-related functions to const-fn
1 parent 481f930 commit 17c5dd0

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/array_string.rs

Lines changed: 4 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
///
@@ -138,7 +138,7 @@ impl<const CAP: usize> ArrayString<CAP>
138138
/// assert_eq!(string.capacity(), 3);
139139
/// ```
140140
#[inline(always)]
141-
pub fn capacity(&self) -> usize { CAP }
141+
pub const fn capacity(&self) -> usize { CAP }
142142

143143
/// Return if the `ArrayString` is completely filled.
144144
///
@@ -150,7 +150,7 @@ impl<const CAP: usize> ArrayString<CAP>
150150
/// string.push_str("A");
151151
/// assert!(string.is_full());
152152
/// ```
153-
pub fn is_full(&self) -> bool { self.len() == self.capacity() }
153+
pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
154154

155155
/// Returns the capacity left in the `ArrayString`.
156156
///

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)