Skip to content

Commit

Permalink
Special case initializers for f32, f64, char
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Oct 10, 2023
1 parent 64afdb4 commit 66a2927
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ macro_rules! concat_slices {
&ARR
}};

([char]: $($s:expr),+ $(,)?) => {
$crate::concat_slices!(['\x00'; char]: $($s),+)
};

([f32]: $($s:expr),+ $(,)?) => {
$crate::concat_slices!([0.0; f32]: $($s),+)
};

([f64]: $($s:expr),+ $(,)?) => {
$crate::concat_slices!([0.0; f64]: $($s),+)
};

([$T:ty]: $($s:expr),+ $(,)?) => {
$crate::concat_slices!([0; $T]: $($s),+)
};
Expand Down
18 changes: 12 additions & 6 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,26 @@ fn concat_slices_smoke() {
const TEST2: &[i32] = concat_slices!([i32]: &[1, 2, 3], TEST1);
assert_eq!(TEST2, [1, 2, 3, 1, 2, 3]);

const TEST3: &[f32] = concat_slices!([0.; f32]: &[1.], &[2.], &[3.]);
const TEST3: &[f32] = concat_slices!([f32]: &[1.], &[2.], &[3.]);
assert_eq!(TEST3, [1., 2., 3.]);

const TEST4: &[char] = concat_slices!(['\x00'; char]: &['a'], &['b'], &['c']);
const TEST4: &[char] = concat_slices!([char]: &['a'], &['b'], &['c']);
assert_eq!(TEST4, ['a', 'b', 'c']);

const TEST5: &[f32] = concat_slices!([1.0; f32]: &[1.], &[2.], &[3.]);
assert_eq!(TEST5, [1., 2., 3.]);

const TEST6: &[char] = concat_slices!(['0'; char]: &['a'], &['b'], &['c']);
assert_eq!(TEST6, ['a', 'b', 'c']);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct I(i32);
const TEST5: &[I] = concat_slices!([I(0); I]: &[I(1), I(2), I(3)]);
assert_eq!(TEST5, [I(1), I(2), I(3)]);
const TEST7: &[I] = concat_slices!([I(0); I]: &[I(1), I(2), I(3)]);
assert_eq!(TEST7, [I(1), I(2), I(3)]);

const DEF: I = I(123);
const TEST6: &[I] = concat_slices!([DEF; I]: &[I(1), I(2), I(3)]);
assert_eq!(TEST6, [I(1), I(2), I(3)]);
const TEST8: &[I] = concat_slices!([DEF; I]: &[I(1), I(2), I(3)]);
assert_eq!(TEST8, [I(1), I(2), I(3)]);
}

#[test]
Expand Down

0 comments on commit 66a2927

Please sign in to comment.