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
#![no_std] // Enable annotating features requirements in docs #![cfg_attr(feature = "doc_cfg", feature(doc_cfg))] // This crate is entirely safe, actually it's just macros #![forbid(unsafe_code)] // Ensures that `pub` means published in the public API. // This property is useful for reasoning about breaking API changes. #![deny(unreachable_pub)] // Denies invalid links in docs #![deny(broken_intra_doc_links)] //! Provides a macro to select a random branch. //! //! This crate provides the [`branch`](crate::branch) and //! [`branch_using`](crate::branch_using) macro, which will //! execute randomly one of the given expressions. //! //! It is maybe best visualized by the following example: //! //! ```rust //! # use random_branch::branch; //! branch!( //! println!("First line."), //! println!("Second line?"), //! println!("Third line!"), //! ); //! ``` //! //! This will be turned into something similar to this: //! //! ```rust //! # use rand::Rng; //! match rand::thread_rng().gen_range(0..3) { //! 0 => println!("First line."), //! 1 => println!("Second line?"), //! 2 => println!("Third line!"), //! _ => unreachable!(), //! } //! ``` //! //! For more details see [`branch`](crate::branch) and //! [`branch_using`](crate::branch_using). The basic difference between them is, //! that `branch` uses [`rand::thread_rng()`](rand::thread_rng()) whereas //! `branch_using` uses the the given [`rand::Rng`](rand::Rng). //! /// Branches into one of the given expressions using the given RNG. /// /// This macro dose essentially the same as [`branch`] but uses the given /// [`Rng`](rand::Rng). /// /// This macro turns something like this: /// /// ```rust /// # use rand_pcg::Lcg64Xsh32; /// # use random_branch::branch_using; /// let mut my_rng = /* snip */ /// # Lcg64Xsh32::new(0,0); /// /// branch_using!( my_rng, { /// println!("First line."), /// println!("Second line?"), /// println!("Third line!"), /// }); /// ``` /// /// into something similar to this: /// /// ```rust /// # use rand_pcg::Lcg64Xsh32; /// let mut my_rng = /* snip */ /// # Lcg64Xsh32::new(0,0); /// # use rand::Rng; /// /// match my_rng.gen_range(0..3) { /// 0 => println!("First line."), /// 1 => println!("Second line?"), /// 2 => println!("Third line!"), /// _ => unreachable!(), /// } /// ``` /// /// # Examples /// /// You can use functions, macros and other arbitrary expressions: /// /// ```rust /// # use rand_pcg::Lcg64Xsh32; /// use random_branch::branch_using; /// fn do_something() { /// println!("There is no such thing") /// } /// let thing = "fuliluf"; /// let mut my_rng = /* snip */ /// # Lcg64Xsh32::new(0,0); /// /// branch_using!( my_rng, { /// println!("A {} is an animal!", thing), /// { /// let thing = "lufiful"; /// println!("Two {}s will never meet.", thing) /// }, /// println!("Only a {} can see other {0}s.", thing), /// do_something(), /// }); /// ``` /// /// You can also use it as an expression to yield some randomly chosen value: /// /// ```rust /// # use rand_pcg::Lcg64Xsh32; /// use random_branch::branch_using; /// let mut my_rng = /* snip */ /// # Lcg64Xsh32::new(0,0); /// /// let num = branch_using!( my_rng, { /// 10, /// 10 + 11, /// 2 * (10 + 11), /// 85, /// }); /// assert!(num == 10 || num == 21 || num == 42 || num == 85); /// ``` #[macro_export] macro_rules! branch_using { ( $rng:expr, { $( $branch:expr ),* $(,)? }) => { { random_branch::branch_internal!(@parseRule $rng, 0, { }, { $( { $branch } )* }, ) } }; } /// Branches into one of the given expressions. /// /// This macro dose essentially the same as [`branch_using`] instead of giving /// it some RNG, this macro will simply use the [`rand::thread_rng()`]. /// However, this then requires `std`, unlike `branch_using`. /// /// This macro turns something like this: /// /// ```rust /// # use random_branch::branch; /// branch!( /// println!("First line."), /// println!("Second line?"), /// println!("Third line!"), /// ); /// ``` /// /// into something similar to this using the `thread_rng()`: /// /// ```rust /// # use rand::Rng; /// match rand::thread_rng().gen_range(0..3) { /// 0 => println!("First line."), /// 1 => println!("Second line?"), /// 2 => println!("Third line!"), /// _ => unreachable!(), /// } /// ``` /// /// /// # Examples /// /// You can use functions, macros and other arbitrary expressions: /// /// ```rust /// use random_branch::branch; /// /// fn do_something() { /// println!("There is no such thing") /// } /// let thing = "fuliluf"; /// /// branch!( /// println!("A {} is an animal!", thing), /// { /// let thing = "lufiful"; /// println!("Two {}s will never meet.", thing) /// }, /// println!("Only a {} can see other {0}s.", thing), /// do_something(), /// ); /// ``` /// /// You can also use it as an expression to yield some randomly chosen value: /// /// ```rust /// use random_branch::branch; /// /// let num = branch!( /// 10, /// 10 + 11, /// 2 * (10 + 11), /// 85, /// ); /// println!("The best number is {}", num); /// # assert!(num == 10 || num == 21 || num == 42 || num == 85); /// ``` #[macro_export] #[cfg(feature = "std")] #[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "std")))] macro_rules! branch { ( $( $branch:expr ),* $(,)? ) => { { random_branch::branch_internal!(@parseRule rand::thread_rng(), 0, { }, { $( { $branch } )* }, ) } }; } /// Internal branching macro /// /// Each branch must be enclosed in braces e.g. `{ }` so it is a single `tt`. /// /// Syntax: /// ```text /// branch_internal!([RNG], [BRANCHES]+) /// ``` #[doc(hidden)] #[macro_export] macro_rules! branch_internal { // Entry pattern ( $rng:expr, $( $branches:tt )* ) => { random_branch::branch_internal!(@parseRule $rng, 0, {}, { $( $branches:tt )* }) }; // Invalid, base case (@parseRule $rng:expr, $cnt:expr, { }, { }, ) => { compile_error!("You must provide at least one choice.") }; // Prepares one branch at a time (@parseRule $rng:expr, $cnt:expr, { $( $stuff:tt )* }, { $branch:tt $( $rest:tt )* }, ) => { { random_branch::branch_internal!(@parseRule $rng, $cnt + 1, { $( $stuff )* { $cnt => $branch } }, { $( $rest )* }, ) } }; // Assembles all branches into a big match (@parseRule $rng:expr, $cnt:expr, { $( { $cc:expr => $branch:tt } )* }, { }, ) => {{ match rand::Rng::gen_range(&mut $rng, 0 .. ($cnt)) { $( n if n == $cc => $branch )* _ => unreachable!() } }}; } #[cfg(test)] mod tests { // We actually use mostly doc-tests, which are better suited for macro tests }