Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made ArrayVec::new and ArrayString::new const fns #181

Merged
merged 4 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ jobs:
- rust: nightly
features: serde
experimental: false
- rust: nightly
features: serde unstable-const-fn
experimental: true

steps:
- uses: actions/checkout@v2
Expand Down
14 changes: 2 additions & 12 deletions src/array_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::str::Utf8Error;
use crate::CapacityError;
use crate::LenUint;
use crate::char::encode_utf8;
use crate::utils::MakeMaybeUninit;

#[cfg(feature="serde")]
use serde::{Serialize, Deserialize, Serializer, Deserializer};
Expand Down Expand Up @@ -58,20 +59,9 @@ impl<const CAP: usize> ArrayString<CAP>
/// assert_eq!(&string[..], "foo");
/// assert_eq!(string.capacity(), 16);
/// ```
#[cfg(not(feature="unstable-const-fn"))]
pub fn new() -> ArrayString<CAP> {
assert_capacity_limit!(CAP);
unsafe {
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
}
}

#[cfg(feature="unstable-const-fn")]
pub const fn new() -> ArrayString<CAP> {
assert_capacity_limit!(CAP);
unsafe {
ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
}
ArrayString { xs: MakeMaybeUninit::ARRAY, len: 0 }
}

/// Return the length of the string.
Expand Down
14 changes: 2 additions & 12 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
use crate::LenUint;
use crate::errors::CapacityError;
use crate::arrayvec_impl::ArrayVecImpl;
use crate::utils::MakeMaybeUninit;

/// A vector with a fixed capacity.
///
Expand Down Expand Up @@ -76,20 +77,9 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
/// assert_eq!(&array[..], &[1, 2]);
/// assert_eq!(array.capacity(), 16);
/// ```
#[cfg(not(feature="unstable-const-fn"))]
pub fn new() -> ArrayVec<T, CAP> {
assert_capacity_limit!(CAP);
unsafe {
ArrayVec { xs: MaybeUninit::uninit().assume_init(), len: 0 }
}
}

#[cfg(feature="unstable-const-fn")]
pub const fn new() -> ArrayVec<T, CAP> {
assert_capacity_limit!(CAP);
unsafe {
ArrayVec { xs: MaybeUninit::uninit().assume_init(), len: 0 }
}
ArrayVec { xs: MakeMaybeUninit::ARRAY, len: 0 }
}

/// Return the number of elements in the `ArrayVec`.
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
//! - Enable serialization for ArrayVec and ArrayString using serde 1.x
//!
//! - `unstable-const-fn`
//! - Optional
//! - Makes [`ArrayVec::new`] and [`ArrayString::new`] `const fn`s,
//! using the nightly `const_fn` feature.
//! - Unstable and requires nightly.
//! - **deprecated** (has no effect)
//! - Not needed, [`ArrayVec::new`] and [`ArrayString::new`] are always `const fn` now
//!
//! ## Rust Version
//!
//! This version of arrayvec requires Rust 1.51 or later.
//!
#![doc(html_root_url="https://docs.rs/arrayvec/0.6/")]
#![cfg_attr(not(feature="std"), no_std)]
#![cfg_attr(feature="unstable-const-fn", feature(const_fn, const_maybe_uninit_assume_init, const_panic))]

#[cfg(feature="serde")]
extern crate serde;
Expand All @@ -37,7 +34,7 @@ macro_rules! assert_capacity_limit {
($cap:expr) => {
if std::mem::size_of::<usize>() > std::mem::size_of::<LenUint>() {
if $cap > LenUint::MAX as usize {
panic!("ArrayVec: largest supported capacity is u32::MAX")
[/*ArrayVec: largest supported capacity is u32::MAX*/][$cap]
}
}
}
Expand All @@ -48,6 +45,7 @@ mod arrayvec;
mod array_string;
mod char;
mod errors;
mod utils;

pub use crate::array_string::ArrayString;
pub use crate::errors::CapacityError;
Expand Down
11 changes: 11 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::marker::PhantomData;
use std::mem::MaybeUninit;

pub(crate) struct MakeMaybeUninit<T, const N: usize>(PhantomData<fn() -> T>);

impl<T, const N: usize> MakeMaybeUninit<T, N> {
pub(crate) const VALUE: MaybeUninit<T> = MaybeUninit::uninit();

pub(crate) const ARRAY: [MaybeUninit<T>; N] = [Self::VALUE; N];
}

29 changes: 27 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,12 +718,37 @@ fn allow_max_capacity_arrayvec_type() {
let _v: ArrayVec<(), {usize::MAX}>;
}

#[should_panic(expected="ArrayVec: largest supported")]
#[should_panic(expected="index out of bounds")]
#[test]
fn deny_max_capacity_arrayvec_value() {
if mem::size_of::<usize>() <= mem::size_of::<u32>() {
panic!("This test does not work on this platform. 'ArrayVec: largest supported'");
panic!("This test does not work on this platform. 'index out of bounds'");
}
// this type is allowed to be used (but can't be constructed)
let _v: ArrayVec<(), {usize::MAX}> = ArrayVec::new();
}

#[test]
fn test_arrayvec_const_constructible() {
const OF_U8: ArrayVec<Vec<u8>, 10> = ArrayVec::new();

let mut var = OF_U8;
assert!(var.is_empty());
assert_eq!(var, ArrayVec::new());
var.push(vec![3, 5, 8]);
assert_eq!(var[..], [vec![3, 5, 8]]);
}


#[test]
fn test_arraystring_const_constructible() {
const AS: ArrayString<10> = ArrayString::new();

let mut var = AS;
assert!(var.is_empty());
assert_eq!(var, ArrayString::new());
var.push_str("hello");
assert_eq!(var, *"hello");
}