Impl traits for specific array lengths above 32


#1

Its occurred to me that while not having impls for arbitrary array lengths is a problem, probably the practical use cases of arrays of length over 32 is not evenly distributed among all the possible lengths. That is, there is probably a small number of additional impls we could define which would unblock an outsized portion of users.

For example, we could implement traits for all arrays of a length that is an exponents of 2 up to 4096 (chosen because in bytes its the smallest page size on most systems). That is, for 64, 128, 256, 512, 1024, 2048, and 4096 - only 7 additional impls.

It might also be good to implement for the standard multiples of ten - 50, 100, 250, 500, 1000 (5 impls).

However, I never actually need large arrays, so I could be off base. Does anyone who runs into this problem have any feedback? Are there specific array lengths that could be targeted that you think would help many use cases?


#2

serde-rs/serde#573 wanted [T; 386]. The prime factors are 2 and 193. :confounded:

Anyway, this seems like a more reasonable version of what typenum is doing here which is:

  • Numbers 0 through 1024
  • Powers of 2 below u64::MAX
  • Powers of 10 below u64::MAX

@paholg any thoughts on how we could get the most bang for the buck here?


#3

Um, if you’re asking what values to pick, I dunno. For typenum, powers of 2 and 10 seemed useful, and 1000ish seemed like a good stopping place. For arrays, I would think the most useful values would be 33, 34, … counting up. Perhaps looking at what values people use for generic-array would give some insight.

If you’re asking how to generate them, typenum uses a build script, but that’s probably not useful here. I think generating the impls for an array is just a macro call with that number, which isn’t really possible for typenum.


#4

I’d imagine that lengths used commonly in cryptography would see some use. The current impls cover those up to 256 bits (32xu8), but array lengths of 48, 64, 128, 256, 384 and 512 would be nice to have.


#5

While supporting arrays for more sizes would be nice, it would also make them even weirder (users would be wondering why does it work for 64, but not 65 elements!?).

There’s work on fully generic constants in types, which would make it work for all array sizes:

https://github.com/rust-lang/rfcs/issues/1930


#6

I know :slight_smile: This is intended as a stopgap.


#7

Are const generics a replacement/generalization of pi types?

I’m surprised there’s talk about supporting expressions. For me, for an MVP, just bare integers would suffice.


#8

It’s not harder to support them (as long as we treat them as opaque), and we have to anyway for [T; expr].