traits icon indicating copy to clipboard operation
traits copied to clipboard

Tracking issue for migrating from `GenericArray` to const generics

Open tarcieri opened this issue 2 years ago • 3 comments

We've had an informal plan to migrate from the generic-array/typenum crates for years. People ask about it quite frequently ever since min_const_generics have been stabilized. I thought it would be good to have a tracking issue we can point people to.

Unfortunately min_const_generics is not quite up to the challenge of replacing our existing usages of generic-array. Here are open issues tracking the two main features we need upstream:

  • https://github.com/rust-lang/rust/issues/60551
  • https://github.com/rust-lang/rust/issues/76560

The first issue (60551) is arguably the biggest blocker. All of the trait crates defined in this repo use associated ArrayLength types which would be best replaced by associated constants. While it would be possible to switch from associated constants to const generic parameters, this has all the drawbacks of using explicit generic parameters versus families of them expressed as associated types. It becomes quite unwieldy when there are several of them involved. Our use cases benefit from these constants being "carried along" with another generic parameter, so it would be best to wait until this feature is implemented.

The second issue (76560) is important because we do quite a bit of arithmetic with typenum.

It will probably be a year or two before these features are implemented upstream, at which point we can consider a wholesale migration to const generics.

tarcieri avatar Mar 27 '22 14:03 tarcieri

Something I've been experimenting with is flex-array, a crate which is a hybrid of const generics and typenum-based arrays, which would make it possible to start using const generics in some capacities but still retain the full expressiveness of typenum along with the ability to define array sizes stored as associated types ala generic-array's ArrayLength:

https://github.com/RustCrypto/utils/pull/707

I started by trying to modify generic-array then quickly noticed that the approach would be a lot more effective if such a crate were built on const generics from the ground up.

I think a crate like this could provide a useful stepping stone for incrementally migrating to const generics.

tarcieri avatar Mar 27 '22 14:03 tarcieri

There's been some talk about potentially stabilizing associated constant support as the next step for const generic stabilization: https://github.com/rust-lang/rust/issues/76560#issuecomment-1100496080

tarcieri avatar Apr 16 '22 21:04 tarcieri

I've published a crate named hybrid-array which aims to provide the same functionality as generic-array, but in a way that can be composed with const generics: https://github.com/RustCrypto/utils/tree/master/hybrid-array

It can hopefully provide a stepping stone from where we're at now towards a full migration to const generics. Ideally this would include being able to use const generic core arrays in public-facing APIs.

tarcieri avatar May 12 '22 15:05 tarcieri

We've migrated to hybrid-array in most of the crates in this repo now, although the work still needs to be done to update the various implementations.

tarcieri avatar Nov 14 '23 14:11 tarcieri

Closing this out in favor of #1481, now that the migration from generic-array to hybrid-array is largely complete

tarcieri avatar Jan 21 '24 21:01 tarcieri