Skip to content

Commit

Permalink
Prepare v0.2.0. (#1)
Browse files Browse the repository at this point in the history
* Prepare v0.2.0.

* Tweak escape sequence.
  • Loading branch information
matanlurey committed Dec 2, 2022
1 parent 7be4251 commit 8bc6115
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .github/actions-rs/grcov.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
output-type: lcov
excl-start:
- "mod tests \\{"
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## 0.2.0

- Removed `Copy` trait from structs with mutable state (i.e. `*Die` structs).
- `Roll.roll` trait now takes a `&Rotate` instead of taking ownership:

```diff
let d6 = D6::new();
- let rd = roller.roll(d6);
+ let rd = roller.roll(&d6);
assert_eq!(rd.value(), 3);
```

_This was always the intended behavior, we just made a mistake :)_

- `SliceDie::with_position(position, element)` changed to `(element, position`):

```diff
type GradeDie<'a> = SliceDie<'a, char, 5>;
const GRADES: [char; 5] = ['A', 'B', 'C', 'D', 'F'];

- let d = GradeDie::with_position(1, &GRADES);
+ let d = GradeDie::with_position(&GRADES, 1);
assert_eq!(d.value(), &'B');
```

- Fixed a bug where negative rotations on `SliceDie` were treated as positive.

## 0.1.0

- Initial release.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tomb"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Matan Lurey <matan@lurey.org>"]
description = "A minimal crate that provides dice rolling mechanisms for games"
Expand Down
2 changes: 1 addition & 1 deletion coverage.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CARGO_INCREMENTAL=0 RUSTFLAGS='-Cinstrument-coverage' LLVM_PROFILE_FILE='target/coverage/cargo-test-%p-%m.profraw' cargo test --tests
grcov target/coverage --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore "/*" -o target/coverage/html
grcov target/coverage --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore "/*" --excl-start "mod tests \{" -o target/coverage/html
open target/coverage/html/index.html
4 changes: 2 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//!
//! // Immutable objects.
//! let d6 = D6::new();
//! let rd = roller.roll(d6);
//! let rd = roller.roll(&d6);
//! assert_eq!(rd.value(), 3);
//!
//! // Mutable objects.
Expand Down Expand Up @@ -46,7 +46,7 @@ mod tests {
let d6 = D6::new();
assert_eq!(d6.value(), 1);

let rd = roller.roll(d6);
let rd = roller.roll(&d6);
assert_eq!(rd.value(), 3);
}

Expand Down
41 changes: 25 additions & 16 deletions src/items/dice/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::traits::{Numeric, Polyhedral, Rotate, RotateMut, Step, StepMut};
/// 1. Implement the [`Numeric`] trait.
/// 2. The _default_ value should be `1` or `1`-like.
/// 3. Solemnly swear to behave like numbers so that future traits can utilize them like one.
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
pub struct NumericDie<T, const MAXIMUM: usize>(T)
where
T: Numeric;
Expand Down Expand Up @@ -176,7 +176,7 @@ where
T: Numeric,
{
fn sides() -> usize {
MAXIMUM
Self::sides()
}
}

