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

Make Debug fancy again and handle #[cfg] properly. #14

Merged
merged 1 commit into from
Jun 29, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
/// flags.clear();
/// assert!(flags.is_empty());
/// assert_eq!(format!("{}", flags), "hi!");
/// assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "Flags { bits: 0b11 }");
/// assert_eq!(format!("{:?}", FLAG_B), "Flags { bits: 0b10 }");
/// assert_eq!(format!("{:?}", FLAG_A | FLAG_B), "FLAG_A | FLAG_B");
/// assert_eq!(format!("{:?}", FLAG_B), "FLAG_B");
/// }
/// ```
///
Expand Down Expand Up @@ -144,10 +144,35 @@ macro_rules! bitflags {

impl ::std::fmt::Debug for $BitFlags {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let out = format!("{} {{ bits: {:#b} }}",
stringify!($BitFlags),
self.bits);
f.write_str(&out[..])
// This convoluted approach is to handle #[cfg]-based flag omission correctly.
mod dummy {
// Now we define the "undefined" versions of the flags.
// This way, all the names exist, even if some are #[cfg]ed out.
$(const $Flag: super::$BitFlags = super::$BitFlags { bits: 0 };)+

#[inline]
pub fn fmt(self_: &super::$BitFlags, f: &mut ::std::fmt::Formatter)
-> ::std::fmt::Result {
// Now we import the real values for the flags.
// Only ones that are #[cfg]ed out will be 0.
use super::*;

let mut first = true;
$(
// $Flag.bits == 0 means that $Flag doesn't exist
if $Flag.bits != 0 && self_.contains($Flag) {
if !first {
try!(f.write_str(" | "));
}
first = false;
try!(f.write_str(stringify!($Flag)));
}
)+
let _ = first; // Silence the unused_assignments warning
Ok(())
}
}
dummy::fmt(self, f)
}
}

Expand All @@ -161,7 +186,22 @@ macro_rules! bitflags {
/// Returns the set containing all flags.
#[inline]
pub fn all() -> $BitFlags {
$BitFlags { bits: $($value)|+ }
// This convoluted approach is to handle #[cfg]-based flag omission correctly.
mod dummy {
// Now we define the "undefined" versions of the flags.
// This way, all the names exist, even if some are #[cfg]ed out.
$(const $Flag: super::$BitFlags = super::$BitFlags { bits: 0 };)+

#[inline]
pub fn all() -> super::$BitFlags {
// Now we import the real values for the flags.
// Only ones that are #[cfg]ed out will be 0.
use super::*;

$BitFlags { bits: $($Flag.bits)|+ }
}
}
dummy::all()
}

/// Returns the raw value of the flags currently stored.
Expand Down Expand Up @@ -333,6 +373,8 @@ mod tests {
const _CfgA = 0b01,
#[cfg(unix)]
const _CfgB = 0b01,
#[cfg(windows)]
const _CfgC = _CfgA.bits | 0b10,
}
}

Expand Down Expand Up @@ -529,7 +571,7 @@ mod tests {

#[test]
fn test_debug() {
assert_eq!(format!("{:?}", FlagA | FlagB), "Flags { bits: 0b11 }");
assert_eq!(format!("{:?}", FlagABC), "Flags { bits: 0b111 }");
assert_eq!(format!("{:?}", FlagA | FlagB), "FlagA | FlagB");
assert_eq!(format!("{:?}", FlagABC), "FlagA | FlagB | FlagC | FlagABC");
}
}