Skip to content

Commit 1185776

Browse files
committed
Replace debug assertions with assertion macrothe and express constraint in the documentation properly
1 parent 6409afb commit 1185776

4 files changed

Lines changed: 11 additions & 5 deletions

File tree

src/array_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<const CAP: usize> ArrayString<CAP>
9191
/// i.e. that `len <= CAP` and that the first `len` bytes of `xs` form a valid UTF-8 string.
9292
pub const unsafe fn from_raw_parts(xs: [MaybeUninit<u8>; CAP], len: usize) -> Self {
9393
assert_capacity_limit_const!(CAP);
94-
debug_assert!(len <= CAP);
94+
assert_capacity_len_const!(CAP, len);
9595
ArrayString { xs, len: len as _ }
9696
}
9797

src/arrayvec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
105105
///
106106
/// # Safety
107107
/// The caller must ensure that the first `len` elements of `xs` are
108-
/// properly initialized.
108+
/// properly initialized and that len is less than or equal to `CAP`.
109109
pub const unsafe fn from_raw_parts(xs: [MaybeUninit<T>; CAP], len: usize) -> Self {
110110
assert_capacity_limit_const!(CAP);
111-
debug_assert!(len <= CAP);
111+
assert_capacity_len_const!(CAP, len);
112112
ArrayVec { xs, len: len as LenUint }
113113
}
114114

src/const_helper.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use core::mem::MaybeUninit;
22

33
use crate::ArrayString;
44

5-
6-
75
/// Creates a const ArrayString from a str slice.
86
pub const fn str<const CAP: usize>(s: &str) -> ArrayString<CAP> {
97
assert_capacity_limit_const!(CAP);

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ macro_rules! assert_capacity_limit_const {
5757
}
5858
}
5959

60+
macro_rules! assert_capacity_len_const {
61+
($cap:expr, $len:expr) => {
62+
if $len > $cap {
63+
[/*ArrayVec/ArrayString: len over capacity*/][$len]
64+
}
65+
};
66+
}
67+
6068
mod arrayvec_impl;
6169
mod arrayvec;
6270
mod array_string;

0 commit comments

Comments
 (0)