36 releases

Uses old Rust 2015

0.14.4 Aug 8, 2020
0.14.3 Jul 11, 2020
0.14.2 Jun 11, 2020
0.13.2 Jul 8, 2019
0.0.3 Nov 1, 2015

#1 in #array

Download history 187087/week @ 2020-06-01 197278/week @ 2020-06-08 191226/week @ 2020-06-15 198331/week @ 2020-06-22 188151/week @ 2020-06-29 203990/week @ 2020-07-06 208423/week @ 2020-07-13 212729/week @ 2020-07-20 221226/week @ 2020-07-27 230073/week @ 2020-08-03 225492/week @ 2020-08-10 230150/week @ 2020-08-17 240947/week @ 2020-08-24 229462/week @ 2020-08-31 237859/week @ 2020-09-07 242583/week @ 2020-09-14

834,203 downloads per month
Used in 4,818 crates (181 directly)

MIT license

60KB
1.5K SLoC

Crates.io Build Status

generic-array

This crate implements generic array types for Rust.

Requires minumum Rust version of 1.36.0, or 1.41.0 for From<[T; N]> implementations

Documentation

Usage

The Rust arrays [T; N] are problematic in that they can't be used generically with respect to N, so for example this won't work:

struct Foo<N> {
	data: [i32; N]
}

generic-array defines a new trait ArrayLength<T> and a struct GenericArray<T, N: ArrayLength<T>>, which let the above be implemented as:

struct Foo<N: ArrayLength<i32>> {
	data: GenericArray<i32, N>
}

The ArrayLength<T> trait is implemented by default for unsigned integer types from typenum crate:

use generic_array::typenum::U5;

struct Foo<N: ArrayLength<i32>> {
    data: GenericArray<i32, N>
}

fn main() {
    let foo = Foo::<U5>{data: GenericArray::default()};
}

For example, GenericArray<T, U5> would work almost like [T; 5]:

use generic_array::typenum::U5;

struct Foo<T, N: ArrayLength<T>> {
    data: GenericArray<T, N>
}

fn main() {
    let foo = Foo::<i32, U5>{data: GenericArray::default()};
}

In version 0.1.1 an arr! macro was introduced, allowing for creation of arrays as shown below:

let array = arr![u32; 1, 2, 3];
assert_eq!(array[2], 3);

Dependencies

~225KB