Skip to content

Commit f96322b

Browse files
author
bors-servo
authored
Auto merge of #152 - ehuss:grow-to-shrink, r=emilio
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 -->
2 parents 19de501 + 50a50ed commit f96322b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ impl<A: Array> SmallVec<A> {
654654
}
655655
self.data = SmallVecData::from_inline(mem::uninitialized());
656656
ptr::copy_nonoverlapping(ptr, self.data.inline_mut().ptr_mut(), len);
657+
self.capacity = len;
657658
} else if new_cap != cap {
658659
let mut vec = Vec::with_capacity(new_cap);
659660
let new_alloc = vec.as_mut_ptr();
@@ -2311,4 +2312,21 @@ mod tests {
23112312
let decoded: SmallVec<[i32; 2]> = deserialize(&encoded).unwrap();
23122313
assert_eq!(small_vec, decoded);
23132314
}
2315+
2316+
#[test]
2317+
fn grow_to_shrink() {
2318+
let mut v: SmallVec<[u8; 2]> = SmallVec::new();
2319+
v.push(1);
2320+
v.push(2);
2321+
v.push(3);
2322+
assert!(v.spilled());
2323+
v.clear();
2324+
// Shrink to inline.
2325+
v.grow(2);
2326+
assert!(!v.spilled());
2327+
assert_eq!(v.capacity(), 2);
2328+
assert_eq!(v.len(), 0);
2329+
v.push(4);
2330+
assert_eq!(v[..], [4]);
2331+
}
23142332
}

0 commit comments

Comments
 (0)