diff --git a/Cargo.toml b/Cargo.toml index 13917b0..e3db147 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,8 @@ harness = false [features] default = ["std"] std = [] +len_u16 = [] +len_u8 = [] [profile.bench] debug = true diff --git a/src/array_string.rs b/src/array_string.rs index 227e01d..36d8b2d 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -27,8 +27,9 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer}; /// The `ArrayString` is a string backed by a fixed size array. It keeps track /// of its length, and is parameterized by `CAP` for the maximum capacity. /// -/// `CAP` is of type `usize` but is range limited to `u32::MAX` (or `u16` on 16-bit targets); -/// attempting to create larger arrayvecs with larger capacity will panic. +/// The length is stored in `u32` by default, and can be changed to `u16` or `u8` +/// by `len_u16` or `len_u8` feature. Attempting to create arrayvecs with larger +/// capacity will panic. /// /// The string is a contiguous value that you can store directly on the stack /// if needed. @@ -76,7 +77,7 @@ impl ArrayString /// ``` /// use arrayvec::ArrayString; /// - /// static ARRAY: ArrayString<1024> = ArrayString::new_const(); + /// static ARRAY: ArrayString<255> = ArrayString::new_const(); /// ``` pub const fn new_const() -> ArrayString { assert_capacity_limit_const!(CAP); diff --git a/src/arrayvec.rs b/src/arrayvec.rs index eec7732..05803db 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -31,8 +31,9 @@ use crate::utils::MakeMaybeUninit; /// the number of initialized elements. The `ArrayVec` is parameterized /// by `T` for the element type and `CAP` for the maximum capacity. /// -/// `CAP` is of type `usize` but is range limited to `u32::MAX` (or `u16::MAX` on 16-bit targets); -/// attempting to create larger arrayvecs with larger capacity will panic. +/// The length is stored in `u32` by default, and can be changed to `u16` or `u8` +/// by `len_u16` or `len_u8` feature. Attempting to create arrayvecs with larger +/// capacity will panic. /// /// The vector is a contiguous value (storing the elements inline) that you can store directly on /// the stack if needed. @@ -94,7 +95,7 @@ impl ArrayVec { /// ``` /// use arrayvec::ArrayVec; /// - /// static ARRAY: ArrayVec = ArrayVec::new_const(); + /// static ARRAY: ArrayVec = ArrayVec::new_const(); /// ``` pub const fn new_const() -> ArrayVec { assert_capacity_limit_const!(CAP); diff --git a/src/lib.rs b/src/lib.rs index 5c4bcee..83b9f3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,14 @@ //! - Optional, enabled by default //! - Use libstd; disable to use `no_std` instead. //! +//! - `len_u16` +//! - Optional. +//! - Use `u16` as length type. +//! +//! - `len_u8` +//! - Optional. +//! - Use `u8` as length type. +//! //! - `serde` //! - Optional //! - Enable serialization for ArrayVec and ArrayString using serde 1.x @@ -28,20 +36,20 @@ extern crate serde; #[cfg(not(feature="std"))] extern crate core as std; -#[cfg(not(target_pointer_width = "16"))] +#[cfg(all(not(feature="len_u8"), not(feature="len_u16")))] pub(crate) type LenUint = u32; -#[cfg(target_pointer_width = "16")] +#[cfg(feature="len_u16")] pub(crate) type LenUint = u16; +#[cfg(feature="len_u8")] +pub(crate) type LenUint = u8; + macro_rules! assert_capacity_limit { ($cap:expr) => { if std::mem::size_of::() > std::mem::size_of::() { if $cap > LenUint::MAX as usize { - #[cfg(not(target_pointer_width = "16"))] - panic!("ArrayVec: largest supported capacity is u32::MAX"); - #[cfg(target_pointer_width = "16")] - panic!("ArrayVec: largest supported capacity is u16::MAX"); + panic!("ArrayVec: largest supported capacity is {}", LenUint::MAX) } } } @@ -51,7 +59,7 @@ macro_rules! assert_capacity_limit_const { ($cap:expr) => { if std::mem::size_of::() > std::mem::size_of::() { if $cap > LenUint::MAX as usize { - [/*ArrayVec: largest supported capacity is u32::MAX*/][$cap] + [/*ArrayVec: largest supported capacity is LenUint::MAX*/][$cap] } } } diff --git a/tests/tests.rs b/tests/tests.rs index 098614b..11a49a5 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -75,6 +75,7 @@ fn test_try_from_slice_error() { } #[test] +#[cfg(feature="len_u16")] fn test_u16_index() { const N: usize = 4096; let mut vec: ArrayVec<_, N> = ArrayVec::new(); @@ -682,7 +683,7 @@ fn test_pop_at() { #[test] #[cfg(not(target_pointer_width = "16"))] fn test_sizes() { - let v = ArrayVec::from([0u8; 1 << 16]); + let v = ArrayVec::from([0u8; 255]); assert_eq!(vec![0u8; v.len()], &v[..]); }