Skip to content

Commit

Permalink
bugfix for append not set self.len properly (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlzhang committed Mar 23, 2024
1 parent c01d595 commit 1bd2dbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -1000,6 +1000,7 @@ impl<T, const N: usize> SmallVec<T, N> {
// SAFETY: we have a mutable reference to each vector and each uniquely owns its memory.
// so the ranges can't overlap
unsafe { copy_nonoverlapping(other.as_ptr(), ptr, other_len) };
unsafe { self.set_len(total_len) }
}

#[inline]
Expand Down
19 changes: 19 additions & 0 deletions src/tests.rs
Expand Up @@ -328,6 +328,25 @@ fn test_insert_many() {
);
}

#[test]
fn test_append() {
let mut v: SmallVec<u8, 8> = SmallVec::new();
for x in 0..4 {
v.push(x);
}
assert_eq!(v.len(), 4);

let mut n: SmallVec<u8, 2> = SmallVec::from_buf([5, 6]);
v.append(&mut n);
assert_eq!(v.len(), 6);
assert_eq!(n.len(), 0);

assert_eq!(
&v.iter().map(|v| *v).collect::<Vec<_>>(),
&[0, 1, 2, 3, 5, 6]
);
}

struct MockHintIter<T: Iterator> {
x: T,
hint: usize,
Expand Down

0 comments on commit 1bd2dbc

Please sign in to comment.