1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
// Copyright ⓒ 2017 Fletcher Nichol and/or applicable contributors. // // Licensed under the Apache License, Version 2.0 (see LICENSE-APACHE or // <http://www.apache.org/licenses/LICENSE-2.0>) or the MIT license (see<LICENSE-MIT or // <http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or // distributed except according to those terms. //! `features` is a small library that implements runtime [feature toggles][fowler_toggles] for //! your library or program allowing behavior to be changed on boot or dynamically at runtime using //! the same compiled binary artifact. This is different from cargo's [feature][cargo_feature] //! support which uses conditional compilation. //! //! At its core, it is a macro (`features!`) that takes a collection of feature flag names which it //! uses to generate a module containing a function to enable a feature toggle (`::enable()`), a //! function to disable a feature toggle (`::disable()`) and a function to check if a feature //! toggle is enabled (`::is_enabled()`). //! //! ## Example //! //! Basic example: //! //! ``` //! #[macro_use] //! extern crate bitflags; //! #[macro_use] //! extern crate features; //! //! features! { //! pub mod feature { //! const Alpha = 0b00000001, //! const Beta = 0b00000010 //! } //! } //! //! fn main() { //! assert_eq!(false, feature::is_enabled(feature::Alpha)); //! assert_eq!(false, feature::is_enabled(feature::Beta)); //! //! feature::enable(feature::Beta); //! assert_eq!(false, feature::is_enabled(feature::Alpha)); //! assert_eq!(true, feature::is_enabled(feature::Beta)); //! } //! ``` //! //! Multiple feature sets: //! //! ``` //! #[macro_use] //! extern crate bitflags; //! #[macro_use] //! extern crate features; //! //! features! { //! pub mod ux { //! const JsonOutput = 0b10000000, //! const VerboseOutput = 0b01000000 //! } //! } //! //! features! { //! pub mod srv { //! const Http2Downloading = 0b10000000, //! const BitTorrentDownloading = 0b01000000 //! } //! } //! //! fn main() { //! // Parse CLI args, environment, read config file etc... //! srv::enable(srv::BitTorrentDownloading); //! ux::enable(ux::JsonOutput); //! //! if srv::is_enabled(srv::Http2Downloading) { //! println!("Downloading via http2..."); //! } else if srv::is_enabled(srv::BitTorrentDownloading) { //! println!("Downloading via bit torrent..."); //! } else { //! println!("Downloading the old fashioned way..."); //! } //! //! if ux::is_enabled(ux::VerboseOutput) { //! println!("COOL"); //! } //! } //! ``` //! //! ## Feature Toggle References //! //! Here are some articles and projects which insipred the implementation of `features`: //! //! * [Feature Toggles](https://martinfowler.com/articles/feature-toggles.html) (Martin Fowler's //! blog) //! * [Using Feature Flags to Ship Changes with //! Confidence](https://blog.travis-ci.com/2014-03-04-use-feature-flags-to-ship-changes-with-confidence/) //! (TravisCI's blog) //! * [Feature Toggles are one of the worst kinds of Technical //! Debt](http://swreflections.blogspot.ca/2014/08/feature-toggles-are-one-of-worst-kinds.html) //! (excellent cautionary tale and warning) //! * [Feature toggle](https://en.wikipedia.org/wiki/Feature_toggle) (Wikipedia article) //! * [Ruby feature gem](https://github.com/mgsnova/feature) (nice prior art) //! //! [fowler_toggles]: https://martinfowler.com/articles/feature-toggles.html //! [cargo_feature]: http://doc.crates.io/manifest.html#the-features-section #[macro_use] extern crate bitflags; /// The `features!` macro generates a module to contain all feature toggling logic. /// /// # Examples /// /// Basic example: /// /// ``` /// #[macro_use] /// extern crate bitflags; /// #[macro_use] /// extern crate features; /// /// features! { /// pub mod feature { /// const Alpha = 0b00000001, /// const Beta = 0b00000010 /// } /// } /// /// fn main() { /// assert_eq!(false, feature::is_enabled(feature::Alpha)); /// assert_eq!(false, feature::is_enabled(feature::Beta)); /// /// feature::enable(feature::Beta); /// assert_eq!(false, feature::is_enabled(feature::Alpha)); /// assert_eq!(true, feature::is_enabled(feature::Beta)); /// } /// ``` /// /// Multiple feature sets: /// /// ``` /// #[macro_use] /// extern crate bitflags; /// #[macro_use] /// extern crate features; /// /// features! { /// pub mod ux { /// const JsonOutput = 0b10000000, /// const VerboseOutput = 0b01000000 /// } /// } /// /// features! { /// pub mod srv { /// const Http2Downloading = 0b10000000, /// const BitTorrentDownloading = 0b01000000 /// } /// } /// /// fn main() { /// // Parse CLI args, environment, read config file etc... /// srv::enable(srv::BitTorrentDownloading); /// ux::enable(ux::JsonOutput); /// /// if srv::is_enabled(srv::Http2Downloading) { /// println!("Downloading via http2..."); /// } else if srv::is_enabled(srv::BitTorrentDownloading) { /// println!("Downloading via bit torrent..."); /// } else { /// println!("Downloading the old fashioned way..."); /// } /// /// if ux::is_enabled(ux::VerboseOutput) { /// println!("COOL"); /// } /// } /// ``` /// #[macro_export] macro_rules! features { (mod $mod_name:ident { $($(#[$flag_attr:meta])* const $flag:ident = $value:expr),+ }) => { #[allow(non_upper_case_globals)] mod $mod_name { features! { @_impl mod $mod_name { $($(#[$flag_attr])* const $flag = $value),+ } } } }; (pub mod $mod_name:ident { $($(#[$flag_attr:meta])* const $flag:ident = $value:expr),+ }) => { #[allow(non_upper_case_globals)] pub mod $mod_name { features! { @_impl mod $mod_name { $($(#[$flag_attr])* const $flag = $value),+ } } } }; (@_impl mod $mod_name:ident { $($(#[$flag_attr:meta])* const $flag:ident = $value:expr),+ }) => { use std::sync::atomic; bitflags! { pub flags Flags: usize { $($(#[$flag_attr])* const $flag = $value),+ } } static mut FEATURES: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT; #[allow(dead_code)] pub fn enable(flag: Flags) { let mut features = unsafe { FEATURES.get_mut() }; let mut flags = Flags::from_bits_truncate(*features); flags.insert(flag); *features = flags.bits(); } #[allow(dead_code)] pub fn disable(flag: Flags) { let mut features = unsafe { FEATURES.get_mut() }; let mut flags = Flags::from_bits_truncate(*features); flags.remove(flag); *features = flags.bits(); } #[allow(dead_code)] pub fn is_enabled(flag: Flags) -> bool { flags().contains(flag) } #[allow(dead_code)] pub fn flags() -> Flags { unsafe { Flags::from_bits_truncate(FEATURES.load(atomic::Ordering::Relaxed)) } } }; } #[cfg(test)] mod tests { #[test] fn enabling() { features! { pub mod f { const Alpha = 0b00000001, const Beta = 0b00000010 } } assert_eq!(false, f::is_enabled(f::Alpha)); assert_eq!(false, f::is_enabled(f::Beta)); f::enable(f::Alpha); assert_eq!(true, f::is_enabled(f::Alpha)); assert_eq!(false, f::is_enabled(f::Beta)); // Enable again f::enable(f::Alpha); assert_eq!(true, f::is_enabled(f::Alpha)); assert_eq!(false, f::is_enabled(f::Beta)); f::enable(f::Beta); assert_eq!(true, f::is_enabled(f::Alpha)); assert_eq!(true, f::is_enabled(f::Beta)); } #[test] fn disabling() { features! { pub mod f { const Cool = 0b01000000, const Beans = 0b10000000 } } f::enable(f::Cool); f::enable(f::Beans); assert_eq!(true, f::is_enabled(f::Cool)); assert_eq!(true, f::is_enabled(f::Beans)); f::disable(f::Cool); assert_eq!(false, f::is_enabled(f::Cool)); assert_eq!(true, f::is_enabled(f::Beans)); // Disable again f::disable(f::Cool); assert_eq!(false, f::is_enabled(f::Cool)); assert_eq!(true, f::is_enabled(f::Beans)); f::disable(f::Beans); assert_eq!(false, f::is_enabled(f::Cool)); assert_eq!(false, f::is_enabled(f::Beans)); } }