Struct DiceRoll

Source
pub struct DiceRoll {
    pub rolls: u32,
    pub sides: u32,
}
Expand description

A dice roll expressed in RPG term e.g. 3d6 means “roll a 6-sided die 3 times”.

Fields§

§rolls: u32

How many times the dice will be rolled.

§sides: u32

The maximum number of sides the dice has. If it has 6 sides, the most it can roll for at any one time is 6.

Implementations§

Source§

impl DiceRoll

Source

pub const MINIMUM_ROLLS: u32 = 1u32

All dice must be rollable at least once.

Source

pub const MINIMUM_SIDES: u32 = 2u32

All dice must have at least 2 sides.

Source

pub fn new(rolls: u32, sides: u32) -> Result<Self>

Create a DiceRoll.

§Errors
  • rolls is less than 1
  • sides is less than 2
Source

pub fn from_string(string: &str) -> Result<Self>

Create a DiceRoll from a string.

use dicelib::dice::DiceRoll;
let dice = DiceRoll::from_string(&"3d6".to_string()).unwrap();
§Errors
  • See parse_rolls_and_sides()
Source

pub fn parse_rolls_and_sides(string: &str) -> Result<(u32, u32)>

Utility function to parse the rolls and sides of a dice roll string into a pair of u32s. If you want a DiceRoll, use from_string() instead.

use dicelib::dice::DiceRoll;
let (rolls, sides) = DiceRoll::parse_rolls_and_sides(&"1d4".to_string()).unwrap();
§Errors
  • If rolls or sides cannot be matched (expression is malformed)
  • If the matched rolls and sides are not parseable as u32
Source

pub fn roll(&self, rng: &mut impl RngCore) -> Result<u32>

Performs the DiceRoll and returns the sum of all rolls.

§Errors
  • IntegerOverFlow if the rolls and sides are very, very big numbers.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V