generic-array
This crate implements a structure that can be used as a generic array type.
**Requires minimum Rust version of 1.65.0
Documentation on GH Pages may be required to view certain types on foreign crates.
Usage
Before Rust 1.51, arrays [T; N]
were problematic in that they couldn't be generic with respect to the length N
, so this wouldn't work:
Since 1.51, the below syntax is valid:
However, the const-generics we have as of writing this are still the minimum-viable product (min_const_generics
), so many situations still result in errors, such as this example:
>;
}
generic-array defines a new trait ArrayLength
and a struct GenericArray<T, N: ArrayLength>
, which lets the above be implemented as:
The ArrayLength
trait is implemented for unsigned integer types from typenum crate. For example, GenericArray<T, U5>
would work almost like [T; 5]
:
use U5;
let foo = ;
The arr!
macro is provided to allow easier creation of literal arrays, as shown below:
let array = arr!;
// array: GenericArray<i32, typenum::U3>
assert_eq!;
Feature flags
[]
= [
"more_lengths", # Expands From/Into implementation for more array lengths
"serde", # Serialize/Deserialize implementation
"zeroize", # Zeroize implementation for setting array elements to zero
"const-default", # Compile-time const default value support via trait
"alloc", # Enables From/TryFrom implementations between GenericArray and Vec<T>/Box<[T]>
"faster-hex" # Enables internal use of the `faster-hex` crate for faster hex encoding via SIMD
]