Skip to content

Commit 812c83a

Browse files
kornelskitbu-
authored andcommitted
Use 16-bit lengths on 16-bit targets
1 parent 4ef0e89 commit 812c83a

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/array_string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
2727
/// The `ArrayString` is a string backed by a fixed size array. It keeps track
2828
/// of its length, and is parameterized by `CAP` for the maximum capacity.
2929
///
30-
/// `CAP` is of type `usize` but is range limited to `u32::MAX`; attempting to create larger
31-
/// arrayvecs with larger capacity will panic.
30+
/// `CAP` is of type `usize` but is range limited to `u32::MAX` (or `u16` on 16-bit targets);
31+
/// attempting to create larger arrayvecs with larger capacity will panic.
3232
///
3333
/// The string is a contiguous value that you can store directly on the stack
3434
/// if needed.

src/arrayvec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use crate::utils::MakeMaybeUninit;
3131
/// the number of initialized elements. The `ArrayVec<T, CAP>` is parameterized
3232
/// by `T` for the element type and `CAP` for the maximum capacity.
3333
///
34-
/// `CAP` is of type `usize` but is range limited to `u32::MAX`; attempting to create larger
35-
/// arrayvecs with larger capacity will panic.
34+
/// `CAP` is of type `usize` but is range limited to `u32::MAX` (or `u16::MAX` on 16-bit targets);
35+
/// attempting to create larger arrayvecs with larger capacity will panic.
3636
///
3737
/// The vector is a contiguous value (storing the elements inline) that you can store directly on
3838
/// the stack if needed.

src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ extern crate serde;
2828
#[cfg(not(feature="std"))]
2929
extern crate core as std;
3030

31+
#[cfg(not(target_pointer_width = "16"))]
3132
pub(crate) type LenUint = u32;
3233

34+
#[cfg(target_pointer_width = "16")]
35+
pub(crate) type LenUint = u16;
36+
3337
macro_rules! assert_capacity_limit {
3438
($cap:expr) => {
3539
if std::mem::size_of::<usize>() > std::mem::size_of::<LenUint>() {
3640
if $cap > LenUint::MAX as usize {
37-
panic!("ArrayVec: largest supported capacity is u32::MAX")
41+
#[cfg(not(target_pointer_width = "16"))]
42+
panic!("ArrayVec: largest supported capacity is u32::MAX");
43+
#[cfg(target_pointer_width = "16")]
44+
panic!("ArrayVec: largest supported capacity is u16::MAX");
3845
}
3946
}
4047
}

tests/tests.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ fn test_pop_at() {
680680
}
681681

682682
#[test]
683+
#[cfg(not(target_pointer_width = "16"))]
683684
fn test_sizes() {
684685
let v = ArrayVec::from([0u8; 1 << 16]);
685686
assert_eq!(vec![0u8; v.len()], &v[..]);
@@ -729,21 +730,17 @@ fn allow_max_capacity_arrayvec_type() {
729730
}
730731

731732
#[should_panic(expected="largest supported capacity")]
733+
#[cfg(not(target_pointer_width = "16"))]
732734
#[test]
733735
fn deny_max_capacity_arrayvec_value() {
734-
if mem::size_of::<usize>() <= mem::size_of::<u32>() {
735-
panic!("This test does not work on this platform. 'largest supported capacity'");
736-
}
737736
// this type is allowed to be used (but can't be constructed)
738737
let _v: ArrayVec<(), {usize::MAX}> = ArrayVec::new();
739738
}
740739

741740
#[should_panic(expected="index out of bounds")]
741+
#[cfg(not(target_pointer_width = "16"))]
742742
#[test]
743743
fn deny_max_capacity_arrayvec_value_const() {
744-
if mem::size_of::<usize>() <= mem::size_of::<u32>() {
745-
panic!("This test does not work on this platform. 'index out of bounds'");
746-
}
747744
// this type is allowed to be used (but can't be constructed)
748745
let _v: ArrayVec<(), {usize::MAX}> = ArrayVec::new_const();
749746
}

0 commit comments

Comments
 (0)