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
//! A tiny lib crate for tiny dice. #![crate_name = "tiny_die"] #![no_std] use core::fmt; use rand::Rng; /// Contains the `Die` struct and its functions. pub struct Die { /// A die has sides, that's what makes it a die. pub sides: u8, } impl Die { /// Returns a die with `sides` number of sides. /// # Arguments /// /// `sides` - optional; a `u8` representing the number of sides on the die /// /// # Example /// /// ```rust /// use tiny_die::Die; /// let dee_ten = Die::new(10); /// ``` pub fn new(sides: u8) -> Self { Self { sides: sides } } /// Returns a die with six sides. /// If you just need a standard d6, use Die::default(). /// /// # Example /// /// ```rust /// use tiny_die::Die; /// let dee_six = Die::default(); /// ``` pub fn default() -> Self { // There are two ways this can be executed, // but I haven't timed how long they take either way. // Self { sides: 6 } Self::new(6) } /// Return a number between 1 and number of sides plus 1. /// Plus 1 because that's how `gen_range()` works baybee. /// /// # Example /// /// ```rust /// use tiny_die::Die; /// let dee_ten = Die::new(10); /// let rolled = dee_ten.roll(); /// ``` pub fn roll(&self) -> u8 { rand::thread_rng().gen_range(1, self.sides + 1) } } impl fmt::Display for Die { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.roll()) } }