Skip to content

Commit f94dca5

Browse files
committed
Add if len > CAP { unreachable_unchecked() }
1 parent 6d5e262 commit f94dca5

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/array_string.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ impl<const CAP: usize> ArrayString<CAP>
8585

8686
/// Return the length of the string.
8787
#[inline]
88-
pub const fn len(&self) -> usize { self.len as usize }
88+
pub const fn len(&self) -> usize {
89+
let len = self.len as usize;
90+
if len <= CAP {
91+
len
92+
} else {
93+
unsafe { std::hint::unreachable_unchecked() }
94+
}
95+
}
8996

9097
/// Returns whether the string is empty.
9198
#[inline]

src/arrayvec.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,14 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
111111
/// assert_eq!(array.len(), 2);
112112
/// ```
113113
#[inline(always)]
114-
pub const fn len(&self) -> usize { self.len as usize }
114+
pub const fn len(&self) -> usize {
115+
let len = self.len as usize;
116+
if len <= CAP {
117+
len
118+
} else {
119+
unsafe { std::hint::unreachable_unchecked() }
120+
}
121+
}
115122

116123
/// Returns whether the `ArrayVec` is empty.
117124
///

0 commit comments

Comments
 (0)