Skip to content

Commit

Permalink
Fix using grow to shrink to inline.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 6, 2019
1 parent 19de501 commit 50a50ed
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib.rs
Expand Up @@ -654,6 +654,7 @@ impl<A: Array> SmallVec<A> {
}
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();
Expand Down Expand Up @@ -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]);
}
}

0 comments on commit 50a50ed

Please sign in to comment.