diff --git a/agb/src/number.rs b/agb/src/number.rs index 2e91c6a9..0524a11e 100644 --- a/agb/src/number.rs +++ b/agb/src/number.rs @@ -59,6 +59,7 @@ pub trait FixedWidthUnsignedInteger: } pub trait FixedWidthSignedInteger: FixedWidthUnsignedInteger + Neg { + #[must_use] fn fixed_abs(self) -> Self; } @@ -276,6 +277,7 @@ impl Num { self.0 / (I::one() << N) } + #[must_use] pub fn rem_euclid(self, rhs: Self) -> Self { let r = self % rhs; if r < I::zero().into() { @@ -307,6 +309,7 @@ impl Num { } impl Num { + #[must_use] pub fn sqrt(self) -> Self { assert_eq!(N % 2, 0, "N must be even to be able to square root"); assert!(self.0 >= 0, "sqrt is only valid for positive numbers"); @@ -381,12 +384,14 @@ fn test_macro_conversion(_gba: &mut super::Gba) { } impl Num { + #[must_use] pub fn abs(self) -> Self { Num(self.0.fixed_abs()) } /// domain of [0, 1]. /// see https://github.com/tarcieri/micromath/blob/24584465b48ff4e87cffb709c7848664db896b4f/src/float/cos.rs#L226 + #[must_use] pub fn cos(self) -> Self { let one: Self = I::one().into(); let mut x = self; @@ -402,6 +407,7 @@ impl Num { x } + #[must_use] pub fn sin(self) -> Self { let one: Self = I::one().into(); let four: I = 4.into(); @@ -697,12 +703,15 @@ impl SubAssign for Vector2D { } impl Vector2D> { + #[must_use] pub fn trunc(self) -> Vector2D { Vector2D { x: self.x.trunc(), y: self.y.trunc(), } } + + #[must_use] pub fn floor(self) -> Vector2D { Vector2D { x: self.x.floor(), @@ -712,14 +721,17 @@ impl Vector2D> { } impl Vector2D> { + #[must_use] pub fn magnitude_squared(self) -> Num { self.x * self.x + self.y * self.y } + #[must_use] pub fn manhattan_distance(self) -> Num { self.x.abs() + self.y.abs() } + #[must_use] pub fn magnitude(self) -> Num { self.magnitude_squared().sqrt() } @@ -728,6 +740,7 @@ impl Vector2D> { // algorithm https://en.wikipedia.org/wiki/Alpha_max_plus_beta_min_algorithm // this has a maximum error of less than 4% of the true magnitude, probably // depending on the size of your fixed point approximation + #[must_use] pub fn fast_magnitude(self) -> Num { let max = core::cmp::max(self.x, self.y); let min = core::cmp::min(self.x, self.y); @@ -735,10 +748,12 @@ impl Vector2D> { max * num!(0.960433870103) + min * num!(0.397824734759) } + #[must_use] pub fn normalise(self) -> Self { self / self.magnitude() } + #[must_use] pub fn fast_normalise(self) -> Self { self / self.fast_magnitude() } @@ -790,6 +805,7 @@ pub struct Rect { } impl Rect { + #[must_use] pub fn new(position: Vector2D, size: Vector2D) -> Self { Rect { position, size } } @@ -808,7 +824,8 @@ impl Rect { && self.position.y + self.size.y > other.position.y } - pub fn overlapping_rect(&self, other: Rect) -> Rect { + #[must_use] + pub fn overlapping_rect(&self, other: Rect) -> Self { fn max(x: E, y: E) -> E { if x > y { x @@ -849,12 +866,11 @@ impl Rect { pub fn iter(self) -> impl Iterator { (self.position.x..=(self.position.x + self.size.x)) .into_iter() - .map(move |x| { + .flat_map(move |x| { (self.position.y..=(self.position.y + self.size.y)) .into_iter() .map(move |y| (x, y)) }) - .flatten() } } @@ -862,15 +878,20 @@ impl Vector2D { pub fn new(x: T, y: T) -> Self { Vector2D { x, y } } + pub fn get(self) -> (T, T) { (self.x, self.y) } + + #[must_use] pub fn hadamard(self, other: Self) -> Self { Self { x: self.x * other.x, y: self.y * other.y, } } + + #[must_use] pub fn swap(self) -> Self { Self { x: self.y, diff --git a/agb/src/syscall.rs b/agb/src/syscall.rs index 83b44b1c..b4357c58 100644 --- a/agb/src/syscall.rs +++ b/agb/src/syscall.rs @@ -1,3 +1,5 @@ +use core::arch::asm; + use crate::display::object::AffineMatrixAttributes; use crate::number::Num;