Skip to content

Commit 1f75553

Browse files
committed
FIX: Add new_const() for const construction and revert new to old version
The new() function is significantly faster, it optimizes better at the moment/in current rust. For this reason, provide a const fn constructor, but not as the default `new`.
1 parent 198a403 commit 1f75553

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/array_string.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,26 @@ impl<const CAP: usize> ArrayString<CAP>
5959
/// assert_eq!(&string[..], "foo");
6060
/// assert_eq!(string.capacity(), 16);
6161
/// ```
62-
pub const fn new() -> ArrayString<CAP> {
62+
pub fn new() -> ArrayString<CAP> {
63+
assert_capacity_limit!(CAP);
64+
unsafe {
65+
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
66+
}
67+
}
68+
69+
/// Create a new empty `ArrayString` (const fn).
70+
///
71+
/// Capacity is inferred from the type parameter.
72+
///
73+
/// ```
74+
/// use arrayvec::ArrayString;
75+
///
76+
/// let mut string = ArrayString::<16>::new();
77+
/// string.push_str("foo");
78+
/// assert_eq!(&string[..], "foo");
79+
/// assert_eq!(string.capacity(), 16);
80+
/// ```
81+
pub const fn new_const() -> ArrayString<CAP> {
6382
assert_capacity_limit!(CAP);
6483
ArrayString { xs: MakeMaybeUninit::ARRAY, len: 0 }
6584
}

src/arrayvec.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,27 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
7777
/// assert_eq!(&array[..], &[1, 2]);
7878
/// assert_eq!(array.capacity(), 16);
7979
/// ```
80-
pub const fn new() -> ArrayVec<T, CAP> {
80+
pub fn new() -> ArrayVec<T, CAP> {
81+
assert_capacity_limit!(CAP);
82+
unsafe {
83+
ArrayVec { xs: MaybeUninit::uninit().assume_init(), len: 0 }
84+
}
85+
}
86+
87+
/// Create a new empty `ArrayVec` (const fn).
88+
///
89+
/// The maximum capacity is given by the generic parameter `CAP`.
90+
///
91+
/// ```
92+
/// use arrayvec::ArrayVec;
93+
///
94+
/// let mut array = ArrayVec::<_, 16>::new();
95+
/// array.push(1);
96+
/// array.push(2);
97+
/// assert_eq!(&array[..], &[1, 2]);
98+
/// assert_eq!(array.capacity(), 16);
99+
/// ```
100+
pub const fn new_const() -> ArrayVec<T, CAP> {
81101
assert_capacity_limit!(CAP);
82102
ArrayVec { xs: MakeMaybeUninit::ARRAY, len: 0 }
83103
}

tests/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ fn deny_max_capacity_arrayvec_value() {
730730

731731
#[test]
732732
fn test_arrayvec_const_constructible() {
733-
const OF_U8: ArrayVec<Vec<u8>, 10> = ArrayVec::new();
733+
const OF_U8: ArrayVec<Vec<u8>, 10> = ArrayVec::new_const();
734734

735735
let mut var = OF_U8;
736736
assert!(var.is_empty());
@@ -742,7 +742,7 @@ fn test_arrayvec_const_constructible() {
742742

743743
#[test]
744744
fn test_arraystring_const_constructible() {
745-
const AS: ArrayString<10> = ArrayString::new();
745+
const AS: ArrayString<10> = ArrayString::new_const();
746746

747747
let mut var = AS;
748748
assert!(var.is_empty());

0 commit comments

Comments
 (0)