pub struct Rolled<'a> {
pub rolls: Vec<DieRoll>,
pub dice: Cow<'a, Dice>,
}
Expand description
Representation of the result from rolling Dice
Fields§
§rolls: Vec<DieRoll>
Each individual die roll that was made
dice: Cow<'a, Dice>
Dice that were rolled to produce this
Implementations§
Source§impl Rolled<'_>
impl Rolled<'_>
Sourcepub fn total(&self) -> Result<u16, Error>
pub fn total(&self) -> Result<u16, Error>
Calculates the total of all roll values.
§Errors
If there is an integer overflow while summing the die rolls, an error variant is returned.
§Examples
use tyche::dice::{roller::{FastRand as FastRandRoller, Roller}, Dice};
let dice = Dice::new(4, 8);
let rolled = FastRandRoller::default().roll(&dice, true)?;
let total = rolled.total()?;
assert_eq!(total, rolled.rolls.iter().map(|roll| roll.val as u16).sum());
Sourcepub fn into_owned(self) -> Rolled<'static>
pub fn into_owned(self) -> Rolled<'static>
Moves all of self’s owned data into a new instance and clones any unowned data in order to create a 'static
instance of self.
Sourcepub fn from_dice_and_rolls(
dice: &Dice,
rolls: impl IntoIterator<Item = u8>,
) -> Rolled<'_>
pub fn from_dice_and_rolls( dice: &Dice, rolls: impl IntoIterator<Item = u8>, ) -> Rolled<'_>
Creates a new rolled set of dice from a given set of dice and an iterator of values.
Trait Implementations§
Source§impl Describe for Rolled<'_>
impl Describe for Rolled<'_>
Source§fn describe(&self, list_limit: Option<usize>) -> String
fn describe(&self, list_limit: Option<usize>) -> String
Builds a string of the dice the roll is from and a list of all of the individual rolled dice
(see DieRoll::fmt()
).
If list_limit
is specified and there are more rolls than it, the list of rolled dice will be truncated and
appended with “X more…” (where X is the remaining roll count past the max).
§Examples
use std::borrow::Cow;
use tyche::{dice::{Dice, DieRoll, Rolled}, expr::Describe};
let dice = Dice::builder().count(4).sides(6).keep_high(2).build();
let kh_mod = dice.modifiers[0];
let rolled = Rolled {
rolls: vec![
DieRoll::new(6),
{
let mut roll = DieRoll::new(2);
roll.drop(kh_mod);
roll
},
DieRoll::new(5),
{
let mut roll = DieRoll::new(3);
roll.drop(kh_mod);
roll
},
],
dice: Cow::Borrowed(&dice),
};
assert_eq!(rolled.describe(None), "4d6kh2[6, 2 (d), 5, 3 (d)]");
assert_eq!(rolled.describe(Some(2)), "4d6kh2[6, 2 (d), 2 more...]");