switch to use builtin bitflags!

This commit is contained in:
Andelf
2014-05-15 23:25:47 +08:00
parent 27b51f192f
commit 847f349da1
2 changed files with 9 additions and 117 deletions
-109
View File
@@ -1,109 +0,0 @@
// Loosely based on zlib-licensed code from RustAllegro
// https://github.com/SiegeLord/RustAllegro
//
// Implements efficient, type-safe flags with support for bitwise operators.
//
// Usage:
//
// flag_type!(FlagTypeName {
// FlagName1 = FlagValue1,
// FlagName2 = FlagValue2,
// ...
// FlagNameN = FlagValueN
// })
//
// fn foo(flag: FlagTypeName) {
// let raw = (flag | FlagNameN).get();
// bar(raw);
// }
#![macro_escape]
macro_rules! flag_type(
($typename:ident : $supertype:ident { $($name:ident = $value:expr),* }) => {
pub struct $typename {
bits: $supertype
}
impl $typename {
#[inline]
pub fn new(bits: $supertype) -> $typename {
$typename { bits: bits }
}
#[inline]
pub fn get(self) -> $supertype {
self.bits
}
}
impl ::std::default::Default for $typename {
fn default() -> $typename {
$typename::new(0)
}
}
impl ::std::cmp::Eq for $typename {
fn eq(&self, other: &$typename) -> bool {
self.bits == other.bits
}
}
impl ::std::cmp::TotalEq for $typename {}
impl ::std::cmp::Ord for $typename {
fn lt(&self, other: &$typename) -> bool {
self.bits < other.bits
}
}
impl ::std::cmp::TotalOrd for $typename {
fn cmp(&self, other: &$typename) -> Ordering {
self.bits.cmp(&other.bits)
}
}
impl ::std::ops::Not<$typename> for $typename {
fn not(&self) -> $typename {
$typename { bits: !self.bits }
}
}
impl ::std::ops::BitAnd<$typename, $typename> for $typename {
fn bitand(&self, rhs: &$typename) -> $typename {
$typename { bits: self.bits & rhs.bits }
}
}
impl ::std::ops::BitOr<$typename, $typename> for $typename {
fn bitor(&self, rhs: &$typename) -> $typename {
$typename { bits: self.bits | rhs.bits }
}
}
impl ::std::ops::BitXor<$typename, $typename> for $typename {
fn bitxor(&self, rhs: &$typename) -> $typename {
$typename { bits: self.bits ^ rhs.bits }
}
}
impl ::std::ops::Shl<$supertype, $typename> for $typename {
fn shl(&self, rhs: &$supertype) -> $typename {
$typename { bits: self.bits << *rhs }
}
}
impl ::std::ops::Shr<$supertype, $typename> for $typename {
fn shr(&self, rhs: &$supertype) -> $typename {
$typename { bits: self.bits >> *rhs }
}
}
$(
pub static $name: $typename = $typename { bits: $value as $supertype };
)+
};
($typename:ident { $($name:ident = $value:expr),* }) => {
flag_type!($typename : u32 { $($name = $value),* })
}
)
+9 -8
View File
@@ -41,15 +41,14 @@ mod others {
#[allow(non_camel_case_types, dead_code)]
mod ffi;
mod flag;
/// InitFlags are passed to init() to control which subsystem
/// functionality to load.
flag_type!(InitFlag : c_int {
InitJpg = ffi::IMG_INIT_JPG,
InitPng = ffi::IMG_INIT_PNG,
InitTif = ffi::IMG_INIT_TIF,
InitWebp = ffi::IMG_INIT_WEBP
bitflags!(flags InitFlag : u32 {
static InitJpg = ffi::IMG_INIT_JPG as u32,
static InitPng = ffi::IMG_INIT_PNG as u32,
static InitTif = ffi::IMG_INIT_TIF as u32,
static InitWebp = ffi::IMG_INIT_WEBP as u32
})
/// Static method extensions for creating Surfaces
@@ -144,8 +143,10 @@ impl LoadTexture for Renderer {
pub fn init(flags: InitFlag) -> InitFlag {
//! Initializes SDL2_image with InitFlags and returns which
//! InitFlags were actually used.
let used = unsafe { ffi::IMG_Init(flags.get()) };
InitFlag::new(used)
unsafe {
let used = ffi::IMG_Init(flags.bits() as c_int);
InitFlag::from_bits(used as u32)
}
}
pub fn quit() {