Skip to content

Commit 366c5da

Browse files
authored
Merge pull request #181 from rodrimati1992/master
Made ArrayVec::new and ArrayString::new const fns
2 parents 9ac0b20 + 5303f69 commit 366c5da

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ jobs:
2929
- rust: nightly
3030
features: serde
3131
experimental: false
32-
- rust: nightly
33-
features: serde unstable-const-fn
34-
experimental: true
3532

3633
steps:
3734
- uses: actions/checkout@v2

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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
//! - Enable serialization for ArrayVec and ArrayString using serde 1.x
1313
//!
1414
//! - `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.
15+
//! - **deprecated** (has no effect)
16+
//! - Not needed, [`ArrayVec::new`] and [`ArrayString::new`] are always `const fn` now
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+

tests/tests.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,12 +718,37 @@ fn allow_max_capacity_arrayvec_type() {
718718
let _v: ArrayVec<(), {usize::MAX}>;
719719
}
720720

721-
#[should_panic(expected="ArrayVec: largest supported")]
721+
#[should_panic(expected="index out of bounds")]
722722
#[test]
723723
fn deny_max_capacity_arrayvec_value() {
724724
if mem::size_of::<usize>() <= mem::size_of::<u32>() {
725-
panic!("This test does not work on this platform. 'ArrayVec: largest supported'");
725+
panic!("This test does not work on this platform. 'index out of bounds'");
726726
}
727727
// this type is allowed to be used (but can't be constructed)
728728
let _v: ArrayVec<(), {usize::MAX}> = ArrayVec::new();
729729
}
730+
731+
#[test]
732+
fn test_arrayvec_const_constructible() {
733+
const OF_U8: ArrayVec<Vec<u8>, 10> = ArrayVec::new();
734+
735+
let mut var = OF_U8;
736+
assert!(var.is_empty());
737+
assert_eq!(var, ArrayVec::new());
738+
var.push(vec![3, 5, 8]);
739+
assert_eq!(var[..], [vec![3, 5, 8]]);
740+
}
741+
742+
743+
#[test]
744+
fn test_arraystring_const_constructible() {
745+
const AS: ArrayString<10> = ArrayString::new();
746+
747+
let mut var = AS;
748+
assert!(var.is_empty());
749+
assert_eq!(var, ArrayString::new());
750+
var.push_str("hello");
751+
assert_eq!(var, *"hello");
752+
}
753+
754+

0 commit comments

Comments
 (0)