• Stars
    star
    404
  • Rank 106,897 (Top 3 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generic array types in Rust

Crates.io Build Status

generic-array

This crate implements a structure that can be used as a generic array type.

**Requires minumum 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:

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

Since 1.51, the below syntax is valid:

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

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:

trait Bar {
    const LEN: usize;

    // Error: cannot perform const operation using `Self`
    fn bar(&self) -> Foo<{ Self::LEN }>;
}

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

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

trait Bar {
    type LEN: ArrayLength;
    fn bar(&self) -> Foo<Self::LEN>;
}

The ArrayLength trait is implemented for unsigned integer types from typenum crate. For example, GenericArray<T, U5> would work almost like [T; 5]:

use generic_array::typenum::U5;

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

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

The arr! macro is provided to allow easier creation of literal arrays, as shown below:

let array = arr![1, 2, 3];
//  array: GenericArray<i32, typenum::U3>
assert_eq!(array[2], 3);

Feature flags

[dependencies.generic-array]
features = [
    "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
]

More Repositories

1

differential-geometry

A Rust crate for differential-geometric calculations.
Rust
35
star
2

ithkuil

A set of tools for analysis of texts in the Ithkuil constructed language
Python
29
star
3

4d-labyrinth

A 4-dimensional labyrinth
Rust
26
star
4

gr-engine

A general-relativistic physics engine
C++
10
star
5

gr-engine-rs

A general-relativistic physics engine written in Rust
Rust
9
star
6

bhs-sim

Black Hole Simulator
C
8
star
7

ithkuil-web

An Ithkuil web-app
HTML
7
star
8

rust-mpfr

MPFR bindings for Rust
Rust
5
star
9

atm-raytracer

A raytracer for panoramas using real-life elevation data
Rust
5
star
10

chatbot

An easily extensible chatbot supporting multiple backends, written in Rust
Rust
4
star
11

ithkuil-utils

Utilities for Ithkuil
4
star
12

gr-raytracer

A general-relativistic raytracer
C++
4
star
13

python-barcode

Barcode generation library in Python
Python
4
star
14

asm-8051-rs

An assembler for 8051 architecture written in Rust
Rust
3
star
15

test-closest

A simulation for testing close group attacks
Rust
2
star
16

fractals

Simple fractal generator with a GUI
Rust
2
star
17

universal-chat

A universal Rust interface for connecting with various chat services
Rust
2
star
18

epidemic-sim

A simple epidemic simulator
Rust
1
star
19

dted

Rust implementation of the DTED file format
Rust
1
star
20

geotiff-rs

A Rust library for reading GeoTIFF files
Rust
1
star
21

atm-refraction

A Rust crate for atmospheric refraction calculations
Rust
1
star
22

labyrinth4d-android

A simple 4-dimensional game for Android
Java
1
star
23

gravity-sim

A simple simulator of Newtonian gravity in Rust and GTK
Rust
1
star
24

fake_clock

Rust crate for a fake clock
Rust
1
star
25

coriolis-sim-3d

A 3D simulator for showcasing the Coriolis effect
Rust
1
star
26

numeric-algs

Numerical algorithms in Rust
Rust
1
star
27

tides-ft

Small program for calculating the Fourier transform of data describing the variability of the sea water level over time.
Rust
1
star