Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable non-const concat #2

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ license = "MIT"
keywords = ["array", "concat"]
categories = ["data-structures", "no-std"]
readme = "README.md"

[features]
const_panic = []
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# array-concat

Macros for concatenating const arrays.
Macros for concatenating arrays.

To add to your Cargo.toml:
```toml
Expand All @@ -30,5 +30,10 @@ fn main() {
assert_eq!([1, 2, 3, 4, 5], C);
assert_eq!([1, 2, 3, 4, 5], c);
assert_eq!([S(true), S(false)], F);

let a = [1, 2, 3];
let b = [4, 5];
let c = concat_arrays!(a, b); // Size is inferred by the assert below
assert_eq!([1, 2, 3, 4, 5], c);
}
```
54 changes: 45 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,55 @@ macro_rules! concat_arrays_size {
#[macro_export]
macro_rules! concat_arrays {
($( $array:expr ),*) => ({
const __ARRAY_SIZE__: usize = $crate::concat_arrays_size!($($array),*);

#[repr(C)]
struct ArrayConcatDecomposed<T>($([T; $array.len()]),*);
struct ArrayConcatDecomposed<T, A, B>(core::mem::ManuallyDrop<[T; 0]>, core::mem::ManuallyDrop<A>, core::mem::ManuallyDrop<B>);

impl<T> ArrayConcatDecomposed<T, [T; 0], [T; 0]> {
#[inline(always)]
const fn default() -> Self {
Self::new(core::mem::ManuallyDrop::new([]), [])
}
}
impl<T, A, B> ArrayConcatDecomposed<T, A, B> {
#[inline(always)]
const fn new(a: core::mem::ManuallyDrop<A>, b: B) -> Self {
Self(core::mem::ManuallyDrop::new([]), a, core::mem::ManuallyDrop::new(b))
}
#[inline(always)]
const fn concat<const N: usize>(self, v: [T; N]) -> ArrayConcatDecomposed<T, A, ArrayConcatDecomposed<T, B, [T; N]>> {
ArrayConcatDecomposed::new(self.1, ArrayConcatDecomposed::new(self.2, v))
}
}

#[repr(C)]
union ArrayConcatComposed<T, const N: usize> {
union ArrayConcatComposed<T, A, B, const N: usize> {
full: core::mem::ManuallyDrop<[T; N]>,
decomposed: core::mem::ManuallyDrop<ArrayConcatDecomposed<T>>,
decomposed: core::mem::ManuallyDrop<ArrayConcatDecomposed<T, A, B>>,
}

impl<T, const N: usize> ArrayConcatComposed<T, N> {
impl<T, A, B, const N: usize> ArrayConcatComposed<T, A, B, N> {
const HAVE_SAME_SIZE: bool = core::mem::size_of::<[T; N]>() == core::mem::size_of::<Self>();

#[cfg(feature="const_panic")]
const PANIC: bool = Self::HAVE_SAME_SIZE || panic!("Size Mismatch");

#[cfg(not(feature="const_panic"))]
const PANIC: bool = !["Size mismatch"][!Self::HAVE_SAME_SIZE as usize].is_empty();

#[inline(always)]
const fn have_same_size(&self) -> bool {
core::mem::size_of::<[T; N]>() == core::mem::size_of::<Self>()
Self::PANIC
}
}

let composed = ArrayConcatComposed { decomposed: core::mem::ManuallyDrop::new(ArrayConcatDecomposed ( $($array),* ))};
let composed = ArrayConcatComposed {
decomposed: core::mem::ManuallyDrop::new(
ArrayConcatDecomposed::default()$(.concat($array))*,
)
};

// Sanity check that composed's two fields are the same size
["Size mismatch"][!composed.have_same_size() as usize];
composed.have_same_size();

// SAFETY: Sizes of both fields in composed are the same so this assignment should be sound
core::mem::ManuallyDrop::into_inner(unsafe { composed.full })
Expand Down Expand Up @@ -71,4 +99,12 @@ mod tests {
assert_eq!([1, 2, 3, 4, 5, 6, 7, 8], F);
assert_eq!([1, 2, 3, 4, 5, 6, 7, 8], f);
}

#[test]
fn test_non_const_arrays() {
let a = [1, 2, 3];
let c = [4, 5];
let f = concat_arrays!(a, c, [6, 7, 8]);
assert_eq!([1, 2, 3, 4, 5, 6, 7, 8], f);
}
}
Loading