Merge pull request #157 from corwinkuiper/number-split

Split number to own crate
This commit is contained in:
Gwilym Kuiper 2022-01-12 22:29:06 +00:00 committed by GitHub
commit 727e9829d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 311 additions and 107 deletions

View file

@ -0,0 +1,20 @@
name: Publish agb-fixnum
on:
push:
tags:
- agb-fixnum/v*
jobs:
build:
runs-on: ubuntu-20.04
steps:
- name: Install build tools
run: sudo apt-get update && sudo apt-get install build-essential binutils-arm-none-eabi -y
- name: Check out repository
uses: actions/checkout@v2
- name: Login to crates.io
run: cargo login ${{ secrets.CRATE_API }}
- name: Publish agb-fixnum
run: cargo publish
working-directory: ./agb-fixnum

View file

@ -46,6 +46,12 @@ jobs:
- name: Run Clippy on agb macros
working-directory: agb-macros
run: cargo clippy --verbose
- name: Run Clippy on agb fixnum
working-directory: agb-fixnum
run: cargo clippy --verbose
- name: Run Tests for fixnum
working-directory: agb-fixnum
run: cargo test --verbose
- name: Run Tests for agb in debug mode
working-directory: agb
run: cargo test --verbose

130
agb-fixnum/Cargo.lock generated Normal file
View file

@ -0,0 +1,130 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "agb_fixnum"
version = "0.1.0"
dependencies = [
"agb_macros",
]
[[package]]
name = "agb_macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"rand",
"syn",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "syn"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

9
agb-fixnum/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "agb_fixnum"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
agb_macros = { version = "0.1.0", path = "../agb-macros" }

View file

