Skip to content

Commit 21034b3

Browse files
authored
Merge pull request #71 from clarcharr/master
Make zero-capacity ArrayVec a zero-sized type.
2 parents c8d12ce + 74f86a7 commit 21034b3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/array.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ pub trait ArrayExt : Array {
4444

4545
impl<A> ArrayExt for A where A: Array { }
4646

47+
impl Index for () {
48+
#[inline(always)]
49+
fn to_usize(self) -> usize { 0 }
50+
#[inline(always)]
51+
fn from(ix: usize) -> Self { () }
52+
}
53+
54+
impl Index for bool {
55+
#[inline(always)]
56+
fn to_usize(self) -> usize { self as usize }
57+
#[inline(always)]
58+
fn from(ix: usize) -> Self { ix != 0 }
59+
}
60+
4761
impl Index for u8 {
4862
#[inline(always)]
4963
fn to_usize(self) -> usize { self as usize }
@@ -97,7 +111,10 @@ macro_rules! fix_array_impl_recursive {
97111
);
98112
}
99113

100-
fix_array_impl_recursive!(u8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
114+
115+
fix_array_impl_recursive!((), 0,);
116+
fix_array_impl_recursive!(bool, 1,);
117+
fix_array_impl_recursive!(u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
101118
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
102119
28, 29, 30, 31, );
103120

tests/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ fn test_compact_size() {
158158
println!("{}", mem::size_of::<ByteArray>());
159159
assert!(mem::size_of::<ByteArray>() <= 8);
160160

161+
// 1 enum tag + 1 drop flag
162+
type EmptyArray = ArrayVec<[u8; 0]>;
163+
println!("{}", mem::size_of::<EmptyArray>());
164+
assert!(mem::size_of::<EmptyArray>() <= 2);
165+
161166
// 12 element size + 1 enum tag + 3 padding + 1 len + 1 drop flag + 2 padding
162167
type QuadArray = ArrayVec<[u32; 3]>;
163168
println!("{}", mem::size_of::<QuadArray>());

0 commit comments

Comments
 (0)