concat-arrays
A Rust procedural macro for concatenating fixed-size arrays into a single array
Repository Health
Technical Analysis
concat-arrays is a small Rust procedural-macro crate that provides a single concat_arrays! macro for joining several fixed-size arrays into one. It works entirely at compile time, producing a new array whose element type matches the inputs and whose length is the sum of the input lengths.
Because Rust cannot yet infer the length of the returned array from the macro alone, the resulting length must be inferable from the surrounding context. Mis-specifying the length is safe: it yields a compilation error rather than incorrect runtime behavior, keeping the abstraction sound.
What You Get
- A single
concat_arrays!macro that concatenates any number of fixed-size arrays - Compile-time expansion with zero runtime overhead
- Element-type checking so mismatched arrays fail to compile
- Safe length handling: an incorrect length produces a compile error, never broken code
Common Use Cases
- Combining fixed-size byte arrays when building binary buffers or headers
- Assembling constant lookup tables from smaller array literals
- Merging fixed-length coordinate or vector arrays in numeric code
Under The Hood
Architecture - The crate is a single src/lib.rs defining one #[proc_macro] function, concat_arrays. It parses the comma-separated argument list into a Punctuated<Expr, Comma> via a small syn::parse::Parse implementation, then emits code with quote! that binds each input to a temporary, declares a #[repr(C)] struct holding all arrays contiguously, and transmutes that struct into the output array. A if false branch carries type-level constraints that make the compiler check element types and infer the concatenated length.
Tech Stack - Written in Rust 2018 as a proc-macro = true crate. Dependencies are the standard procedural-macro trio: syn (with the full feature) for parsing, quote for code generation, and proc-macro2 for token handling. trybuild is used as a dev-dependency for compile-fail tests.
Code Quality - The implementation is compact and well-commented, with doc comments and an example on the public macro. It includes a tests/ directory with runtime assertions plus trybuild compile-fail cases that verify misuse is rejected at compile time. The single unsafe transmute is guarded by type constraints so mistakes surface as compile errors.
API Design - The public surface is a single macro with an obvious call form, concat_arrays!(a, b, c), that reads exactly like the operation it performs. The one ergonomic caveat, that the output length must be inferable from context, is documented on the macro itself, and violating it produces a clear compile error rather than silent breakage.