@ -1,3 +1,5 @@
#![no_std]
use core::{
cmp::{Eq, Ord, PartialEq, PartialOrd},
fmt::{Debug, Display},
@ -10,7 +12,7 @@ use core::{
#[macro_export]
macro_rules! num {
($value:literal) => {{
$crate::number::Num::new_from_parts(agb_macros::num!($value))
$crate::Num::new_from_parts(agb_macros::num!($value))
}};
}
@ -471,34 +473,6 @@ where
}
}
#[cfg(feature = "alloc")]
#[cfg(test)]
mod formatting_tests {
use super::Num;
use alloc::format;
#[test_case]
fn formats_whole_numbers_correctly(_gba: &mut crate::Gba) {
let a = Num::<i32, 8>::new(-4i32);
assert_eq!(format!("{}", a), "-4");
}
#[test_case]
fn formats_fractions_correctly(_gba: &mut crate::Gba) {
let a = Num::<i32, 8>::new(5);
let two = Num::<i32, 8>::new(4);
let minus_one = Num::<i32, 8>::new(-1);
let b: Num<i32, 8> = a / two;
let c: Num<i32, 8> = b * minus_one;
assert_eq!(b + c, 0.into());
assert_eq!(format!("{}", b), "1.25");
assert_eq!(format!("{}", c), "-1.25");
}
}
impl<T: Number> AddAssign<Self> for Vector2D<T> {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
@ -691,33 +665,6 @@ impl<T: FixedWidthUnsignedInteger> Rect<T> {
}
}
#[cfg(test)]
#[test_case]
fn test_rect_iter(_gba: &mut crate::Gba) {
let rect: Rect<i32> = Rect::new((5_i32, 5_i32).into(), (3_i32, 3_i32).into());
assert_eq!(
rect.iter().collect::<alloc::vec::Vec<_>>(),
&[
(5, 5),
(6, 5),
(7, 5),
(8, 5),
(5, 6),
(6, 6),
(7, 6),
(8, 6),
(5, 7),
(6, 7),
(7, 7),
(8, 7),
(5, 8),
(6, 8),
(7, 8),
(8, 8),
]
);
}
impl<T: Number> Vector2D<T> {
pub fn new(x: T, y: T) -> Self {
Vector2D { x, y }
@ -746,18 +693,43 @@ impl<T: Number> Vector2D<T> {
#[cfg(test)]
mod tests {
use super::*;
#[test_case]
fn sqrt(_gba: &mut crate::Gba) {
extern crate alloc;
use super::*;
use alloc::format;
#[test]
fn formats_whole_numbers_correctly() {
let a = Num::<i32, 8>::new(-4i32);
assert_eq!(format!("{}", a), "-4");
}
#[test]
fn formats_fractions_correctly() {
let a = Num::<i32, 8>::new(5);
let two = Num::<i32, 8>::new(4);
let minus_one = Num::<i32, 8>::new(-1);
let b: Num<i32, 8> = a / two;
let c: Num<i32, 8> = b * minus_one;
assert_eq!(b + c, 0.into());
assert_eq!(format!("{}", b), "1.25");
assert_eq!(format!("{}", c), "-1.25");
}
#[test]
fn sqrt() {
for x in 1..1024 {
let n: Num<i32, 8> = Num::new(x * x);
assert_eq!(n.sqrt(), x.into());
}
}
#[test_case]
fn test_macro_conversion(_gba: &mut crate::Gba) {
#[test]
fn test_macro_conversion() {
fn test_positive<A: FixedWidthUnsignedInteger, const B: usize>() {
let a: Num<A, B> = num!(1.5);
let one = A::one() << B;
@ -797,8 +769,8 @@ mod tests {
test_base::<11>();
}
#[test_case]
fn test_numbers(_gba: &mut crate::Gba) {
#[test]
fn test_numbers() {
// test addition
let n: Num<i32, 8> = 1.into();
assert_eq!(n + 2, 3.into(), "testing that 1 + 2 == 3");
@ -815,8 +787,8 @@ mod tests {
assert_ne!(n, p, "testing that 30 != 3");
}
#[test_case]
fn test_division_by_one(_gba: &mut crate::Gba) {
#[test]
fn test_division_by_one() {
let one: Num<i32, 8> = 1.into();
for i in -40..40 {
@ -825,8 +797,8 @@ mod tests {
}
}
#[test_case]
fn test_division_and_multiplication_by_16(_gba: &mut crate::Gba) {
#[test]
fn test_division_and_multiplication_by_16() {
let sixteen: Num<i32, 8> = 16.into();
for i in -40..40 {
@ -837,8 +809,8 @@ mod tests {
}
}
#[test_case]
fn test_division_by_2_and_15(_gba: &mut crate::Gba) {
#[test]
fn test_division_by_2_and_15() {
let two: Num<i32, 8> = 2.into();
let fifteen: Num<i32, 8> = 15.into();
let thirty: Num<i32, 8> = 30.into();
@ -851,8 +823,8 @@ mod tests {
}
}
#[test_case]
fn test_change_base(_gba: &mut crate::Gba) {
#[test]
fn test_change_base() {
let two: Num<i32, 9> = 2.into();
let three: Num<i32, 4> = 3.into();
@ -860,8 +832,8 @@ mod tests {
assert_eq!(three + two.change_base(), 5.into());
}
#[test_case]
fn test_rem_returns_sensible_values_for_integers(_gba: &mut crate::Gba) {
#[test]
fn test_rem_returns_sensible_values_for_integers() {
for i in -50..50 {
for j in -50..50 {
if j == 0 {
@ -876,8 +848,8 @@ mod tests {
}
}
#[test_case]
fn test_rem_returns_sensible_values_for_non_integers(_gba: &mut crate::Gba) {
#[test]
fn test_rem_returns_sensible_values_for_non_integers() {
let one: Num<i32, 8> = 1.into();
let third = one / 3;
@ -900,8 +872,8 @@ mod tests {
}
}
#[test_case]
fn test_rem_euclid_is_always_positive_and_sensible(_gba: &mut crate::Gba) {
#[test]
fn test_rem_euclid_is_always_positive_and_sensible() {
let one: Num<i32, 8> = 1.into();
let third = one / 3;
@ -920,8 +892,8 @@ mod tests {
}
}
#[test_case]
fn test_vector_multiplication_and_division(_gba: &mut crate::Gba) {
#[test]
fn test_vector_multiplication_and_division() {
let a: Vector2D<i32> = (1, 2).into();
let b = a * 5;
let c = b / 5;
@ -929,8 +901,8 @@ mod tests {
assert_eq!(a, c);
}
#[test_case]
fn magnitude_accuracy(_gba: &mut crate::Gba) {
#[test]
fn magnitude_accuracy() {
let n: Vector2D<Num<i32, 16>> = (3, 4).into();
assert!((n.magnitude() - 5).abs() < num!(0.1));
@ -938,8 +910,8 @@ mod tests {
assert!((n.magnitude() - 5).abs() < num!(0.1));
}
#[test_case]
fn test_vector_changing(_gba: &mut crate::Gba) {
#[test]
fn test_vector_changing() {
let v1: Vector2D<FixedNum<8>> = Vector2D::new(1.into(), 2.into());
let v2 = v1.trunc();
@ -947,4 +919,30 @@ mod tests {
assert_eq!(v1 + v1, (v2 + v2).into());
}
#[test]
fn test_rect_iter() {
let rect: Rect<i32> = Rect::new((5_i32, 5_i32).into(), (3_i32, 3_i32).into());
assert_eq!(
rect.iter().collect::<alloc::vec::Vec<_>>(),
&[
(5, 5),
(6, 5),
(7, 5),
(8, 5),
(5, 6),
(6, 6),
(7, 6),
(8, 6),
(5, 7),
(6, 7),
(7, 7),
(8, 7),
(5, 8),
(6, 8),
(7, 8),
(8, 8),
]
);
}
}

8
agb/Cargo.lock generated
View file

@ -12,12 +12,20 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
name = "agb"
version = "0.8.0"
dependencies = [
"agb_fixnum",
"agb_image_converter",
"agb_macros",
"agb_sound_converter",
"bitflags",
]
[[package]]
name = "agb_fixnum"
version = "0.1.0"
dependencies = [
"agb_macros",
]
[[package]]
name = "agb_image_converter"
version = "0.6.0"

View file

@ -24,6 +24,7 @@ bitflags = "1.3"
agb_image_converter = { version = "0.6.0", path = "../agb-image-converter" }
agb_sound_converter = { version = "0.1.0", path = "../agb-sound-converter" }
agb_macros = { version = "0.1.0", path = "../agb-macros" }
agb_fixnum = { version = "0.1.0", path = "../agb-fixnum" }
[package.metadata.docs.rs]
default-target = "thumbv6m-none-eabi"

View file

@ -1,8 +1,8 @@
#![no_std]
#![no_main]
use agb::fixnum::Num;
use agb::input::{Button, ButtonController, Tri};
use agb::number::Num;
use agb::sound::mixer::SoundChannel;
use agb::{include_wav, Gba};

View file

@ -3,8 +3,8 @@
use agb::{
display::example_logo,
fixnum::FixedNum,
interrupt::{Interrupt, Mutex},
number::FixedNum,
};
struct BackCosines {

View file

@ -1,8 +1,8 @@
use core::ops::Index;
use crate::{
fixnum::{Rect, Vector2D},
memory_mapped::{MemoryMapped, MemoryMapped1DArray},
number::{Rect, Vector2D},
};
use super::{

View file

@ -2,8 +2,8 @@ use core::cell::RefCell;
use super::{palette16, Priority, DISPLAY_CONTROL};
use crate::bitarray::Bitarray;
use crate::fixnum::Vector2D;
use crate::memory_mapped::MemoryMapped1DArray;
use crate::number::Vector2D;
type AffineLoan<'a> = crate::arena::Loan<'a, 32>;
type AffineArena = crate::arena::Arena<32>;

View file

@ -152,7 +152,7 @@ mod memory_mapped;
/// Implements logging to the mgba emulator.
pub mod mgba;
/// Implementation of fixnums for working with non-integer values.
pub mod number;
pub use agb_fixnum as fixnum;
mod single;
/// Implements sound output.
pub mod sound;

View file

@ -4,7 +4,7 @@ mod sw_mixer;
pub use sw_mixer::ChannelId;
pub use sw_mixer::Mixer;
use crate::number::Num;
use crate::fixnum::Num;
use crate::timer::Timer;
#[non_exhaustive]

View file

@ -1,7 +1,7 @@
use super::hw;
use super::hw::LeftOrRight;
use super::{SoundChannel, SoundPriority};
use crate::number::Num;
use crate::fixnum::Num;
use crate::timer::Timer;
// Defined in mixer.s

View file

@ -1,7 +1,7 @@
use core::arch::asm;
use crate::display::object::AffineMatrixAttributes;
use crate::number::Num;
use crate::fixnum::Num;
#[allow(non_snake_case)]

View file

@ -12,12 +12,20 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
name = "agb"
version = "0.8.0"
dependencies = [
"agb_fixnum",
"agb_image_converter",
"agb_macros",
"agb_sound_converter",
"bitflags",
]
[[package]]
name = "agb_fixnum"
version = "0.1.0"
dependencies = [
"agb_macros",
]
[[package]]
name = "agb_image_converter"
version = "0.6.0"

View file

@ -12,12 +12,20 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
name = "agb"
version = "0.8.0"
dependencies = [
"agb_fixnum",
"agb_image_converter",
"agb_macros",
"agb_sound_converter",
"bitflags",
]
[[package]]
name = "agb_fixnum"
version = "0.1.0"
dependencies = [
"agb_macros",
]
[[package]]
name = "agb_image_converter"
version = "0.6.0"

View file

@ -170,7 +170,7 @@ mod tiled_export {
&mut writer,
r#"
use crate::Level;
use agb::number::Vector2D;
use agb::fixnum::Vector2D;
pub const fn get_level() -> Level {{
Level {{

View file

@ -1,7 +1,7 @@
use super::{object_tiles, sfx::SfxPlayer, Entity, FixedNumberType, HatState, Level};
use agb::{
display::object::{ObjectControl, Size},
number::Vector2D,
fixnum::Vector2D,
};
enum UpdateState {

View file

@ -109,8 +109,8 @@ use agb::{
object::{ObjectControl, ObjectStandard, Size},
Priority, HEIGHT, WIDTH,
},
fixnum::{FixedNum, Vector2D},
input::{self, Button, ButtonController},
number::{FixedNum, Vector2D},
};
type FixedNumberType = FixedNum<10>;

View file

@ -12,12 +12,20 @@ checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
name = "agb"
version = "0.8.0"
dependencies = [
"agb_fixnum",
"agb_image_converter",
"agb_macros",
"agb_sound_converter",
"bitflags",
]
[[package]]
name = "agb_fixnum"
version = "0.1.0"
dependencies = [
"agb_macros",
]
[[package]]
name = "agb_image_converter"
version = "0.6.0"

View file

@ -16,8 +16,8 @@ use agb::{
object::{ObjectControl, ObjectStandard},
Priority, HEIGHT, WIDTH,
},
fixnum::{FixedNum, Rect, Vector2D},
input::{Button, ButtonController, Tri},
number::{FixedNum, Rect, Vector2D},
};
use generational_arena::Arena;

View file

@ -1,5 +1,5 @@
use super::rng::get_random;
use agb::number::Num;
use agb::fixnum::Num;
use agb::sound::mixer::{ChannelId, Mixer, SoundChannel};
const BAT_DEATH: &[u8] = agb::include_wav!("sfx/BatDeath.wav");

View file

@ -47,7 +47,10 @@ case "$PROJECT" in
DIRECTORY="mgba-test-runner"
TAGNAME="mgba-test-runner/v$VERSION"
;;
agb-fixnum)
DIRECTORY="agb-fixnum"
TAGNAME="agb-fixnum/v$VERSION"
;;
*)
echo "Unknown project name $PROJECT"
exit 1
@ -78,25 +81,30 @@ if [ "$PROJECT" = "agb" ]; then
sed -i -e "s/^agb = \".*\"/agb = \"$VERSION\"/" template/Cargo.toml
git add template/Cargo.toml
for EXAMPLE_DIR in examples/*/ book/games/*/; do
for EXAMPLE_TOML_FILE in examples/*/Cargo.toml book/games/*/Cargo.toml; do
EXAMPLE_DIR=$(dirname "$EXAMPLE_TOML_FILE")
sed -E -i -e "/agb =/ s/version = \"[^\"]+\"/version = \"$VERSION\"/" "$EXAMPLE_DIR/Cargo.toml"
(cd "$EXAMPLE_DIR" && cargo update)
git add "$EXAMPLE_DIR"/{Cargo.toml,Cargo.lock}
done
else
PROJECT_NAME_WITH_UNDERSCORES=$(echo -n "$PROJECT" | tr - _)
sed -i -E -e "s/($PROJECT_NAME_WITH_UNDERSCORES = .*version = \")[^\"]+(\".*)/\1$VERSION\2/" agb/Cargo.toml
(cd agb && cargo update)
git add agb/Cargo.toml agb/Cargo.lock
for CARGO_TOML_FILE in agb-*/Cargo.toml agb/Cargo.toml examples/*/Cargo.toml book/games/*/Cargo.toml; do
sed -i -E -e "s/($PROJECT_NAME_WITH_UNDERSCORES = .*version = \")[^\"]+(\".*)/\1$VERSION\2/" "$CARGO_TOML_FILE"
(cd "$(dirname "$CARGO_TOML_FILE")" && cargo generate-lockfile)
git add "$CARGO_TOML_FILE" "${CARGO_TOML_FILE/.toml/.lock}"
done
fi
# Sanity check to make sure the build works
(cd agb && cargo test)
(cd agb-image-converter && cargo test)
(cd agb-sound-converter && cargo test)
(cd agb-macros && cargo test)
for EXAMPLE_DIR in examples/*/; do
for CARGO_TOML_FILE in agb-*/Cargo.toml agb/Cargo.toml; do
(cd "$(dirname "$CARGO_TOML_FILE")" && cargo test)
done
for EXAMPLE_TOML_FILE in examples/*/Cargo.toml book/games/*/Cargo.toml; do
EXAMPLE_DIR=$(dirname "$EXAMPLE_TOML_FILE")
(cd "$EXAMPLE_DIR" && cargo check --release)
done