Skip to content

Tweak Request: Add identical enum variants as associated constants #1353

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

Closed
Lytigas opened this issue Jul 29, 2018 · 2 comments
Closed

Tweak Request: Add identical enum variants as associated constants #1353

Lytigas opened this issue Jul 29, 2018 · 2 comments

Comments

@Lytigas
Copy link
Contributor

Lytigas commented Jul 29, 2018

I think it would make more sense to expose duplicate variants as associated constants rather than externally. Could be disabled by default for backwards compatibility.

#include <stdint.h>
enum Opinion : int32_t {
	BAD = 0,
	BEST = 1,
	BESTEST = 1,
	WORST = -2,
	WORSTER = -2,
};
$ bindgen test.hpp --whitelist-type "Opinion" --rustified-enum ".*" -- -std=c++14 > bind.rs

yields

pub type __int32_t = ::std::os::raw::c_int;
pub const Opinion_BESTEST: Opinion = Opinion::BEST;
pub const Opinion_WORSTER: Opinion = Opinion::WORST;
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Opinion {
    BAD = 0,
    BEST = 1,
    WORST = -2,
}

Requesting something like:

impl Opinion {
    pub const BESTEST: Opinion = Opinion::BEST;
    pub const WORSTER: Opinion = Opinion::WORST;
}

I'd be happy to try myself if someone could point me the right direction.

@Lytigas Lytigas changed the title Tweak Request: Add enum variants with the same value as associated constants Tweak Request: Add identical enum variants as associated constants Jul 29, 2018
@emilio
Copy link
Contributor

emilio commented Jul 29, 2018

This behavior looks strictly better to me so I'd be fine making it the default. Not sure since when it's available, but presumably we'd need to gate it with a rust feature gate, see features.rs for that.

Other than that, this should be somewhat straight-forward to implement.

The point where we handle the duplicated values for rust enums is:

https://github.com/rust-lang-nursery/rust-bindgen/blob/d61ab759e3512d79131eb6c455862915c6b6c4d2/src/codegen/mod.rs#L2633-L2657

We should presumably change add_constant to emit something like:

impl Opinion {
     pub const BESTEST: Opinion = Opinion::BEST;
}

if supported instead. It's probably easier to emit multiple impl blocks rather than coalescing them, even though the generated code won't be so pretty :)

@Lytigas
Copy link
Contributor Author

Lytigas commented Jul 30, 2018

Fixed by #1355

@Lytigas Lytigas closed this as completed Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants