rmk icon indicating copy to clipboard operation
rmk copied to clipboard

Initial migration from const-generics to typenum

Open Ben-PH opened this issue 1 year ago • 6 comments

This is an initial PoC of what a migration to use of typenum and GenericArray might look like.

Ben-PH avatar Dec 15 '24 17:12 Ben-PH

(Not the developer, just curious)

Could you expand on what the motivation for moving away from const generics is here?

thomasaarholt avatar Dec 15 '24 17:12 thomasaarholt

typenum allows you to do things like specify legal values at compile time, as well as removes the need to choose an integer type until runtime.

For example, if you wanted to implement a "sub-matrix" extension trait, you could constrain it so its width and height is less than the matrix it's extending. when you are placing it in the "super-matrix", you can constrain it so that it's left most column, plus its width is less than the width of the super-matrix, and similarly for the highest row.

this is all at compile time.

as for integer types, you do not need to choose usize, u32, etc., until an actual machine-representation of the integer is required.

Ben-PH avatar Dec 15 '24 20:12 Ben-PH

Thank you for the explanation!

thomasaarholt avatar Dec 15 '24 20:12 thomasaarholt

Another way to put it:

const generics allow you to specify "use u32/usize/u64/etc here", typenum says "use any integer value here", and you can specify what "legal" is (non-zero, even/odd, the result of arithmetic, etc)

oh yeah, that's the other thing: you can do arithmetic with typenum. no need for SIZE because you can just derive that within the type system my multiplying the width-type by the height-type.

Ben-PH avatar Dec 15 '24 20:12 Ben-PH

I've looked through the PR, afaict, typenum brings us the compile-time checking and calculation features, which could be very useful. But now I tend to not use it because:

  1. The last release of typenum was over 1 year ago, I'm not sure whether it's been actively maintained.
  2. The compile-time checking part can be done in proc-macro, by checking values set in keyboard.toml. We've already had some, like: https://github.com/HaoboGu/rmk/blob/6ea066a128b0f1d04c5ab383ba78a98137fa6556/rmk-macro/src/keyboard_config.rs#L372-L386
  3. The compile-time calculation part could be covered by https://github.com/rust-lang/rust/issues/76560. typenum has a long history, it appeared even before const_generics. But for the future, I prefer to use features provided by official Rust, not a third-party crate.

HaoboGu avatar Dec 16 '24 02:12 HaoboGu

After our discussion in DMs, I'd be happy to re-write the PR so that it only affects the internal code-base, and not the public facing API.

Ben-PH avatar Dec 16 '24 13:12 Ben-PH

Closing this due to no activity. If you still want to work on it, please feel free to reopen it!

HaoboGu avatar May 20 '25 12:05 HaoboGu