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

#[derive(new)] on non-pub struct should give fn new the right visibility (required for 2018 idioms) #40

Open
wycats opened this issue Jun 20, 2018 · 3 comments

Comments

@wycats
Copy link

wycats commented Jun 20, 2018

Today, #[derive(new)] always creates a pub fn, regardless of the visibility of the struct it's applied to.

In Rust 2018, that becomes a lint error, which makes it (at present) difficult to use this crate in a crate with #[warn(rust_2018_idioms)].

Instead, the visibility of the new method should be the same as the visibility of the struct.

It might also be useful to allow the visibility to be controlled directly. Straw man:

#[derive(new)]
#[new(visibility = crate)]
struct Point {
    x: u32,
    y: u32,
}
@Boscop
Copy link

Boscop commented Oct 3, 2019

Yes, please allow making new private even for public structs/enums.
In several of my use cases I only intend to call the derived new internally, and I get this error:

error: private type in public interface. can't leak private type

Because the derived new takes as arg a private type, to initialize a private member.

@JarrettBillingsley
Copy link

I've run into this, and now (since this is a relatively recent Rust feature) with wanting a const fn new.

@wycats
Copy link
Author

wycats commented Dec 9, 2020

I still think this is a good idea, even though the original annoying issue is no longer the root cause of the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants