From ffd0526e67d6f047af3bc458fd48c69978255c1b Mon Sep 17 00:00:00 2001 From: wub Date: Fri, 29 Sep 2023 09:09:07 +0800 Subject: [PATCH] Add len_u16 and len_u8 features. --- Cargo.toml | 2 ++ src/array_string.rs | 7 ++++--- src/arrayvec.rs | 7 ++++--- src/lib.rs | 19 +++++++++++++++++-- tests/tests.rs | 5 +++-- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f4fd2c..972b611 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,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 90cfc09..fbe0fed 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -25,8 +25,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`; 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. @@ -73,7 +74,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 37e151a..6ce07b0 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`; 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. @@ -93,7 +94,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 f9a2fe6..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,13 +36,20 @@ extern crate serde; #[cfg(not(feature="std"))] extern crate core as std; +#[cfg(all(not(feature="len_u8"), not(feature="len_u16")))] pub(crate) type LenUint = u32; +#[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 { - panic!("ArrayVec: largest supported capacity is u32::MAX") + panic!("ArrayVec: largest supported capacity is {}", LenUint::MAX) } } } @@ -44,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 2f8a5ef..c3f71c9 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -75,6 +75,7 @@ fn test_try_from_slice_error() { } #[test] +#[cfg(feature="array-len-u16")] fn test_u16_index() { const N: usize = 4096; let mut vec: ArrayVec<_, N> = ArrayVec::new(); @@ -681,7 +682,7 @@ fn test_pop_at() { #[test] fn test_sizes() { - let v = ArrayVec::from([0u8; 1 << 16]); + let v = ArrayVec::from([0u8; 255]); assert_eq!(vec![0u8; v.len()], &v[..]); } @@ -790,4 +791,4 @@ fn test_arraystring_zero_filled_has_some_sanity_checks() { let string = ArrayString::<4>::zero_filled(); assert_eq!(string.as_str(), "\0\0\0\0"); assert_eq!(string.len(), 4); -} \ No newline at end of file +}