Docs.rs
  • generic-array-1.2.0
    • generic-array 1.2.0
    • Permalink
    • Docs.rs crate page
    • MIT
    • Links
    • Documentation
    • Repository
    • crates.io
    • Source
    • Owners
    • fizyk20
    • novacrazy
    • Dependencies
      • const-default ^1 normal optional
      • faster-hex ^0.10 normal optional
      • serde ^1.0 normal optional
      • typenum ^1.17 normal
      • zeroize ^1 normal optional
      • bincode ^1.0 dev
      • criterion ^0.5 dev
      • rand ^0.8 dev
      • serde_json ^1.0 dev
    • Versions
    • 100% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Badges
    • Builds
    • Metadata
    • Shorthand URLs
    • Download
    • Rustdoc JSON
    • Build queue
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate generic_array

generic_array1.2.0

  • All Items

Sections

  • Feature flags

Crate Items

  • Re-exports
  • Modules
  • Macros
  • Structs
  • Traits
  • Type Aliases

Crates

  • generic_array

Crate generic_array

Source
Expand description

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

**Requires minimum Rust version of 1.83.0

Documentation on GH Pages may be required to view certain types on foreign crates.

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:

use generic_array::{GenericArray, ArrayLength};

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. 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
]

Re-exports§

pub extern crate typenum;

Modules§

functional
Functional programming with generic sequences
sequence
Useful traits for manipulating sequences of data stored in GenericArrays

Macros§

arr
Macro allowing for easy construction of Generic Arrays.
box_arralloc
Like arr!, but returns a Box<GenericArray<T, N>>

Structs§

GenericArray
Struct representing a generic array - GenericArray<T, N> works like [T; N]
GenericArrayIter
An iterator that moves out of a GenericArray
LengthError
Error for TryFrom and try_from_iter

Traits§

ArrayLength
Trait used to define the number of elements in a GenericArray.
IntoArrayLength
Implemented for types which can have an associated ArrayLength, such as Const<N> for use with const-generics.

Type Aliases§

ConstArrayLength
Associated ArrayLength for one Const<N>

Results

Settings
Help
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.