Merge rust-bitcoin/rust-bitcoin#3272: Fix bug in `ArrayVec::extend_from_slice`

4024ba5910 Add unit test for ArrayVec::extend_from_slice (Tobin C. Harding)
798c9cff1c Fix bug in ArrayVec::extend_from_slice (Tobin C. Harding)

Pull request description:

  Currently the source slice must be the exact length to fill the array to max capacity, this is an unnecessary restriction since an `ArrayVec` is a variable sized data structure.

  Set the destination slice to be the same length as the source slice (still maintain capacity checks).

ACKs for top commit:
  Kixunil:
    ACK 4024ba5910
  apoelstra:
    ACK 4024ba5910 successfully ran local tests

Tree-SHA512: b7a510ba6f6e5cf5a535d691f79da98e29558eb9a50b60d41e080769560e9b9f0b8a831806dc9f159c888001951e17b5bbd970363d6a2e57fa9748902548b3ab
This commit is contained in:
merge-script 2024-08-30 16:45:10 +00:00
commit 904fefba5a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 7 additions and 1 deletions

View File

@ -78,7 +78,7 @@ mod safety_boundary {
assert!(new_len <= CAP, "buffer overflow");
// SAFETY: MaybeUninit<T> has the same layout as T
let slice = unsafe { &*(slice as *const _ as *const [MaybeUninit<T>]) };
self.data[self.len..].copy_from_slice(slice);
self.data[self.len..new_len].copy_from_slice(slice);
self.len = new_len;
}
}
@ -181,4 +181,10 @@ mod tests {
let mut av = ArrayVec::<_, 0>::new();
av.extend_from_slice(&[42]);
}
#[test]
fn extend_from_slice() {
let mut av = ArrayVec::<u8, 8>::new();
av.extend_from_slice(b"abc");
}
}