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

Constructor generated new should be marked const #131

Closed
KarelPeeters opened this issue Jun 14, 2020 · 4 comments · Fixed by #218
Closed

Constructor generated new should be marked const #131

KarelPeeters opened this issue Jun 14, 2020 · 4 comments · Fixed by #218
Milestone

Comments

@KarelPeeters
Copy link

KarelPeeters commented Jun 14, 2020

For example, given this struct:

#[derive(Constructor)]
struct Foo { x: f32, y: f32 }

The generated code should be:

impl Foo {
    const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }
}

As far as I know this should be fine to do on the current stable. It's an easy change, would a PR adding this be accepted?

@JelteF
Copy link
Owner

JelteF commented Jun 20, 2020

A pull request would be accepted for this assuming it doesn't break Rust 1.36.0 (which is the currently lowest supported Rust). To do so you might have to check the version with rustc_version in the build.rs script.

@KarelPeeters
Copy link
Author

Unfortunately const fn is not quite as stable as I thought, for example trait bounds aren't yet supported in stable (rust-lang/rfcs#2632). I think this issue will have to wait until const fn is more mature.

@krojew
Copy link

krojew commented Mar 15, 2022

Any plans for this? Having const constructors would be a nice feature.

@appetrosyan
Copy link

One option for how it could be rendered is if we add #[CosntConstructor] to the derive macros. Then if the const-ness is not supported you have alternatives. No need for complex logic, just a conscious choice.

tyranron added a commit that referenced this issue Jan 31, 2023
@tyranron tyranron added this to the 1.0.0 milestone Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants