Skip to content
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

const fn constructors #521

Open
whatisaphone opened this issue Jan 6, 2019 · 8 comments
Open

const fn constructors #521

whatisaphone opened this issue Jan 6, 2019 · 8 comments
Labels
enhancement good first issue Good first issue for newcomers. P-high High priority

Comments

@whatisaphone
Copy link

const fn has landed in Rust stable! It would be really helpful in one of my projects if nalgebra adopted it for constructors. The shortlist of types I need is Point2, Point3, Vector2, Vector3, UnitComplex, UnitQuaternion. But it probably makes sense for every type in the library.

Things like UnitComplex::new might not work due to compile-time restrictions on FP math, but I'm happy to work around that with UnitComplex::from_cos_sin_unchecked or whatever else makes sense.

Adding the keyword is a simple enough change (at least I hope!), but it will break on older compilers and I don't know your policy on Rust version compatibility.

@jswrenn
Copy link
Contributor

jswrenn commented Jan 7, 2019

I don't believe this is possible quite yet! The current level of const_fn support doesn't include functions with trait bounds. Not only do we need this feature to mark constructors as const in nalgebra (because of the pervasive N: Scalar, R: Dim, C: Dim, S: Storage bounds), but we need this feature to make the constructors in generic-array const, too.

@whatisaphone
Copy link
Author

Aw, you're right :(

I guess this becomes the tracking issue then.

In the meantime I just learned about the union transmute hack, so that's the alternative for now.

related rust-lang/rust#24111

@sebcrozet
Copy link
Member

Revisiting this issue to see if the situation improved since last year. It appears that const fn on functions with trait bounds is still a problem:

error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
    --> src/base/construction.rs:842:14
     |
842  |           impl<N> MatrixMN<N, $R, $C>
     |                ^
...

@clarfonthey
Copy link

Side note, it would be nice if we could have a feature to enable these on nightly even though it'd be a mega pain to get working with macros.

@lylythechosenone
Copy link

lylythechosenone commented May 16, 2024

I'm pretty sure trait bounds on const fns are now stable. It would be nice if we could revisit this.

Edit: confirmed stable since 1.61.0, just about two years ago.

@Ralith
Copy link
Collaborator

Ralith commented May 16, 2024

Calling trait methods in const isn't, so I'm not sure you'll be able to constify any useful constructors.

@Andlon
Copy link
Collaborator

Andlon commented May 16, 2024

The point!, vector! and matrix! construction macros are all usable in a const context, allowing you to construct const vectors, matrices and points.

The Unit-related constructors are probably currently not possible to support in const though, since AFAIK you'll need methods from SimdRealField or similar traits.

@lylythechosenone
Copy link

Ah, in that case this is not supported yet. Indeed, you still cannot call trait methods in const (coming soon, hopefully).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Good first issue for newcomers. P-high High priority
Projects
None yet
Development

No branches or pull requests

7 participants