array-plus-extra
An array type that holds N+EXTRA elements using const generic parameters, providing safe slice access to contiguous memory.
This allows the creation of arrays that would require more powerful const-generics, e.g. [T; N+3].
Features
- Specify both base size (N) and extra elements (EXTRA) at compile time
- Deref to
&{mut} [T]provides safe access to all N+EXTRA elements as_slice()andas_mut_slice()work in const contexts- No runtime overhead compared to raw arrays
- Works in embedded and bare-metal environments
- All unsafe code verified with Miri for undefined behavior
- Optional
serdesupport for serialization/deserialization
Examples
Basic usage
use ArrayPlusExtra;
// Create an array with 5 base elements + 3 extra = 8 total elements.
let arr: = new;
// Access via deref to slice.
assert_eq!;
assert_eq!;
assert_eq!;
// Use slice methods.
let sum: i32 = arr.iter.sum;
assert_eq!; // 42 * 8
Mutable access
use ArrayPlusExtra;
let mut arr: = new;
// Modify through deref_mut.
arr = 10;
arr = 20;
arr = 30;
arr = 40;
assert_eq!;
assert_eq!;
Const contexts
use ArrayPlusExtra;
const ARR: = new;
const SLICE: & = ARR.as_slice;
const LEN: usize = SLICE.len;
assert_eq!;
assert_eq!;
Testing
Run tests:
Run tests with Miri (requires nightly):
Minimum Supported Rust Version (MSRV)
This crate requires Rust 1.85 or later due to the use of Edition 2024 and const fn features.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.