From 50a50ed90d6ad78d812a40680257d8338843869a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Thu, 6 Jun 2019 13:29:53 -0700 Subject: [PATCH] Fix using `grow` to shrink to inline. --- lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib.rs b/lib.rs index 5af32ba..2e0aa04 100644 --- a/lib.rs +++ b/lib.rs @@ -654,6 +654,7 @@ impl SmallVec { } self.data = SmallVecData::from_inline(mem::uninitialized()); ptr::copy_nonoverlapping(ptr, self.data.inline_mut().ptr_mut(), len); + self.capacity = len; } else if new_cap != cap { let mut vec = Vec::with_capacity(new_cap); let new_alloc = vec.as_mut_ptr(); @@ -2311,4 +2312,21 @@ mod tests { let decoded: SmallVec<[i32; 2]> = deserialize(&encoded).unwrap(); assert_eq!(small_vec, decoded); } + + #[test] + fn grow_to_shrink() { + let mut v: SmallVec<[u8; 2]> = SmallVec::new(); + v.push(1); + v.push(2); + v.push(3); + assert!(v.spilled()); + v.clear(); + // Shrink to inline. + v.grow(2); + assert!(!v.spilled()); + assert_eq!(v.capacity(), 2); + assert_eq!(v.len(), 0); + v.push(4); + assert_eq!(v[..], [4]); + } }