split to own crate

This commit is contained in:
Corwin Kuiper 2022-01-07 16:59:19 +00:00
parent 6bf197bb3a
commit 5b7e1a760d
6 changed files with 230 additions and 84 deletions

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

@ -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 number;
mod single;
/// Implements sound output.
pub mod sound;