Eq renames to PartialEq; use standard bitflag implementation

This commit is contained in:
Andelf
2014-05-31 22:52:12 +08:00
parent 376d3c0764
commit efeb4721e9
2 changed files with 14 additions and 123 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),* })
}
)
+14 -14
View File
@@ -47,7 +47,6 @@ mod others {
#[allow(non_camel_case_types, dead_code)]
mod ffi;
mod flag;
#[inline]
fn color_to_c_color(color: Color) -> SDL_Color {
@@ -58,16 +57,15 @@ fn color_to_c_color(color: Color) -> SDL_Color {
}
/// Font Style
#[deriving(Show)]
flag_type!(FontStyle : c_int {
StyleNormal = ffi::TTF_STYLE_NORMAL,
StyleBold = ffi::TTF_STYLE_BOLD,
StyleItalic = ffi::TTF_STYLE_ITALIC,
StyleUnderline = ffi::TTF_STYLE_UNDERLINE,
StyleStrikeThrough = ffi::TTF_STYLE_STRIKETHROUGH
bitflags!(flags FontStyle : c_int {
static StyleNormal = ffi::TTF_STYLE_NORMAL,
static StyleBold = ffi::TTF_STYLE_BOLD,
static StyleItalic = ffi::TTF_STYLE_ITALIC,
static StyleUnderline = ffi::TTF_STYLE_UNDERLINE,
static StyleStrikeThrough = ffi::TTF_STYLE_STRIKETHROUGH
})
#[deriving(Show, Eq, FromPrimitive)]
#[deriving(Show, PartialEq, FromPrimitive)]
pub enum Hinting {
HintingNormal = ffi::TTF_HINTING_NORMAL as int,
HintingLight = ffi::TTF_HINTING_LIGHT as int,
@@ -76,7 +74,7 @@ pub enum Hinting {
}
/// Glyph Metrics
#[deriving(Eq, Clone, Show)]
#[deriving(PartialEq, Clone, Show)]
pub struct GlyphMetrics {
pub minx: int,
pub maxx: int,
@@ -117,7 +115,7 @@ pub fn quit() {
/// The opaque holder of a loaded font.
#[allow(raw_pointer_deriving)]
#[deriving(Eq)]
#[deriving(PartialEq)]
pub struct Font {
raw: *ffi::TTF_Font,
owned: bool
@@ -167,14 +165,16 @@ impl Font {
pub fn get_style(&self) -> FontStyle {
//! Get font render style
let raw = unsafe { ffi::TTF_GetFontStyle(self.raw) };
FontStyle::new(raw)
unsafe {
let raw = ffi::TTF_GetFontStyle(self.raw);
FontStyle::from_bits_truncate(raw)
}
}
pub fn set_style(&mut self, styles: FontStyle) {
//! Set font render style.
unsafe {
ffi::TTF_SetFontStyle(self.raw, styles.get())
ffi::TTF_SetFontStyle(self.raw, styles.bits())
}
}