diff --git a/src/sdl2/flag.rs b/src/sdl2/flags.rs similarity index 76% rename from src/sdl2/flag.rs rename to src/sdl2/flags.rs index 4b573574..d656bd33 100644 --- a/src/sdl2/flag.rs +++ b/src/sdl2/flags.rs @@ -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 { + /// 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 }; )+ diff --git a/src/sdl2/joystick.rs b/src/sdl2/joystick.rs index 7b4a6018..c27fc0fc 100644 --- a/src/sdl2/joystick.rs +++ b/src/sdl2/joystick.rs @@ -1,4 +1,4 @@ -mod flag; +mod flags; #[allow(non_camel_case_types)] pub mod ll { diff --git a/src/sdl2/keyboard.rs b/src/sdl2/keyboard.rs index 333bdcce..4630c89e 100644 --- a/src/sdl2/keyboard.rs +++ b/src/sdl2/keyboard.rs @@ -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 { diff --git a/src/sdl2/lib.rs b/src/sdl2/lib.rs index 9c5fa286..e3f5ff6b 100644 --- a/src/sdl2/lib.rs +++ b/src/sdl2/lib.rs @@ -33,3 +33,4 @@ pub mod rwops; pub mod sdl; pub mod audio; pub mod version; +pub mod flags; \ No newline at end of file diff --git a/src/sdl2/mouse.rs b/src/sdl2/mouse.rs index 6fb79b6a..1d983e82 100644 --- a/src/sdl2/mouse.rs +++ b/src/sdl2/mouse.rs @@ -4,7 +4,7 @@ use get_error; use surface; use video; -mod flag; +mod flags; #[allow(non_camel_case_types)] pub mod ll { diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index bcb1ddaf..4c97a56c 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -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 { diff --git a/src/sdl2/sdl.rs b/src/sdl2/sdl.rs index 20fa9a25..7ad8fde7 100644 --- a/src/sdl2/sdl.rs +++ b/src/sdl2/sdl.rs @@ -1,7 +1,7 @@ use std::cast; use std::str; -mod flag; +mod flags; // Setup linking for all targets. #[cfg(target_os="macos")] diff --git a/src/sdl2/surface.rs b/src/sdl2/surface.rs index d291366a..eccfd64b 100644 --- a/src/sdl2/surface.rs +++ b/src/sdl2/surface.rs @@ -6,7 +6,7 @@ use libc::c_int; use pixels; use rwops; -mod flag; +mod flags; #[allow(non_camel_case_types)] pub mod ll { diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 6645e17c..d6a6cf01 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -11,7 +11,7 @@ use std::num::FromPrimitive; use get_error; -mod flag; +mod flags; #[allow(non_camel_case_types)] pub mod ll {