D

Struct D 

Source
pub struct D<const SIDES: u8, R: Random<u8>> { /* private fields */ }
Expand description

A generic die with a configurable number of sides.

This struct represents a die with SIDES number of sides, using a random number generator of type R that implements the Random<u8> trait.

The die is thread-safe due to the internal mutex protecting the random number generator. Nevertheless, consider creating new dice for each thread to avoid waiting for the lock.

§Type Parameters

  • SIDES - The number of sides on the die (must be a positive integer)
  • R - The type of random number generator to use

§Examples

Creating a custom 30-sided die:

use darkforge_rng::dice::{D, Dice};
use darkforge_rng::rng::UniformThreadRandom;

// Create a 30-sided die with the default random number generator
let d30 = D::<30, UniformThreadRandom<u8>>::default();
let result = d30.roll();
assert!(result >= 1 && result <= 30);

Using a custom random number generator:

use darkforge_rng::dice::{D, Dice};
use darkforge_rng::rng::UniformThreadRandom;

// Create a custom RNG for a 12-sided die
let rng = UniformThreadRandom::new(1, 12).unwrap();
let d12 = D::<12, _>::new(rng);

let result = d12.roll();
assert!(result >= 1 && result <= 12);

Implementations§

Source§

impl<const SIDES: u8, R: Random<u8>> D<SIDES, R>

Source

pub fn new(rng: R) -> Self

Creates a new die with the specified random number generator.

§Arguments
  • rng - A random number generator that implements the Random<u8> trait
§Examples
use darkforge_rng::dice::{D, Dice};
use darkforge_rng::rng::UniformThreadRandom;

let rng = UniformThreadRandom::new(1, 20).unwrap();
let d20 = D::<20, _>::new(rng);

let result = d20.roll();
assert!(result >= 1 && result <= 20);

Trait Implementations§

Source§

impl<const SIDES: u8> Default for D<SIDES, UniformThreadRandom<u8>>

Source§

fn default() -> Self

Creates a new die with the default random number generator.

This uses a UniformThreadRandom<u8> configured to generate values between 1 and SIDES (inclusive).

§Examples
use darkforge_rng::dice::{D, Dice};

let d6 = D::<6, _>::default();
let result = d6.roll();
assert!(result >= 1 && result <= 6);
Source§

impl<const SIDES: u8, R: Random<u8>> Dice for D<SIDES, R>

Source§

fn roll(&self) -> u8

Rolls the die once and returns the result. Read more
Source§

fn roll_pool(&self, pool: usize) -> Vec<u8>

Rolls the die multiple times and returns all results as a vector. Read more
Source§

fn sides(&self) -> u8

Returns the number of sides on this die. Read more

Auto Trait Implementations§

§

impl<const SIDES: u8, R> !Freeze for D<SIDES, R>

§

impl<const SIDES: u8, R> RefUnwindSafe for D<SIDES, R>

§

impl<const SIDES: u8, R> Send for D<SIDES, R>
where R: Send,

§

impl<const SIDES: u8, R> Sync for D<SIDES, R>
where R: Send,

§

impl<const SIDES: u8, R> Unpin for D<SIDES, R>
where R: Unpin,

§

impl<const SIDES: u8, R> UnwindSafe for D<SIDES, R>

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