Less failure, better comments
This commit is contained in:
parent
a111ea482b
commit
1c185c22a8
18
src/roll.rs
18
src/roll.rs
@ -1,20 +1,19 @@
|
|||||||
//! A struct in which our data is held for rolling. By ensuring that
|
//! A struct in which our data is held for rolling. By ensuring that
|
||||||
//! only `usize`s are used we prevent the need for additional type
|
//! only `usize`s are used we prevent the need for additional type
|
||||||
//! checking at time of use. */
|
//! checking at time of use.
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
use super::std;
|
use super::std;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Roll {
|
pub struct Roll {
|
||||||
die: usize,
|
die: usize,
|
||||||
number: usize,
|
number: usize,
|
||||||
}
|
}
|
||||||
impl Roll {
|
impl Roll {
|
||||||
/// A fn to create new `Roll` structs, private to prevent
|
/// A function to create new `Roll` structs.
|
||||||
/// being used outside the library.
|
|
||||||
pub fn new(number: usize, die: usize) -> Roll {
|
pub fn new(number: usize, die: usize) -> Roll {
|
||||||
Roll {
|
Roll {
|
||||||
die: die,
|
die: die,
|
||||||
@ -26,7 +25,7 @@ impl Roll {
|
|||||||
///Allows the fields of `Roll` to be private (and unchangable)
|
///Allows the fields of `Roll` to be private (and unchangable)
|
||||||
///while still allowing access to act on their internal value
|
///while still allowing access to act on their internal value
|
||||||
/// ```
|
/// ```
|
||||||
/// let x = Roll::new(&2, &6);
|
/// let x = Roll::new(2, 6);
|
||||||
/// assert!(x.number() == 2);
|
/// assert!(x.number() == 2);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn number(&self) -> usize {
|
pub fn number(&self) -> usize {
|
||||||
@ -41,14 +40,14 @@ impl Roll {
|
|||||||
/// Creates a Vector to hold each of the values rolled. Making
|
/// Creates a Vector to hold each of the values rolled. Making
|
||||||
/// a Vector ensures the ability to work on exactly the same
|
/// a Vector ensures the ability to work on exactly the same
|
||||||
/// die rolls easily and accurately, since calling the `Roll`
|
/// die rolls easily and accurately, since calling the `Roll`
|
||||||
/// iterator again will produce new results.
|
/// iterator again will produce new results. Consumes the `Roll`
|
||||||
pub fn rolls(self) -> Vec<usize> {
|
pub fn rolls(self) -> Vec<usize> {
|
||||||
self.collect()
|
self.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used on a `Roll` to return only the final value, ignoring
|
/// Used on a `Roll` to return only the final value, ignoring
|
||||||
/// the values used to add up to that result. Allows `.total()`
|
/// the values used to add up to that result. Allows `.total()`
|
||||||
/// syntax when used
|
/// syntax when used. Consumes the `Roll`.
|
||||||
pub fn total(self) -> usize {
|
pub fn total(self) -> usize {
|
||||||
self.sum()
|
self.sum()
|
||||||
}
|
}
|
||||||
@ -123,8 +122,9 @@ fn displays() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn rolls() {
|
fn rolls() {
|
||||||
let x = Roll::new(20, 6);
|
let x = Roll::new(20, 6);
|
||||||
for y in x.clone().rolls() {
|
let y = x.die;
|
||||||
assert!(y <= x.die());
|
for z in x.rolls() {
|
||||||
|
assert!(z <= y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user