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>
impl<const SIDES: u8, R: Random<u8>> D<SIDES, R>
Sourcepub fn new(rng: R) -> Self
pub fn new(rng: R) -> Self
Creates a new die with the specified random number generator.
§Arguments
rng- A random number generator that implements theRandom<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>>
impl<const SIDES: u8> Default for D<SIDES, UniformThreadRandom<u8>>
Source§fn default() -> Self
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);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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more