Merge pull request #61 from dtolnay/conversions

Remove i128 feature and conversions to u64
This commit is contained in:
Alex Crichton 2017-03-07 09:04:49 -06:00 committed by GitHub
commit b486cd69db
5 changed files with 48 additions and 43 deletions

View File

@ -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="

View File

@ -13,5 +13,4 @@ A macro to generate structures which behave like bitflags.
"""
[features]
i128 = []
unstable = ["i128"]
unstable_testing = []

View File

@ -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,
}
}
```

View File

@ -12,8 +12,6 @@
#![no_std]
#![cfg_attr(feature = "i128", feature(i128_type))]
#[cfg(test)]
#[macro_use]
extern crate std;
@ -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.
///
@ -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.
@ -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(" | "));
}
@ -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)
}
}
@ -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.

View File

@ -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 {
@ -15,7 +16,6 @@ bitflags! {
}
}
#[cfg(feature = "i128")]
#[test]
fn test_i128_bitflags() {
assert_eq!(ABC, A | B | C);