Struct Dice

Source
pub struct Dice<T: Rollable = u32> { /* private fields */ }
Expand description

A Handful of dice.

§Examples

use one_d_six::Dice;

let mut dice: Dice = "3d6".parse().unwrap();

assert!(dice.roll_all().total() >= 3);
assert!(dice.total() <= 18);

§Adding two collections of dice

use one_d_six::Dice;

let one_d6: Dice = "1d6".parse().unwrap();
let three_d4 = Dice::new(3, 4);

let dice = one_d6 + three_d4;

assert!(dice.total() >= 4);
assert!(dice.total() <= 18);

Implementations§

Source§

impl<T: Rollable> Dice<T>

Source

pub fn new(dice: usize, faces: T) -> Self

Creates a new set of dice. Each die in the set has an initial starting value. Only allows dice of same type. No mixture of d4 and d6.

§Example
use one_d_six::Dice;

// Creates 3d6 dice collection
let dice = Dice::new(3, 6);
Source

pub fn from(dice: Box<[Die<T>]>) -> Self

Creates a set of dice from a Vec<Die>. Allows for mixture of Die types (d4, d6, etc.).

§Example
use one_d_six::{
    Dice,
    Die,
};

// Creates 2d6 + 1d4 dice collection
let dice = {
    let dice = [
        Die::new(6),
        Die::new(6),
        Die::new(4),
    ];
    Dice::from(Box::new(dice))
};
Source

pub fn current_faces(&self) -> Vec<T>

Gets the current face of each die in the dice set.

§Example
use one_d_six::Dice;

let four_coins = Dice::new(4, 2);

for val in four_coins.current_faces().iter() {
    assert!(val == &1 || val == &2);
}
Source

pub fn roll_all(&mut self) -> &Self

Rolls all dice and returns self.

§Example
use one_d_six::Dice;

let mut ten_d_4 = Dice::new(10, 4);

for val in ten_d_4.roll_all().current_faces().iter() {
    let val: u32 = *val;
    assert!(val >= 1);
    assert!(val <= 4);
}
Source

pub fn total(&self) -> T
where T: DiceTotal<T>,

Gets the total of the current faces of the dice.

§Example
use one_d_six::Dice;

let two_d_4 = Dice::new(2, 4);

assert!(two_d_4.total() >= 2);
assert!(two_d_4.total() <= 8);

Trait Implementations§

Source§

impl<T: Rollable> Add for Dice<T>

Source§

type Output = Dice<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> Debug for Dice<T>
where T: Display + Rollable,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Dice<T>
where T: DiceTotal<T> + Display + Rollable,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> FromStr for Dice<T>
where T: FromStr + Rollable,

Source§

type Err = String

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Dice<T>

§

impl<T> RefUnwindSafe for Dice<T>
where T: RefUnwindSafe,

§

impl<T> Send for Dice<T>
where T: Send,

§

impl<T> Sync for Dice<T>
where T: Sync,

§

impl<T> Unpin for Dice<T>
where T: Unpin,

§

impl<T> UnwindSafe for Dice<T>
where T: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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