项目作者: fizyk20

项目描述 :
Generic array types in Rust
高级语言: Rust
项目地址: git://github.com/fizyk20/generic-array.git
创建时间: 2015-09-24T18:52:33Z
项目社区:https://github.com/fizyk20/generic-array

开源协议:MIT License

下载


Crates.io
Build Status
Rust Version

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.

Upgrading from 0.14 or using with hybrid-array 0.4

generic-array 0.14 has been officially deprecated, so here’s a quick guide on how to upgrade from generic-array 0.14 to 1.x. Note that libraries depending on generic-array will need to update their usage as well. Some libraries are moving to hybrid-array 0.4 instead, which we provide interoperability with generic-array 1.x via the hybrid-array-0_4 feature flag.


Click to expand

To upgrade to 1.x, change your Cargo.toml to use the new version:

toml [dependencies] generic-array = "1"

then in your code, go through and remove the <T> from ArrayLength<T> bounds, as the type parameter has been removed. It’s now just ArrayLength.

If you need to interoperate with generic-array 0.14, enable the compat-0_14 feature flag:

toml [dependencies] generic-array = { version = "1", features = ["compat-0_14"] }

then use the to_0_14/from_0_14/as_0_14/as_0_14_mut methods on GenericArray to convert between versions, or use the From/AsRef/AsMut implementations.

The arr! macro has changed to no longer require a type parameter, so change:

rust let array = arr![i32; 1, 2, 3]; // to let array = arr![1, 2, 3];

For interoperability with hybrid-array 0.4, enable the hybrid-array-0_4 feature flag:

toml [dependencies] generic-array = { version = "1", features = ["hybrid-array-0_4"] }

then use the to_ha0_4/from_ha0_4/as_ha0_4/as_ha0_4_mut methods on GenericArray to convert between versions, or use the From/AsRef/AsMut implementations.

We also implement the AssocArraySize and AsArrayRef/AsArrayMut traits from hybrid-array for GenericArray.

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:

  1. struct Foo<N> {
  2. data: [i32; N],
  3. }

Since 1.51, the below syntax is valid:

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

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:

  1. trait Bar {
  2. const LEN: usize;
  3. // Error: cannot perform const operation using `Self`
  4. fn bar(&self) -> Foo<{ Self::LEN }>;
  5. }

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

  1. struct Foo<N: ArrayLength> {
  2. data: GenericArray<i32, N>
  3. }
  4. trait Bar {
  5. type LEN: ArrayLength;
  6. fn bar(&self) -> Foo<Self::LEN>;
  7. }

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

  1. use generic_array::typenum::U5;
  2. struct Foo<T, N: ArrayLength> {
  3. data: GenericArray<T, N>
  4. }
  5. let foo = Foo::<i32, U5> { data: GenericArray::default() };

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

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

Feature flags

  1. [dependencies.generic-array]
  2. features = [
  3. "serde", # Serialize/Deserialize implementation
  4. "zeroize", # Zeroize implementation for setting array elements to zero
  5. "const-default", # Compile-time const default value support via trait
  6. "alloc", # Enables From/TryFrom implementations between GenericArray and Vec<T>/Box<[T]>
  7. "faster-hex", # Enables internal use of the `faster-hex` crate for faster hex encoding via SIMD
  8. "subtle", # Enables `subtle` crate support for constant-time equality checks and conditional selection
  9. "arbitrary", # Enables `arbitrary` crate support for fuzzing
  10. "bytemuck", # Enables `bytemuck` crate support
  11. "bitvec", # Enables `bitvec` crate support to use GenericArray as a storage backend for bit arrays
  12. "compat-0_14", # Enables interoperability with `generic-array` 0.14
  13. "hybrid-array-0_4" # Enables interoperability with `hybrid-array` 0.4
  14. ]