Skip to content

associated constants #21

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
tamird opened this issue Nov 1, 2015 · 8 comments
Closed

associated constants #21

tamird opened this issue Nov 1, 2015 · 8 comments

Comments

@tamird
Copy link
Contributor

tamird commented Nov 1, 2015

This crate really wants to use associated constants (see rust-lang/rust#24921) but the tricks introduced in #14 are incompatible with associated constants (because associated constants can't be used to shadow locally scoped constants).

What do?

@Amanieu
Copy link
Contributor

Amanieu commented Nov 28, 2015

Here is an implementation which does the same thing using a dummy trait:

impl ::std::fmt::Debug for $BitFlags {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        // This convoluted approach is to handle #[cfg]-based flag
        // omission correctly. Some of the $Flag variants may not be
        // defined in the main struct so we create a trait which defines
        // *all* flags to the value of 0. Afterwards we define a struct
        // which overrides the definitions in the trait for all defined
        // variants, leaving only the undefined ones with the bit value
        // of 0.
        trait DummyTrait {
            // Now we define the "undefined" versions of the flags.
            // This way, all the names exist, even if some are #[cfg]ed
            // out.
            $(const $Flag: $BitFlags = $BitFlags { bits: 0 };)+
        };
        struct DummyStruct;
        impl DummyTrait for DummyStruct {
            // Then we override the "undefined" flags with flags that
            // have their proper value. Flags that are #[cfg]ed out
            // retain the default value of 0 defined by the trait.
            $($(#[$Flag_attr])* const $Flag: $BitFlags = $BitFlags { bits: $value };)+
        };
        let mut _first = true;
        $(
            // $Flag.bits == 0 means that $Flag doesn't exist
            if DummyStruct::$Flag.bits != 0 && self.contains(DummyStruct::$Flag) {
                if !_first {
                    try!(f.write_str(" | "));
                }
                _first = false;
                try!(f.write_str(stringify!($Flag)));
            }
        )+
        Ok(())
    }
}

One open question is whether Debug should print Flags::FLAG_A | Flags::FLAB_B or just FLAG_A | FLAG_B as it currently does.

@Ericson2314
Copy link
Contributor

@tamird are you going to make a PR? Or does the conflict in the OP still stand?

Ericson2314 pushed a commit to QuiltOS/bitflags that referenced this issue Apr 9, 2016
@Amanieu
Copy link
Contributor

Amanieu commented Apr 9, 2016

There is already a PR (#24), however it is blocked until associated constants are stable.

@Ericson2314
Copy link
Contributor

Ah thanks

msanders added a commit to msanders/core-graphics-rs that referenced this issue Feb 23, 2017
Necessary until bitflags/bitflags#21
is implemented.
msanders added a commit to msanders/core-graphics-rs that referenced this issue Feb 23, 2017
Necessary until bitflags/bitflags/issues/21 is implemented.
@crumblingstatue
Copy link
Contributor

Associated consts have been stabilized.
They will be stable in 1.20.

@Ericson2314
Copy link
Contributor

Time to clean up #21 ?

@aep
Copy link

aep commented Sep 8, 2017

can we get a release with the new associated constants? :)

@alexcrichton
Copy link
Contributor

published!

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

6 participants