mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
Implement num::Num for fixnum::Num
This commit is contained in:
parent
eaf5a83076
commit
cfd3135d76
|
@ -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 }
|
||||
|
|
|
@ -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() {
|
||||
|
|
36
book/games/pong/Cargo.lock
generated
36
book/games/pong/Cargo.lock
generated
|
@ -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"
|
||||
|
|
36
examples/amplitude/Cargo.lock
generated
36
examples/amplitude/Cargo.lock
generated
|
@ -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"
|
||||
|
|
36
examples/combo/Cargo.lock
generated
36
examples/combo/Cargo.lock
generated
|
@ -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"
|
||||
|
|
36
examples/hyperspace-roll/Cargo.lock
generated
36
examples/hyperspace-roll/Cargo.lock
generated
|
@ -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"
|
||||
|
|
36
examples/the-dungeon-puzzlers-lament/Cargo.lock
generated
36
examples/the-dungeon-puzzlers-lament/Cargo.lock
generated
|
@ -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"
|
||||
|
|
36
examples/the-hat-chooses-the-wizard/Cargo.lock
generated
36
examples/the-hat-chooses-the-wizard/Cargo.lock
generated
|
@ -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"
|
||||
|
|
36
examples/the-purple-night/Cargo.lock
generated
36
examples/the-purple-night/Cargo.lock
generated
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue