-
-
Notifications
You must be signed in to change notification settings - Fork 530
Integrate min-const-generics. #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
2e47518 to
3d0f68f
Compare
3d0f68f to
cc4427e
Compare
|
I believe I have done everything I can think of regarding the integration of const-generics to nalgebra.
Because Rust doesn't allow to write a type parameter to be either a const or a type, I had to make some changes to our aliases. In particular, we have:
The same applies to vectors: I renamed the scalar type parameter from |
| use std::ops; | ||
|
|
||
| // N.B.: Not a public trait! | ||
| // T.B.: Not a public trait! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I don't think this N was supposed to be renamed to T!
Fix #852, fix #858, fix #621, fix #590, fix #526, fix #373
This PR isn't intended to be merged right now. It won't be merged before
min-const-genericsis stabilized. Though I wanted to submit this to show thatmin-const-genericsis powerful enough to let us replacegeneric-arrayby a regular array based on const-generics.This PR does not do everything we can change when min-const-generics will stabilize, but it contains all the changes needed to make everything work. More changes can be made to make the API a bit nicer though (e.g. to allow the user to write
Point<f32, 10>instead ofPoint<f32, Const<10>>for example).The idea here is to replace the typenum numbers (and our own number types
U1, U2, etc.) by a new struct wrapping an integer:struct Const<const T: usize>, and define an array storage like this:Then we have to adapt all the relevant trait implementations and voilà!
Limitation
There is one limitation here. Because
min-const-genericsdoes not allow operations on the const parameter, some method modifying the dimension of a matrix won't work. For exampleVector<N, Const<2>>::pushshould return aVector<N, Const<3>>, yet we can't write something like this:because the
{R + 1}isn't allowed yet. So we still need to usetypenumas a workaround here. Basically, we have two traitsToTypenumandToConstwhich we use to mapConst<1>totypenum::U1,Const<2>totypenum::U2, etc. That way, we can type ourpushfunction like this:The code I use in
nalgebrafor this is actually much nicer because all the gory details can be hidden behind ourDimSum,DimSub, etc. traits (which already existed before this PR).So the limitation here is that methods like
pushthat do some operations on the dimension of the input matrix will only work with matrices having dimensions with a type implementingToTypenum. However we can only implement ourToTypenumandToConsttraits for a finite number of types. In this PR I implementedToTypenumforConst<1>toConst<128>. We may add more if someone needs it. So right now methods likepushwill only work on statically-sized matrices with dimensions smaller than 128 (but it will continue to work on all dynamically-sized matrices). Methods that don't rely on this kind of type-level dimension addition or subtraction, will work with all dimensions (including these that don't implementToTypenum).