generic-array

Generic array types in Rust

Latest version: 1.3.5 registry icon
Maintenance score
72
Safety score
95
Popularity score
80
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
1.3.5 0 0 0 0 0
1.3.4 0 0 0 0 0
1.3.3 0 0 0 0 0
1.3.2 0 0 0 0 0
1.3.1 0 0 0 0 0
1.3.0 0 0 0 0 0
1.2.1 0 0 0 0 0
1.2.0 0 0 0 0 0
1.1.1 0 0 0 0 0
1.1.0 0 0 0 0 0
1.0.1 0 0 0 0 0
1.0.0 0 0 0 0 0
0.14.9 0 0 0 0 0
0.14.8 0 0 0 0 0
0.14.7 0 0 0 0 0
0.14.6 0 0 0 0 0
0.14.5 0 0 0 0 0
0.14.4 0 0 0 0 0
0.14.3 0 0 0 0 0
0.14.2 0 0 0 0 0
0.14.1 0 0 0 0 0
0.14.0 0 0 0 0 0
0.13.3 0 0 0 0 0
0.13.2 0 0 1 0 0
0.13.1 0 0 1 0 0
0.13.0 0 0 1 0 0
0.12.4 0 0 0 0 0
0.12.3 0 0 1 0 0
0.12.2 0 0 1 0 0
0.12.1 0 0 1 0 0
0.12.0 0 0 1 0 0
0.11.2 0 0 0 0 0
0.11.1 0 0 1 0 0
0.11.0 0 0 1 0 0
0.10.1 0 0 0 0 0
0.10.0 0 0 1 0 0
0.9.1 0 0 0 0 0
0.9.0 0 0 1 0 0
0.8.4 0 0 0 1 0
0.8.3 0 0 1 0 0
0.8.2 0 0 1 0 0
0.8.1 0 0 1 0 0
0.8.0 0 0 1 0 0
0.7.3 0 0 0 0 0
0.7.2 0 0 0 0 0
0.7.1 0 0 0 0 0
0.7.0 0 0 0 0 0
0.6.0 0 0 0 0 0
0.5.1 0 0 0 0 0
0.5.0 0 0 0 0 0
0.4.1 0 0 0 0 0
0.4.0 0 0 0 0 0
0.3.2 0 0 0 0 0
0.3.1 0 0 0 0 0
0.3.0 0 0 0 0 0
0.2.1 0 0 0 0 0
0.2.0 0 0 0 0 0
0.1.2 0 0 0 0 0
0.1.1 0 0 0 0 0
0.1.0 0 0 0 0 0
0.0.3 0 0 0 0 0
0.0.2 0 0 0 0 0
0.0.1 0 0 0 0 0

Stability
Latest release:

1.3.5 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



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:

[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:

[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:

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:

[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:

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 = [
    "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
    "subtle",           # Enables `subtle` crate support for constant-time equality checks and conditional selection
    "arbitrary",        # Enables `arbitrary` crate support for fuzzing
    "bytemuck",         # Enables `bytemuck` crate support
    "bitvec",           # Enables `bitvec` crate support to use GenericArray as a storage backend for bit arrays
    "compat-0_14",      # Enables interoperability with `generic-array` 0.14
    "hybrid-array-0_4"  # Enables interoperability with `hybrid-array` 0.4
]