Skip to content

Commit

Permalink
Merge pull request #16 from Nemo157/fix-resize
Browse files Browse the repository at this point in the history
Fix TinyVec::resize across inline/heap boundary
  • Loading branch information
Lokathor authored Jan 14, 2020
2 parents fd3ce48 + 3335c63 commit 93ff65b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/tiny_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,12 @@ impl<A: Arrayish> TinyVec<A> {
A::Item: Clone,
{
match self {
TinyVec::Inline(a) => a.resize(new_len, new_val),
TinyVec::Inline(a) => if new_len > A::CAPACITY {
self.move_to_the_heap();
self.resize(new_len, new_val);
} else {
a.resize(new_len, new_val);
},
TinyVec::Heap(v) => v.resize(new_len, new_val),
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/tinyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ fn TinyVec_drain() {
assert_eq!(Vec::from_iter(tv.clone().drain(1..=1)), vec![2]);
assert_eq!(Vec::from_iter(tv.clone().drain(1..=2)), vec![2, 3]);
}

#[test]
fn TinyVec_resize() {
let mut tv: TinyVec<[i32; 10]> = Default::default();
tv.resize(20, 5);
assert_eq!(&tv[..], &[5; 20]);
}

0 comments on commit 93ff65b

Please sign in to comment.