- Notifications
You must be signed in to change notification settings - Fork77
Generic array types in Rust
License
fizyk20/generic-array
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 lengthN
, so this wouldn't work:
structFoo<N>{data:[i32;N],}
Since 1.51, the below syntax is valid:
structFoo<constN: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:
traitBar{constLEN:usize;// Error: cannot perform const operation using `Self`fnbar(&self) ->Foo<{Self::LEN}>;}
generic-array defines a new traitArrayLength
and a structGenericArray<T, N: ArrayLength>
, which lets the above be implemented as:
structFoo<N:ArrayLength>{data:GenericArray<i32,N>}traitBar{typeLEN:ArrayLength;fnbar(&self) ->Foo<Self::LEN>;}
TheArrayLength
trait is implemented forunsigned integer types fromtypenum crate. For example,GenericArray<T, U5>
would work almost like[T; 5]
:
use generic_array::typenum::U5;structFoo<T,N:ArrayLength>{data:GenericArray<T,N>}let foo =Foo::<i32,U5>{data:GenericArray::default()};
Thearr!
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);
[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]
About
Generic array types in Rust
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.