Skip to main content

DiceRoller

Trait DiceRoller 

Source
pub trait DiceRoller: Send + Sync {
    // Required method
    fn roll_individual(&self, count: u32, sides: u32) -> Vec<i64>;

    // Provided method
    fn roll(&self, count: u32, sides: u32) -> i64 { ... }
}
Expand description

A pluggable dice-rolling backend.

Implement this trait to substitute the default rand-based roller with a deterministic stub in tests, or a custom RNG in production.

Required Methods§

Source

fn roll_individual(&self, count: u32, sides: u32) -> Vec<i64>

Roll count dice each with sides faces and return each individual result.

Each element is in the range 1..=sides. An empty Vec is returned when count == 0. Behaviour for sides == 0 is implementation-defined.

Provided Methods§

Source

fn roll(&self, count: u32, sides: u32) -> i64

Roll and return the total (sum of all individual results).

§Overflow behaviour

The default implementation uses checked addition and saturates to i64::MAX on overflow. This is a limitation of the i64 return type — callers that need proper error propagation should use Self::roll_individual and sum the results with [crate::vm::eval::checked_sum_rolls] instead.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§