Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fizyk20/generic-array
Generic array types in Rust
https://github.com/fizyk20/generic-array
rust
Last synced: 7 days ago
JSON representation
Generic array types in Rust
- Host: GitHub
- URL: https://github.com/fizyk20/generic-array
- Owner: fizyk20
- License: mit
- Created: 2015-09-24T18:52:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-07-05T05:42:11.000Z (4 months ago)
- Last Synced: 2024-09-13T16:27:42.487Z (about 2 months ago)
- Topics: rust
- Language: Rust
- Size: 17 MB
- Stars: 404
- Watchers: 10
- Forks: 77
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rust-cn - fizyk20/generic-array - ci.org/fizyk20/generic-array.svg?branch=master">](https://travis-ci.org/fizyk20/generic-array) (Libraries / Data structures)
- awesome-rust - fizyk20/generic-array - ci.org/fizyk20/generic-array.svg?branch=master">](https://travis-ci.org/fizyk20/generic-array) (Libraries / Data structures)
- awesome-rust - fizyk20/generic-array
- awesome-rust-cn - fizyk20/generic-array
- awesome-rust-zh - fizyk20/generic-array - 通过 typenums,允许 sized(固定大小)数组的 hack [<img src="https://api.travis-ci.org/fizyk20/generic-array.svg?branch=master">](https://travis-ci.org/fizyk20/generic-array) (库 / 数据结构)
- awesome-rust - fizyk20/generic-array - a hack to allow for arrays sized by typenums (Libraries / Data structures)
- awesome-rust - fizyk20/generic-array - ci.org/fizyk20/generic-array.svg?branch=master">](https://travis-ci.org/fizyk20/generic-array) (库 Libraries / 数据结构 Data structures)
- fucking-awesome-rust - fizyk20/generic-array - a hack to allow for arrays sized by typenums (Libraries / Data structures)
- fucking-awesome-rust - fizyk20/generic-array - a hack to allow for arrays sized by typenums (Libraries / Data structures)
README
[![Crates.io](https://img.shields.io/crates/v/generic-array.svg)](https://crates.io/crates/generic-array)
[![Build Status](https://github.com/fizyk20/generic-array/actions/workflows/CI.yml/badge.svg)](https://github.com/fizyk20/generic-array/actions/workflows/CI.yml)
# generic-arrayThis 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](https://fizyk20.github.io/generic-array/generic_array/) 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:
```rust
struct Foo {
data: [i32; N],
}
```Since 1.51, the below syntax is valid:
```rust
struct Foo {
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:
```rust
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`, which lets the above be implemented as:
```rust
struct Foo {
data: GenericArray
}trait Bar {
type LEN: ArrayLength;
fn bar(&self) -> Foo;
}
```The `ArrayLength` trait is implemented 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. For example, `GenericArray` would work almost like `[T; 5]`:
```rust
use generic_array::typenum::U5;struct Foo {
data: GenericArray
}let foo = Foo:: { data: GenericArray::default() };
```The `arr!` macro is provided to allow easier creation of literal arrays, as shown below:
```rust
let array = arr![1, 2, 3];
// array: GenericArray
assert_eq!(array[2], 3);
```## Feature flags
```toml
[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/Box<[T]>
"faster-hex" # Enables internal use of the `faster-hex` crate for faster hex encoding via SIMD
]
```