Fix for #39 #51

Merged
dimbleby merged 2 commits from issue-39 into master 2016-05-14 19:02:19 -05:00
dimbleby commented 2016-05-13 13:51:56 -05:00 (Migrated from github.com)

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

Avoid definitions in inner modules being dependent on things that may only be in scope in outer modules.
dimbleby commented 2016-05-13 13:54:32 -05:00 (Migrated from github.com)

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

I'm not particularly attached to this if it doesn't work for some cases; but it does the job for me.
nox commented 2016-05-13 14:48:19 -05:00 (Migrated from github.com)

Interesting fix! Will try in cocoa-rs.

Interesting fix! Will try in cocoa-rs.
nox commented 2016-05-13 15:04:36 -05:00 (Migrated from github.com)

@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 That fixes the values, but not the type of the enum. This additional patch fixes the types: ``` diff 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 commented 2016-05-13 15:58:34 -05:00 (Migrated from github.com)

@nox - thanks! will do

@nox - thanks! will do
Amanieu commented 2016-05-14 07:43:48 -05:00 (Migrated from github.com)

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.

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 commented 2016-05-14 07:44:29 -05:00 (Migrated from github.com)

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

@Amanieu True that! It should be `u64` given you can do `u64` bitflags on a 32 bits system. Nice catch.
alexreg commented 2016-05-14 15:13:28 -05:00 (Migrated from github.com)

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

This is a useful PR. @alexcrichton Any chance of a merge and possible release? :)
alexcrichton commented 2016-05-14 19:02:25 -05:00 (Migrated from github.com)

Thanks @dimbleby!

Thanks @dimbleby!
Sign in to join this conversation.
No reviewers
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags!51
No description provided.