Skip to content

Commit 94dff4e

Browse files
committed
Fix size check when composed array is too long
Ensure the union fields of ArrayConcatComposed have matching sizes by updating the size validations in the macros concat_array and split_array. Previously, the size check in concat_arrays were only catching cases where the inferred result type was shorter than the sum of input array lengths. It failed to detect longer result arrays, since the size of Self matches its biggest union field. For example, this compiled without error: let c: [u32; 6] = concat_arrays!([1, 2, 3], [4, 5]); This led to arrays containing undefined values. The same applied for split_array when the input array is longer than the sum of the output array lengths. While this did not lead to undefined values, the exceeding elements would not be dropped.
1 parent 79ac0b0 commit 94dff4e

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

src/lib.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ macro_rules! concat_arrays_size {
99
}
1010

1111
/// Concatenates provided arrays.
12+
///
13+
/// The macro verifies that the combined length of the input arrays matches the length of the result array.
14+
/// The examples below will fail to compile as the input arrays are either too short or too long.
15+
/// ```compile_fail
16+
/// # #[macro_use] extern crate array_concat;
17+
/// # fn main() {
18+
/// let c: [u32; 6] = concat_arrays!([1, 2, 3], [4, 5]); // too short
19+
/// # }
20+
/// ```
21+
/// ```compile_fail
22+
/// # #[macro_use] extern crate array_concat;
23+
/// # fn main() {
24+
/// let c: [u32; 6] = concat_arrays!([1, 2, 3], [4, 5, 6, 7]); // too long
25+
/// # }
26+
/// ```
1227
#[macro_export]
1328
macro_rules! concat_arrays {
1429
($( $array:expr ),*) => ({
@@ -39,7 +54,7 @@ macro_rules! concat_arrays {
3954
}
4055

4156
impl<T, A, B, const N: usize> ArrayConcatComposed<T, A, B, N> {
42-
const PANIC: bool = $crate::_const_assert_same_size::<[T; N], Self>();
57+
const PANIC: bool = $crate::_const_assert_same_size::<[T; N], ArrayConcatDecomposed::<T, A, B>>();
4358

4459
#[inline(always)]
4560
const fn have_same_size(&self) -> bool {
@@ -89,6 +104,21 @@ macro_rules! flatten_split {
89104
}
90105

91106
/// Split the provided array into the specified sizes.
107+
///
108+
/// The macro verifies that the length of the input array matches the combined length of the result arrays.
109+
/// The examples below will fail to compile as the input array is either too short or too long.
110+
/// ```compile_fail
111+
/// # #[macro_use] extern crate array_concat;
112+
/// # fn main() {
113+
/// let (left, right): ([u32; 3], [u32; 4]) = split_array!([1, 2, 3, 4, 5, 6], 3, 4); // too short
114+
/// # }
115+
/// ```
116+
/// ```compile_fail
117+
/// # #[macro_use] extern crate array_concat;
118+
/// # fn main() {
119+
/// let (left, right): ([u32; 3], [u32; 2]) = split_array!([1, 2, 3, 4, 5, 6], 3, 2); // too long
120+
/// # }
121+
/// ```
92122
#[macro_export]
93123
macro_rules! split_array {
94124
($array:expr, $size:expr) => ($array);
@@ -141,7 +171,7 @@ macro_rules! split_array {
141171
}
142172

143173
impl<T, A, B, const N: usize> ArrayConcatComposed<T, A, B, N> {
144-
const PANIC: bool = $crate::_const_assert_same_size::<[T; N], Self>();
174+
const PANIC: bool = $crate::_const_assert_same_size::<[T; N], ArrayConcatDecomposed::<T, A, B>>();
145175

146176
#[inline(always)]
147177
const fn have_same_size(&self) -> bool {

0 commit comments

Comments
 (0)