Implement num traits for fixnum (#568)

from_str_radix is implemented incredibly lazily, but should hopefully be
okay since you shouldn't be putting that in the critical path anyway :)

- [x] Changelog updated
This commit is contained in:
Gwilym Inzani 2024-02-21 14:37:29 +00:00 committed by GitHub
commit 2fa33ede16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 312 additions and 7 deletions

View file

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Replaced `.show()` and `.hide()` with `.set_visible()`in `RegularMap`, `AffineMap` and `InfiniteScrolledMap`.
- Added `.hflip()`, `.vflip()`, `.priority()`, `.position()` to `ObjectUnmanaged` and `Object`.
- Expermental and incomplete support for MIDI files with agb-tracker.
- Fixnum now implements [`num::Num`](https://docs.rs/num/0.4/num/trait.Num.html) from the [`num`](https://crates.io/crates/num) crate.
## [0.18.1] - 2024/02/06

View file

@ -8,3 +8,4 @@ repository = "https://github.com/agbrs/agb"
[dependencies]
agb_macros = { version = "0.18.1", path = "../agb-macros" }
num = { version = "0.4", default-features = false }

View file

@ -170,6 +170,39 @@ fixed_width_signed_integer_impl!(i32);
#[repr(transparent)]
pub struct Num<I: FixedWidthUnsignedInteger, const N: usize>(I);
impl<I: FixedWidthUnsignedInteger, const N: usize> num::Zero for Num<I, N> {
fn zero() -> Self {
Self::new(I::zero())
}
fn is_zero(&self) -> bool {
self.to_raw() == I::zero()
}
}
impl<I: FixedWidthUnsignedInteger, const N: usize> num::One for Num<I, N> {
fn one() -> Self {
Self::new(I::one())
}
}
impl<I: FixedWidthUnsignedInteger + num::Num, const N: usize> num::Num for Num<I, N> {
type FromStrRadixErr = <f64 as num::Num>::FromStrRadixErr;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
// for some reason, if I don't have this it's an error, and if I do it is unused
#[allow(unused_imports)]
use num::traits::float::FloatCore;
let v: f64 = f64::from_str_radix(str, radix)?;
let integer = v.trunc();
let fractional = v.fract() * (1u64 << 30) as f64;
Ok(Self::new_from_parts((integer as i32, fractional as i32)))
}
}
/// An often convenient representation for the Game Boy Advance using word sized
/// internal representation for maximum efficiency
pub type FixedNum<const N: usize> = Num<i32, N>;
@ -1109,6 +1142,7 @@ mod tests {
use super::*;
use alloc::format;
use num::Num as _;
#[test]
fn formats_whole_numbers_correctly() {
@ -1426,6 +1460,37 @@ mod tests {
);
}
#[test]
fn test_str_radix() {
use alloc::string::ToString;
macro_rules! str_radix_test {
($val:tt) => {
assert_eq!(
Num::<i32, 8>::from_str_radix(stringify!($val), 10).unwrap(),
num!($val)
);
};
(-$val:tt) => {
assert_eq!(
Num::<i32, 8>::from_str_radix(&("-".to_string() + stringify!($val)), 10)
.unwrap(),
num!(-$val)
);
};
}
str_radix_test!(0.1);
str_radix_test!(0.100000);
str_radix_test!(0000.1000);
str_radix_test!(000000.100000);
str_radix_test!(000000.1);
str_radix_test!(138.229);
str_radix_test!(-138.229);
str_radix_test!(-1321.229231);
}
#[cfg(not(debug_assertions))]
#[test]
fn test_all_multiplies() {

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -253,7 +254,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -297,6 +298,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -328,6 +351,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -260,7 +261,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -304,6 +305,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -335,6 +358,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -393,7 +394,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -501,6 +502,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -532,6 +555,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -260,7 +261,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -304,6 +305,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -335,6 +358,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -349,7 +350,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -439,6 +440,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -470,6 +493,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -253,7 +254,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -303,6 +304,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -334,6 +357,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"

View file

@ -33,6 +33,7 @@ name = "agb_fixnum"
version = "0.18.1"
dependencies = [
"agb_macros",
"num",
]
[[package]]
@ -271,7 +272,7 @@ dependencies = [
"byteorder",
"color_quant",
"num-iter",
"num-rational",
"num-rational 0.3.2",
"num-traits",
"png",
]
@ -327,6 +328,28 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0f889fb66f7acdf83442c35775764b51fed3c606ab9cee51500dbde2cf528ca"
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-complex",
"num-integer",
"num-iter",
"num-rational 0.4.1",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -358,6 +381,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.18"