Skip to content

Commit

Permalink
Merge pull request #61 from dtolnay/conversions
Browse files Browse the repository at this point in the history
Remove i128 feature and conversions to u64
  • Loading branch information
alexcrichton committed Mar 7, 2017
2 parents 3b487dd + 619af81 commit b486cd6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ after_success:
- travis-cargo --only nightly doc-upload
env:
global:
secure: "DoZ8g8iPs+X3xEEucke0Ae02JbkQ1qd1SSv/L2aQqxULmREtRcbzRauhiT+ToQO5Ft1Lul8uck14nPfs4gMr/O3jFFBhEBVpSlbkJx7eNL3kwUdp95UNroA8I43xPN/nccJaHDN6TMTD3+uajTQTje2SyzOQP+1gvdKg17kguvE="
- TRAVIS_CARGO_NIGHTLY_FEATURE=unstable_testing
- secure: "DoZ8g8iPs+X3xEEucke0Ae02JbkQ1qd1SSv/L2aQqxULmREtRcbzRauhiT+ToQO5Ft1Lul8uck14nPfs4gMr/O3jFFBhEBVpSlbkJx7eNL3kwUdp95UNroA8I43xPN/nccJaHDN6TMTD3+uajTQTje2SyzOQP+1gvdKg17kguvE="



Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ A macro to generate structures which behave like bitflags.
"""

[features]
i128 = []
unstable = ["i128"]
unstable_testing = []
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ extern crate bitflags;

## 128-bit integer bitflags (nightly only)

Add this to your `Cargo.toml`:

```toml
[dependencies.bitflags]
version = "0.7"
features = ["i128"]
```

and this to your crate root:
`u128` and `i128` are supported just like any other integer type.

```rust
#![feature(i128_type)]

#[macro_use]
extern crate bitflags;

bitflags! {
flags Flags128: u128 {
const A = 0x0000_0000_0000_0000_0000_0000_0000_0001,
const B = 0x0000_0000_0000_1000_0000_0000_0000_0000,
const C = 0x8000_0000_0000_0000_0000_0000_0000_0000,
const ABC = A.bits | B.bits | C.bits,
}
}
```
60 changes: 32 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#![no_std]

#![cfg_attr(feature = "i128", feature(i128_type))]

#[cfg(test)]
#[macro_use]
extern crate std;
Expand All @@ -24,11 +22,6 @@ extern crate std;
#[doc(hidden)]
pub use core as __core;

#[cfg(feature = "i128")]
pub type __BitFlagsWidth = u128;
#[cfg(not(feature = "i128"))]
pub type __BitFlagsWidth = u64;

/// The `bitflags!` macro generates a `struct` that holds a set of C-style
/// bitmask flags. It is useful for creating typesafe wrappers for C APIs.
///
Expand Down Expand Up @@ -237,22 +230,28 @@ macro_rules! bitflags {
// We can't use the real $BitFlags struct because it may be
// private, which prevents us from using it to define
// public constants.
pub struct $BitFlags {
bits: $crate::__BitFlagsWidth,
pub struct __Pub(super::$BitFlags);
impl $crate::__core::convert::From<super::$BitFlags> for __Pub {
fn from(original: super::$BitFlags) -> Self {
__Pub(original)
}
}
mod real_flags {
use super::$BitFlags;
$($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags {
bits: super::super::$Flag.bits as $crate::__BitFlagsWidth
};)+
use super::__Pub;
$(
$(#[$Flag_attr])*
pub const $Flag: __Pub = __Pub(super::super::$Flag);
)+
}
// 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 };)+
$(
const $Flag: __Pub = __Pub(super::$BitFlags { bits: 0 });
)+

#[inline]
pub fn fmt(self_: $crate::__BitFlagsWidth,
pub fn fmt(self_: __Pub,
f: &mut $crate::__core::fmt::Formatter)
-> $crate::__core::fmt::Result {
// Now we import the real values for the flags.
Expand All @@ -262,8 +261,7 @@ macro_rules! bitflags {
let mut first = true;
$(
// $Flag.bits == 0 means that $Flag doesn't exist
if $Flag.bits != 0 && self_ & $Flag.bits as $crate::__BitFlagsWidth ==
$Flag.bits as $crate::__BitFlagsWidth {
if $Flag.0.bits != 0 && self_.0.bits & $Flag.0.bits == $Flag.0.bits {
if !first {
try!(f.write_str(" | "));
}
Expand All @@ -274,7 +272,7 @@ macro_rules! bitflags {
Ok(())
}
}
dummy::fmt(self.bits as $crate::__BitFlagsWidth, f)
dummy::fmt($crate::__core::convert::From::from(*self), f)
}
}

Expand All @@ -292,24 +290,30 @@ macro_rules! bitflags {
// See above `dummy` module for why this approach is taken.
#[allow(dead_code)]
mod dummy {
pub struct $BitFlags {
bits: $crate::__BitFlagsWidth,
pub struct __Pub(super::$BitFlags);
impl $crate::__core::convert::From<__Pub> for super::$BitFlags {
fn from(wrapper: __Pub) -> Self {
wrapper.0
}
}
mod real_flags {
use super::$BitFlags;
$($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags {
bits: super::super::$Flag.bits as $crate::__BitFlagsWidth
};)+
use super::__Pub;
$(
$(#[$Flag_attr])*
pub const $Flag: __Pub = __Pub(super::super::$Flag);
)+
}
$(const $Flag: $BitFlags = $BitFlags { bits: 0 };)+
$(
const $Flag: __Pub = __Pub(super::$BitFlags { bits: 0 });
)+

#[inline]
pub fn all() -> $crate::__BitFlagsWidth {
pub fn all() -> __Pub {
use self::real_flags::*;
$($Flag.bits)|+
__Pub(super::$BitFlags { bits: $($Flag.0.bits)|+ })
}
}
$BitFlags { bits: dummy::all() as $T }
$crate::__core::convert::From::from(dummy::all())
}

/// Returns the raw value of the flags currently stored.
Expand Down
6 changes: 3 additions & 3 deletions tests/i128_bitflags.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![cfg(feature = "unstable_testing")]

#![allow(dead_code, unused_imports)]
#![cfg_attr(feature = "i128", feature(i128_type))]
#![feature(i128_type)]

#[macro_use]
extern crate bitflags;

#[cfg(feature = "i128")]
bitflags! {
/// baz
flags Flags128: u128 {
Expand All @@ -15,7 +16,6 @@ bitflags! {
}
}

#[cfg(feature = "i128")]
#[test]
fn test_i128_bitflags() {
assert_eq!(ABC, A | B | C);
Expand Down

0 comments on commit b486cd6

Please sign in to comment.