Skip to content

Commit

Permalink
Auto merge of #152 - ehuss:grow-to-shrink, r=emilio
Browse files Browse the repository at this point in the history
Fix using `grow` to shrink to inline.

Using `grow` on a spilled SmallVec to shrink to an unspilled SmallVec would result in a corrupted structure, as `capacity` was not updated.

Fixes #149

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/152)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jun 7, 2019
2 parents 19de501 + 50a50ed commit f96322b
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 f96322b

Please sign in to comment.