-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
Integrate min-const-generics. #820
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-generics
is stabilized. Though I wanted to submit this to show thatmin-const-generics
is powerful enough to let us replacegeneric-array
by 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-generics
does not allow operations on the const parameter, some method modifying the dimension of a matrix won't work. For exampleVector<N, Const<2>>::push
should 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 usetypenum
as a workaround here. Basically, we have two traitsToTypenum
andToConst
which we use to mapConst<1>
totypenum::U1
,Const<2>
totypenum::U2
, etc. That way, we can type ourpush
function like this:The code I use in
nalgebra
for 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
push
that 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 ourToTypenum
andToConst
traits for a finite number of types. In this PR I implementedToTypenum
forConst<1>
toConst<128>
. We may add more if someone needs it. So right now methods likepush
will 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
).