Tenders McChiken
Tenders McChiken

Reputation: 1543

What is the idiomatic way to implement a trait for a large number of arrays in stable Rust?

Given my trait T and a large set of std/core arrays (not slices), How can I make implementations of T for these arrays available to other crates on stable Rust?

From searching around, it seems my only options (that don't sidestep this problem) are to:

  1. wait for a release of rust with a stable implementation of rfc 2000: https://github.com/rust-lang/rust/issues/44580
  2. implement the trait manually for each and every array.

Option 1 is not acceptable. Option 2 leads to very long compile times (especially when the set of arrays exceeds 5000 types). Hiding every single implementation behind its own feature, i.e. feature impl-t-for-array-N conditionally compiles in an implementation of T for array [U;N], does considerably lower compilation time. (Compilation times went from from tens of minutes to a couple of seconds). However, the delay caused by processing thousands of features is still noticeable.

Is using features and manual implementations the most idiomatic way to handle this problem on stable rust, or is there a more idiomatic way I'm missing?

Upvotes: 1

Views: 309

Answers (1)

Laney
Laney

Reputation: 1649

Is using features and manual implementations the most idiomatic way to handle this problem on stable rust

Yes. For example, check Debug trait and its sources - as you can see rust uses special macro to implement it for arrays with length <= 32

Upvotes: 1

Related Questions