From e40cb0bdd871a0a6ec1b72cba53ab3950a7472e7 Mon Sep 17 00:00:00 2001 From: "J.C. Moyer" Date: Mon, 28 Apr 2014 23:02:40 -0400 Subject: [PATCH 1/3] Add Flags trait for all flag types This trait makes it easier to specify none or all flags. --- src/sdl2/flag.rs | 14 ++++++++++++++ src/sdl2/lib.rs | 1 + 2 files changed, 15 insertions(+) diff --git a/src/sdl2/flag.rs b/src/sdl2/flag.rs index 4b573574..cf8aa90d 100644 --- a/src/sdl2/flag.rs +++ b/src/sdl2/flag.rs @@ -19,6 +19,14 @@ #![macro_escape] +pub trait Flags: ::std::ops::Not { + fn none() -> Self; + 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 +107,12 @@ macro_rules! flag_type( } } + impl ::flag::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/lib.rs b/src/sdl2/lib.rs index 9c5fa286..76244401 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 flag; \ No newline at end of file From 04546b77e6c364fe5e1dbb48b3aa8a360b4f8fc9 Mon Sep 17 00:00:00 2001 From: "J.C. Moyer" Date: Mon, 28 Apr 2014 23:08:10 -0400 Subject: [PATCH 2/3] Rename flag to flags for stdlib consistency --- src/sdl2/{flag.rs => flags.rs} | 2 +- src/sdl2/joystick.rs | 2 +- src/sdl2/keyboard.rs | 2 +- src/sdl2/lib.rs | 2 +- src/sdl2/mouse.rs | 2 +- src/sdl2/render.rs | 2 +- src/sdl2/sdl.rs | 2 +- src/sdl2/surface.rs | 2 +- src/sdl2/video.rs | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) rename src/sdl2/{flag.rs => flags.rs} (98%) diff --git a/src/sdl2/flag.rs b/src/sdl2/flags.rs similarity index 98% rename from src/sdl2/flag.rs rename to src/sdl2/flags.rs index cf8aa90d..c2bc4eb1 100644 --- a/src/sdl2/flag.rs +++ b/src/sdl2/flags.rs @@ -107,7 +107,7 @@ macro_rules! flag_type( } } - impl ::flag::Flags for $typename { + impl ::flags::Flags for $typename { fn none() -> $typename { $typename { bits: 0 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 76244401..e3f5ff6b 100644 --- a/src/sdl2/lib.rs +++ b/src/sdl2/lib.rs @@ -33,4 +33,4 @@ pub mod rwops; pub mod sdl; pub mod audio; pub mod version; -pub mod flag; \ No newline at end of file +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 { From 5f2837b9fbfbacb8e40bcf9ba603624da323193a Mon Sep 17 00:00:00 2001 From: "J.C. Moyer" Date: Tue, 29 Apr 2014 19:35:28 -0400 Subject: [PATCH 3/3] Add rustdoc comments for flags module --- src/sdl2/flags.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/sdl2/flags.rs b/src/sdl2/flags.rs index c2bc4eb1..d656bd33 100644 --- a/src/sdl2/flags.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,8 +21,16 @@ #![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