Expand Down Expand Up @@ -270,7 +270,7 @@ where
#[must_use]
fn rotate(&self, amount: i8) -> Self {
if amount == 0 {
return *self;
return self.clone();
}
let result = if amount > 0 {
rotate_forward_usize::<T, MAXIMUM>(amount.unsigned_abs() as usize, self.0.as_usize())
Expand Down Expand Up @@ -316,25 +316,18 @@ mod tests {
assert_eq!(d4_2.value(), 2);
}

#[test]
fn numeric_die_is_copy() {
fn copy<T>(die: T) -> T
where
T: Copy,
{
die
}

let d4_2 = copy(D4::from(2));
assert_eq!(d4_2.value(), 2);
}

#[test]
fn numeric_die_is_debug() {
let d4_2 = D4::from(2);
assert_eq!(format!("{:?}", d4_2), "D4:2");
}

#[test]
fn numeric_die_is_default() {
let d4_1: D4 = Default::default();
assert_eq!(d4_1.value(), 1);
}

#[test]
fn numeric_die_is_eq() {
let a = D4::from(2);
Expand Down Expand Up @@ -420,6 +413,22 @@ mod tests {
assert_eq!(d4.value(), 4);
}

#[test]
fn numeric_die_rotate_none() {
let d4_2 = D4::from(2);
let d4_2 = d4_2.rotate(0);

assert_eq!(d4_2.value(), 2);
}

#[test]
fn numeric_die_rotate_mut_none() {
let mut d4_2 = D4::from(2);
d4_2.rotate_mut(0);

assert_eq!(d4_2.value(), 2);
}

#[test]
fn numeric_die_rotate_next() {
let d4_2 = D4::from(2);
Expand Down
116 changes: 107 additions & 9 deletions src/items/dice/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::traits::{Polyhedral, Rotate, RotateMut, Step, StepMut};
///
/// SliceDie::from(&GRADES);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SliceDie<'a, T, const LENGTH: usize> {
position: usize,
elements: &'a [T; LENGTH],
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<'a, T, const LENGTH: usize> SliceDie<'a, T, LENGTH> {
/// # Panics
///
/// If the value is out of bounds.
pub fn with_position(position: usize, elements: &'a [T; LENGTH]) -> Self {
pub fn with_position(elements: &'a [T; LENGTH], position: usize) -> Self {
assert!(position < LENGTH);
Self { elements, position }
}
Expand Down Expand Up @@ -153,7 +153,7 @@ fn rotate_forward_usize<const MAXIMUM: usize>(position: usize, amount: usize) ->

fn rotate_backward_usize<const MAXIMUM: usize>(position: usize, amount: i8) -> usize {
let current = position as i8;
let rotated = current - amount;
let rotated = current + amount;
if rotated >= 0 {
return rotated.unsigned_abs() as usize;
}
Expand Down Expand Up @@ -198,6 +198,7 @@ impl<'a, T, const MAXIMUM: usize> RotateMut for SliceDie<'a, T, MAXIMUM> {
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::Polyhedral;

type GradeDie<'a> = SliceDie<'a, char, 5>;
const GRADES: [char; 5] = ['A', 'B', 'C', 'D', 'F'];
Expand All @@ -220,12 +221,18 @@ mod tests {

#[test]
fn slice_from() {
let a = GradeDie::new(&GRADES);
let b = GradeDie::new(&GRADES);
let a = GradeDie::from(&GRADES);
let b = GradeDie::from(&GRADES);

assert_eq!(a, b);
}

#[test]
fn slice_sides() {
let a = GradeDie::from(&GRADES);
assert_eq!(a.sides(), &GRADES);
}

#[test]
fn slice_next() {
let d = GradeDie::new(&GRADES).next();
Expand All @@ -236,7 +243,7 @@ mod tests {

#[test]
fn slice_next_wrap() {
let d = GradeDie::with_position(4, &GRADES).next();
let d = GradeDie::with_position(&GRADES, 4).next();

assert_eq!(d.position(), 0);
assert_eq!(d.value(), &'A');
Expand All @@ -253,7 +260,7 @@ mod tests {

#[test]
fn slice_next_mut_wrap() {
let mut d = GradeDie::with_position(4, &GRADES);
let mut d = GradeDie::with_position(&GRADES, 4);
d.next_mut();

assert_eq!(d.position(), 0);
Expand All @@ -262,7 +269,7 @@ mod tests {

#[test]
fn slice_back() {
let d = GradeDie::with_position(4, &GRADES).back();
let d = GradeDie::with_position(&GRADES, 4).back();

assert_eq!(d.position(), 3);
assert_eq!(d.value(), &'D');
Expand All @@ -278,7 +285,7 @@ mod tests {

#[test]
fn slice_back_mut() {
let mut d = GradeDie::with_position(4, &GRADES);
let mut d = GradeDie::with_position(&GRADES, 4);
d.back_mut();

assert_eq!(d.position(), 3);
Expand All @@ -293,4 +300,95 @@ mod tests {
assert_eq!(d.position(), 4);
assert_eq!(d.value(), &'F');
}

#[test]
fn slice_polyhedral_sides() {
let d = GradeDie::new(&GRADES);

fn get_sides<P: Polyhedral>(_: P) -> usize {
P::sides()
}

assert_eq!(get_sides(d), GRADES.len());
}

#[test]
fn slice_rotate_none() {
let d = GradeDie::new(&GRADES);
let r = d.rotate(0);

assert_eq!(r.value(), &'A');
}

#[test]
fn slice_rotate_mut_none() {
let mut d = GradeDie::new(&GRADES);
d.rotate_mut(0);

assert_eq!(d.value(), &'A');
}

#[test]
fn slice_rotate_next() {
let d = GradeDie::new(&GRADES);
let r = d.rotate(1);

assert_eq!(r.value(), &'B');
}

#[test]
fn numeric_die_rotate_next_wrap() {
let d = GradeDie::with_position(&GRADES, 4);
let r = d.rotate(1);

assert_eq!(r.value(), &'A');
}

#[test]
fn numeric_die_rotate_back() {
let d = GradeDie::with_position(&GRADES, 1);
let r = d.rotate(-1);

assert_eq!(r.value(), &'A');
}

#[test]
fn numeric_die_rotate_back_wrap() {
let d = GradeDie::new(&GRADES);
let r = d.rotate(-1);

assert_eq!(r.value(), &'F');
}

#[test]
fn numeric_die_rotate_next_mut() {
let mut d = GradeDie::new(&GRADES);
d.rotate_mut(1);

assert_eq!(d.value(), &'B');
}

#[test]
fn numeric_die_rotate_next_mut_wrap() {
let mut d = GradeDie::with_position(&GRADES, 4);
d.rotate_mut(1);

assert_eq!(d.value(), &'A');
}

#[test]
fn numeric_die_rotate_back_mut() {
let mut d = GradeDie::with_position(&GRADES, 1);
d.rotate_mut(-1);

assert_eq!(d.value(), &'A');
}

#[test]
fn numeric_die_rotate_back_mut_wrap() {
let mut d = GradeDie::new(&GRADES);
d.rotate_mut(-1);

assert_eq!(d.value(), &'F');
}
}
Loading

0 comments on commit 8bc6115

Please sign in to comment.