-
Notifications
You must be signed in to change notification settings - Fork 150
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
Comments
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 |
@tamird are you going to make a PR? Or does the conflict in the OP still stand? |
Fixes bitflags#20. Closes bitflags#21.
There is already a PR (#24), however it is blocked until associated constants are stable. |
Ah thanks |
Necessary until bitflags/bitflags#21 is implemented.
Necessary until bitflags/bitflags/issues/21 is implemented.
Associated consts have been stabilized. |
Time to clean up #21 ? |
can we get a release with the new associated constants? :) |
published! |
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
use
d to shadow locally scoped constants).What do?
The text was updated successfully, but these errors were encountered: