Merge pull request #86 from jcmoyer/flags-trait

Flags trait
This commit is contained in:
Tony Aldridge
2014-04-30 10:04:05 +01:00
9 changed files with 34 additions and 9 deletions
+26 -2
View File
@@ -1,9 +1,11 @@
// Loosely based on zlib-licensed code from RustAllegro
// https://github.com/SiegeLord/RustAllegro
//
// Implements efficient, type-safe flags with support for bitwise operators.
//! Implements efficient, type-safe flags with support for bitwise operators.
//
// Usage:
// The macros defined in this module are for internal usage only.
//
// Example:
//
// flag_type!(FlagTypeName {
// FlagName1 = FlagValue1,
@@ -19,6 +21,22 @@
#![macro_escape]
/// The `Flags` trait is used to define values for flag types where either none
/// or all of the flags are set.
pub trait Flags: ::std::ops::Not<Self> {
/// Returns a value representing an empty bitset. Implementors should ensure
/// this function returns the equivalent of `0` (no bits set) for a flag
/// type.
fn none() -> Self;
/// By default, this function will negate the result of `none()`. For sanely
/// implemented types, this should be equivalent to having all flags set.
fn all() -> Self {
let none: Self = Flags::none();
!none
}
}
macro_rules! flag_type(
($typename:ident : $supertype:ident { $($name:ident = $value:expr),* }) => {
pub struct $typename {
@@ -99,6 +117,12 @@ macro_rules! flag_type(
}
}
impl ::flags::Flags for $typename {
fn none() -> $typename {
$typename { bits: 0 as $supertype }
}
}
$(
pub static $name: $typename = $typename { bits: $value as $supertype };
)+
+1 -1
View File
@@ -1,4 +1,4 @@
mod flag;
mod flags;
#[allow(non_camel_case_types)]
pub mod ll {
+1 -1
View File
@@ -9,7 +9,7 @@ use rect::Rect;
use scancode::ScanCode;
use video::Window;
mod flag;
mod flags;
#[allow(non_camel_case_types)]
pub mod ll {
+1
View File
@@ -33,3 +33,4 @@ pub mod rwops;
pub mod sdl;
pub mod audio;
pub mod version;
pub mod flags;
+1 -1
View File
@@ -4,7 +4,7 @@ use get_error;
use surface;
use video;
mod flag;
mod flags;
#[allow(non_camel_case_types)]
pub mod ll {
+1 -1
View File
@@ -13,7 +13,7 @@ use std::num::FromPrimitive;
use std::vec::Vec;
use std::c_vec::CVec;
mod flag;
mod flags;
#[allow(non_camel_case_types)]
pub mod ll {
+1 -1
View File
@@ -1,7 +1,7 @@
use std::cast;
use std::str;
mod flag;
mod flags;
// Setup linking for all targets.
#[cfg(target_os="macos")]
+1 -1
View File
@@ -6,7 +6,7 @@ use libc::c_int;
use pixels;
use rwops;
mod flag;
mod flags;
#[allow(non_camel_case_types)]
pub mod ll {
+1 -1
View File
@@ -11,7 +11,7 @@ use std::num::FromPrimitive;
use get_error;
mod flag;
mod flags;
#[allow(non_camel_case_types)]
pub mod ll {