• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-2,0201,397

tests/H03-May-2022-779609

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitignoreH A D05-Mar-2018147 1712

.travis.ymlH A D08-Jul-20191.2 KiB1918

CHANGELOG.mdH A D08-Aug-20203.7 KiB9179

Cargo.tomlH A D01-Jan-19701.3 KiB4640

Cargo.toml.orig-cargoH A D08-Aug-2020912 3826

DESIGN.mdH A D05-Jul-201923.7 KiB585413

LICENSEH A D12-Aug-20171.1 KiB2117

README.mdH A D11-Jun-20201.7 KiB6344

build.rsH A D11-Jun-2020146 65

rustfmt.tomlH A D15-Jun-201881 43

README.md

1[![Crates.io](https://img.shields.io/crates/v/generic-array.svg)](https://crates.io/crates/generic-array)
2[![Build Status](https://travis-ci.org/fizyk20/generic-array.svg?branch=master)](https://travis-ci.org/fizyk20/generic-array)
3# generic-array
4
5This crate implements generic array types for Rust.
6
7**Requires minumum Rust version of 1.36.0, or 1.41.0 for `From<[T; N]>` implementations**
8
9[Documentation](http://fizyk20.github.io/generic-array/generic_array/)
10
11## Usage
12
13The 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:
14
15```rust
16struct Foo<N> {
17	data: [i32; N]
18}
19```
20
21**generic-array** defines a new trait `ArrayLength<T>` and a struct `GenericArray<T, N: ArrayLength<T>>`, which let the above be implemented as:
22
23```rust
24struct Foo<N: ArrayLength<i32>> {
25	data: GenericArray<i32, N>
26}
27```
28
29The `ArrayLength<T>` trait is implemented by default for [unsigned integer types](http://fizyk20.github.io/generic-array/typenum/uint/index.html) from [typenum](http://fizyk20.github.io/generic-array/typenum/index.html) crate:
30
31```rust
32use generic_array::typenum::U5;
33
34struct Foo<N: ArrayLength<i32>> {
35    data: GenericArray<i32, N>
36}
37
38fn main() {
39    let foo = Foo::<U5>{data: GenericArray::default()};
40}
41```
42
43For example, `GenericArray<T, U5>` would work almost like `[T; 5]`:
44
45```rust
46use generic_array::typenum::U5;
47
48struct Foo<T, N: ArrayLength<T>> {
49    data: GenericArray<T, N>
50}
51
52fn main() {
53    let foo = Foo::<i32, U5>{data: GenericArray::default()};
54}
55```
56
57In version 0.1.1 an `arr!` macro was introduced, allowing for creation of arrays as shown below:
58
59```rust
60let array = arr![u32; 1, 2, 3];
61assert_eq!(array[2], 3);
62```
63