Skip to content

Commit 5ad4687

Browse files
committed
Made ArrayVec::new and ArrayString::new const fns
Added utils module with a `MaybeUninit` helper type to construct `[MaybeUninit<T>; N]` Removed all uses of the "unstable-const-fn" feature, and documented it as being deprecated. Changed `assert_capacity_limit` macro to work in const contexts.
1 parent 9ac0b20 commit 5ad4687

File tree

4 files changed

+20
-31
lines changed

4 files changed

+20
-31
lines changed

src/array_string.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::str::Utf8Error;
1414
use crate::CapacityError;
1515
use crate::LenUint;
1616
use crate::char::encode_utf8;
17+
use crate::utils::MakeMaybeUninit;
1718

1819
#[cfg(feature="serde")]
1920
use serde::{Serialize, Deserialize, Serializer, Deserializer};
@@ -58,20 +59,9 @@ impl<const CAP: usize> ArrayString<CAP>
5859
/// assert_eq!(&string[..], "foo");
5960
/// assert_eq!(string.capacity(), 16);
6061
/// ```
61-
#[cfg(not(feature="unstable-const-fn"))]
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-
#[cfg(feature="unstable-const-fn")]
7062
pub const fn new() -> ArrayString<CAP> {
7163
assert_capacity_limit!(CAP);
72-
unsafe {
73-
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
74-
}
64+
ArrayString { xs: MakeMaybeUninit::ARRAY, len: 0 }
7565
}
7666

7767
/// Return the length of the string.

src/arrayvec.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
2323
use crate::LenUint;
2424
use crate::errors::CapacityError;
2525
use crate::arrayvec_impl::ArrayVecImpl;
26+
use crate::utils::MakeMaybeUninit;
2627

2728
/// A vector with a fixed capacity.
2829
///
@@ -76,20 +77,9 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
7677
/// assert_eq!(&array[..], &[1, 2]);
7778
/// assert_eq!(array.capacity(), 16);
7879
/// ```
79-
#[cfg(not(feature="unstable-const-fn"))]
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-
#[cfg(feature="unstable-const-fn")]
8880
pub const fn new() -> ArrayVec<T, CAP> {
8981
assert_capacity_limit!(CAP);
90-
unsafe {
91-
ArrayVec { xs: MaybeUninit::uninit().assume_init(), len: 0 }
92-
}
82+
ArrayVec { xs: MakeMaybeUninit::ARRAY, len: 0 }
9383
}
9484

9585
/// Return the number of elements in the `ArrayVec`.

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@
1111
//! - Optional
1212
//! - Enable serialization for ArrayVec and ArrayString using serde 1.x
1313
//!
14-
//! - `unstable-const-fn`
15-
//! - Optional
16-
//! - Makes [`ArrayVec::new`] and [`ArrayString::new`] `const fn`s,
17-
//! using the nightly `const_fn` feature.
18-
//! - Unstable and requires nightly.
14+
//! - `unstable-const-fn`: **deprecated**,
15+
//! used to be needed to make [`ArrayVec::new`] and [`ArrayString::new`] `const fn`s,
16+
//! now they are always `const fn`s.
1917
//!
2018
//! ## Rust Version
2119
//!
2220
//! This version of arrayvec requires Rust 1.51 or later.
2321
//!
2422
#![doc(html_root_url="https://docs.rs/arrayvec/0.6/")]
2523
#![cfg_attr(not(feature="std"), no_std)]
26-
#![cfg_attr(feature="unstable-const-fn", feature(const_fn, const_maybe_uninit_assume_init, const_panic))]
2724

2825
#[cfg(feature="serde")]
2926
extern crate serde;
@@ -37,7 +34,7 @@ macro_rules! assert_capacity_limit {
3734
($cap:expr) => {
3835
if std::mem::size_of::<usize>() > std::mem::size_of::<LenUint>() {
3936
if $cap > LenUint::MAX as usize {
40-
panic!("ArrayVec: largest supported capacity is u32::MAX")
37+
[/*ArrayVec: largest supported capacity is u32::MAX*/][$cap]
4138
}
4239
}
4340
}
@@ -48,6 +45,7 @@ mod arrayvec;
4845
mod array_string;
4946
mod char;
5047
mod errors;
48+
mod utils;
5149

5250
pub use crate::array_string::ArrayString;
5351
pub use crate::errors::CapacityError;

src/utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::marker::PhantomData;
2+
use std::mem::MaybeUninit;
3+
4+
pub(crate) struct MakeMaybeUninit<T, const N: usize>(PhantomData<fn() -> T>);
5+
6+
impl<T, const N: usize> MakeMaybeUninit<T, N> {
7+
pub(crate) const VALUE: MaybeUninit<T> = MaybeUninit::uninit();
8+
9+
pub(crate) const ARRAY: [MaybeUninit<T>; N] = [Self::VALUE; N];
10+
}
11+

0 commit comments

Comments
 (0)