From dd802b30fa97ca97e014e6c17117cc32e0641d45 Mon Sep 17 00:00:00 2001 From: mental Date: Sun, 19 Jan 2020 04:57:34 +0000 Subject: [PATCH 1/2] Account for heap allocated capacities. --- src/tinyvec.rs | 8 ++++++-- tests/tinyvec.rs | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/tinyvec.rs b/src/tinyvec.rs index 9edf4f7..0bb84e9 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -170,11 +170,15 @@ impl TinyVec { /// The capacity of the `TinyVec`. /// - /// This is fixed based on the array type. + /// When not heap allocated this is fixed based on the array type. + /// Otherwise its the result of the underlying Vec::capacity. #[inline(always)] #[must_use] pub fn capacity(&self) -> usize { - A::CAPACITY + match self { + TinyVec::Inline(_) => A::CAPACITY, + TinyVec::Heap(v) => v.capacity(), + } } /// Removes all elements from the vec. diff --git a/tests/tinyvec.rs b/tests/tinyvec.rs index f539cd2..6295458 100644 --- a/tests/tinyvec.rs +++ b/tests/tinyvec.rs @@ -21,6 +21,12 @@ fn TinyVec_swap_remove() { assert_eq!(&tv[..], &[][..]); } +#[test] +fn TinyVec_capacity() { + let tv: TinyVec<[i32; 10]> = Default::default(); + assert_eq!(tv.capacity(), 10); +} + #[test] fn TinyVec_drain() { let mut tv: TinyVec<[i32; 10]> = Default::default(); From 59257ad80fdec0f3139667e40bbbce9e9b9e9a42 Mon Sep 17 00:00:00 2001 From: mental Date: Sun, 19 Jan 2020 05:07:35 +0000 Subject: [PATCH 2/2] Account for heap capacity in capacity unit test --- tests/tinyvec.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/tinyvec.rs b/tests/tinyvec.rs index 6295458..7a642ee 100644 --- a/tests/tinyvec.rs +++ b/tests/tinyvec.rs @@ -23,8 +23,11 @@ fn TinyVec_swap_remove() { #[test] fn TinyVec_capacity() { - let tv: TinyVec<[i32; 10]> = Default::default(); - assert_eq!(tv.capacity(), 10); + let mut tv: TinyVec<[i32; 1]> = Default::default(); + assert_eq!(tv.capacity(), 1); + tv.move_to_the_heap(); + tv.extend_from_slice(&[1, 2, 3, 4]); + assert_eq!(tv.capacity(), 4); } #[test]