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

Fix for #39 #51

Merged
merged 2 commits into from
May 15, 2016
Merged

Fix for #39 #51

merged 2 commits into from
May 15, 2016

Conversation

dimbleby
Copy link
Contributor

Avoid definitions in inner modules being dependent on things that may
only be in scope in outer modules.

@dimbleby
Copy link
Contributor Author

I'm not particularly attached to this if it doesn't work for some cases; but it does the job for me.

Avoid definitions in inner modules being dependent on things that may
only be in scope in outer modules.
@nox
Copy link
Contributor

nox commented May 13, 2016

Interesting fix! Will try in cocoa-rs.

@nox
Copy link
Contributor

nox commented May 13, 2016

@dimbleby That fixes the values, but not the type of the enum. This additional patch fixes the types:

diff --git i/src/lib.rs w/src/lib.rs
index d687d3f..5782cc7 100644
--- i/src/lib.rs
+++ w/src/lib.rs
@@ -231,11 +231,13 @@ macro_rules! bitflags {
                     // private, which prevents us from using it to define
                     // public constants.
                     pub struct $BitFlags {
-                        bits: $T,
+                        bits: usize,
                     }
                     mod real_flags {
                         use super::$BitFlags;
-                        $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: super::super::$Flag.bits };)+
+                        $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags {
+                            bits: super::super::$Flag.bits as usize
+                        };)+
                     }
                     // Now we define the "undefined" versions of the flags.
                     // This way, all the names exist, even if some are #[cfg]ed
@@ -243,7 +245,7 @@ macro_rules! bitflags {
                     $(const $Flag: $BitFlags = $BitFlags { bits: 0 };)+

                     #[inline]
-                    pub fn fmt(self_: $T,
+                    pub fn fmt(self_: usize,
                                f: &mut $crate::__core::fmt::Formatter)
                                -> $crate::__core::fmt::Result {
                         // Now we import the real values for the flags.
@@ -253,7 +255,7 @@ macro_rules! bitflags {
                         let mut first = true;
                         $(
                             // $Flag.bits == 0 means that $Flag doesn't exist
-                            if $Flag.bits != 0 && self_ & $Flag.bits == $Flag.bits {
+                            if $Flag.bits != 0 && self_ & $Flag.bits as usize == $Flag.bits as usize {
                                 if !first {
                                     try!(f.write_str(" | "));
                                 }
@@ -264,7 +266,7 @@ macro_rules! bitflags {
                         Ok(())
                     }
                 }
-                dummy::fmt(self.bits, f)
+                dummy::fmt(self.bits as usize, f)
             }
         }

@@ -283,21 +285,23 @@ macro_rules! bitflags {
                 #[allow(dead_code)]
                 mod dummy {
                     pub struct $BitFlags {
-                        bits: $T,
+                        bits: usize,
                     }
                     mod real_flags {
                         use super::$BitFlags;
-                        $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: super::super::$Flag.bits };)+
+                        $($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags {
+                            bits: super::super::$Flag.bits as usize
+                        };)+
                     }
                     $(const $Flag: $BitFlags = $BitFlags { bits: 0 };)+

                     #[inline]
-                    pub fn all() -> $T {
+                    pub fn all() -> usize {
                         use self::real_flags::*;
                         $($Flag.bits)|+
                     }
                 }
-                $BitFlags { bits: dummy::all() }
+                $BitFlags { bits: dummy::all() as $T }
             }

             /// Returns the raw value of the flags currently stored.

Feel free to amend your PR with this if you want.

@dimbleby
Copy link
Contributor Author

@nox - thanks! will do

@Amanieu
Copy link
Contributor

Amanieu commented May 14, 2016

Wouldn't it be better to use u64 instead of usize? The optimizer should be able to reduce it down to the proper size anyways.

@nox
Copy link
Contributor

nox commented May 14, 2016

@Amanieu True that! It should be u64 given you can do u64 bitflags on a 32 bits system. Nice catch.

Don't have inner modules use a type that may only be available in outer
modules.
@alexreg
Copy link

alexreg commented May 14, 2016

This is a useful PR. @alexcrichton Any chance of a merge and possible release? :)

@alexcrichton alexcrichton merged commit cff0fb8 into bitflags:master May 15, 2016
@alexcrichton
Copy link
Contributor

Thanks @dimbleby!

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

Successfully merging this pull request may close these issues.

None yet

5 participants