derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Constructor generated new should be marked const

Open KarelPeeters opened this issue 5 years ago • 4 comments
trafficstars

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?

KarelPeeters avatar Jun 14 '20 23:06 KarelPeeters

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.

JelteF avatar Jun 20 '20 21:06 JelteF

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

KarelPeeters avatar Jun 30 '20 12:06 KarelPeeters

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

krojew avatar Mar 15 '22 05:03 krojew

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.

appetrosyan avatar May 28 '22 08:05 appetrosyan