Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generic array types in Rust

License

NotificationsYou must be signed in to change notification settings

fizyk20/generic-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crates.ioBuild Status

generic-array

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.

Usage

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);

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]

About

Generic array types in Rust

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors38

Languages


[8]ページ先頭

©2009-2025 Movatter.jp