From 6d829bd74192900110a4702a54e43a4f6fb83540 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Tue, 4 Jan 2022 00:10:51 +0000 Subject: [PATCH 1/2] remove the step by feature should have test to make sure it does what I want it to do --- agb/src/lib.rs | 1 - agb/src/number.rs | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/agb/src/lib.rs b/agb/src/lib.rs index 3b973565..c806729c 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -7,7 +7,6 @@ #![feature(alloc_error_handler)] #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] -#![feature(step_trait)] //! # agb //! `agb` is a library for making games on the Game Boy Advance using the Rust //! programming language. It attempts to be a high level abstraction over the diff --git a/agb/src/number.rs b/agb/src/number.rs index 0524a11e..92ac260c 100644 --- a/agb/src/number.rs +++ b/agb/src/number.rs @@ -862,15 +862,22 @@ impl Rect { } } -impl Rect { +impl Rect { pub fn iter(self) -> impl Iterator { - (self.position.x..=(self.position.x + self.size.x)) - .into_iter() - .flat_map(move |x| { - (self.position.y..=(self.position.y + self.size.y)) - .into_iter() - .map(move |y| (x, y)) - }) + let mut x = self.position.x - T::one(); + let mut y = self.position.y; + core::iter::from_fn(move || { + x = x + T::one(); + if x > self.position.x + self.size.x { + x = self.position.x; + y = y + T::one(); + if y > self.position.y + self.size.y { + return None; + } + } + + Some((x, y)) + }) } } From 0e5591686594fc0e843c57d76f74dfaa9730312e Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Tue, 4 Jan 2022 00:31:27 +0000 Subject: [PATCH 2/2] add test --- agb/src/number.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/agb/src/number.rs b/agb/src/number.rs index 92ac260c..5983a065 100644 --- a/agb/src/number.rs +++ b/agb/src/number.rs @@ -881,6 +881,33 @@ impl Rect { } } +#[cfg(test)] +#[test_case] +fn test_rect_iter(_gba: &mut crate::Gba) { + let rect: Rect = Rect::new((5_i32, 5_i32).into(), (3_i32, 3_i32).into()); + assert_eq!( + rect.iter().collect::>(), + &[ + (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 Vector2D { pub fn new(x: T, y: T) -> Self { Vector2D { x, y }