From 798c9cff1c9e85bbfbb41a0f80336b7391b41a45 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 30 Aug 2024 02:59:01 +1000 Subject: [PATCH 1/2] Fix bug in ArrayVec::extend_from_slice 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). --- internals/src/array_vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internals/src/array_vec.rs b/internals/src/array_vec.rs index f75924ba1..8b6e54bf2 100644 --- a/internals/src/array_vec.rs +++ b/internals/src/array_vec.rs @@ -78,7 +78,7 @@ mod safety_boundary { assert!(new_len <= CAP, "buffer overflow"); // SAFETY: MaybeUninit has the same layout as T let slice = unsafe { &*(slice as *const _ as *const [MaybeUninit]) }; - self.data[self.len..].copy_from_slice(slice); + self.data[self.len..new_len].copy_from_slice(slice); self.len = new_len; } } From 4024ba59103a84dcc94d3d2208f64b26ac478c0d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 30 Aug 2024 03:00:15 +1000 Subject: [PATCH 2/2] Add unit test for ArrayVec::extend_from_slice Test that we can extend an `ArrayVec` with a slice that is less than remaining capacity. --- internals/src/array_vec.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internals/src/array_vec.rs b/internals/src/array_vec.rs index 8b6e54bf2..b728503e8 100644 --- a/internals/src/array_vec.rs +++ b/internals/src/array_vec.rs @@ -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::::new(); + av.extend_from_slice(b"abc"); + } }