diff --git a/src/geometry.rs b/src/geometry.rs index 36758ad..afb2cf6 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -22,35 +22,35 @@ impl Point { Self { x: 0, y: 0 } } - pub const fn with_x(&self, x: u32) -> Self { + pub const fn with_x(self, x: u32) -> Self { Self { x, y: self.y } } - pub const fn with_y(&self, y: u32) -> Self { + pub const fn with_y(self, y: u32) -> Self { Self { x: self.x, y } } - pub const fn x(&self) -> u32 { + pub const fn x(self) -> u32 { self.x } - pub const fn y(&self) -> u32 { + pub const fn y(self) -> u32 { self.y } - pub const fn left_of(&self, other: &Self) -> bool { + pub const fn left_of(self, other: Self) -> bool { self.x < other.x } - pub const fn right_of(&self, other: &Self) -> bool { + pub const fn right_of(self, other: Self) -> bool { self.x > other.x } - pub const fn above(&self, other: &Self) -> bool { + pub const fn above(self, other: Self) -> bool { self.y < other.y } - pub const fn below(&self, other: &Self) -> bool { + pub const fn below(self, other: Self) -> bool { self.y > other.y } } @@ -147,10 +147,10 @@ impl Rectangle { } pub fn intersects(&self, other: &Rectangle) -> bool { - !(self.top_left().right_of(&other.top_right()) - || self.top_left().below(&other.bottom_left()) - || self.top_right().left_of(&other.top_left()) - || self.bottom_left().above(&other.top_left())) + !(self.top_left().right_of(other.top_right()) + || self.top_left().below(other.bottom_left()) + || self.top_right().left_of(other.top_left()) + || self.bottom_left().above(other.top_left())) } pub fn intersection(&self, other: &Rectangle) -> Option { @@ -161,31 +161,27 @@ impl Rectangle { // idk wtf this is lol let in_top_left; let in_bottom_right; - if self.top_left().right_of(&other.top_left()) { - if self.top_left().below(&other.top_left()) { + if self.top_left().right_of(other.top_left()) { + if self.top_left().below(other.top_left()) { in_top_left = self.top_left(); } else { in_top_left = Point::new(self.top_left().x(), other.top_left().y()); } + } else if self.top_left().below(other.top_left()) { + in_top_left = Point::new(other.top_left().x(), self.top_left().y()); } else { - if self.top_left().below(&other.top_left()) { - in_top_left = Point::new(other.top_left().x(), self.top_left().y()); - } else { - in_top_left = other.top_left(); - } + in_top_left = other.top_left(); } - if self.bottom_right().left_of(&other.bottom_right()) { - if self.bottom_right().below(&other.bottom_right()) { + if self.bottom_right().left_of(other.bottom_right()) { + if self.bottom_right().below(other.bottom_right()) { in_bottom_right = Point::new(self.bottom_right().x(), other.bottom_right().y()); } else { in_bottom_right = self.bottom_right(); } + } else if self.bottom_right().below(other.bottom_right()) { + in_bottom_right = other.bottom_right(); } else { - if self.bottom_right().below(&other.bottom_right()) { - in_bottom_right = other.bottom_right(); - } else { - in_bottom_right = Point::new(other.bottom_right().x(), self.bottom_right().y()); - } + in_bottom_right = Point::new(other.bottom_right().x(), self.bottom_right().y()); } Some(Rectangle::from_corners(in_top_left, in_bottom_right)) } @@ -217,28 +213,28 @@ mod tests { #[test] fn test_point_left_of() { - assert!(Point::new(10, 20).left_of(&Point::new(20, 20))); - assert!(!Point::new(10, 11).left_of(&Point::new(10, 11))); - assert!(!Point::new(10, 11).left_of(&Point::new(8, 11))); + assert!(Point::new(10, 20).left_of(Point::new(20, 20))); + assert!(!Point::new(10, 11).left_of(Point::new(10, 11))); + assert!(!Point::new(10, 11).left_of(Point::new(8, 11))); } #[test] fn test_point_right_of() { - assert!(Point::new(10, 20).right_of(&Point::new(0, 20))); - assert!(!Point::new(10, 11).right_of(&Point::new(10, 11))); - assert!(!Point::new(10, 11).right_of(&Point::new(12, 11))); + assert!(Point::new(10, 20).right_of(Point::new(0, 20))); + assert!(!Point::new(10, 11).right_of(Point::new(10, 11))); + assert!(!Point::new(10, 11).right_of(Point::new(12, 11))); } #[test] fn test_point_above() { - assert!(Point::new(1, 1).above(&Point::new(1, 2))); - assert!(!Point::new(10, 11).above(&Point::origin())); + assert!(Point::new(1, 1).above(Point::new(1, 2))); + assert!(!Point::new(10, 11).above(Point::origin())); } #[test] fn test_point_below() { - assert!(Point::new(1, 1).below(&Point::new(1, 0))); - assert!(!Point::new(10, 11).below(&Point::new(12, 11))); + assert!(Point::new(1, 1).below(Point::new(1, 0))); + assert!(!Point::new(10, 11).below(Point::new(12, 11))); } #[test] diff --git a/src/main.rs b/src/main.rs index 09afb41..e79dfd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ fn main() { Rectangle::from_corner_width_height(Point::new(2010, 2100), 1, 1), ]; + // Clone here so that we can continue to use the original if needed later. let r: Vec<_> = stream_algos::translate(various.clone().drain(0..), Point::new(1, 2)).collect(); dbg!(&r); } diff --git a/src/stream_algos.rs b/src/stream_algos.rs index 5ce94d6..d6c0dfe 100644 --- a/src/stream_algos.rs +++ b/src/stream_algos.rs @@ -52,8 +52,8 @@ pub fn get_all_intersecting(rects: T, rectangle: &Rectangle) -> impl Iterator where T: Iterator, { - // We need to satisfy the borrow checker here. - let rectangle = rectangle.clone(); + // We need to satisfy the borrow checker here, make a copy. + let rectangle = *rectangle; rects.filter(move |r| r.intersects(&rectangle)) } @@ -93,12 +93,12 @@ pub fn intersect_all(mut rects: T) -> Option where T: Iterator, { - let first = rects.next(); - - if let None = first { - // Can't do intersection if no items. - return None; - } + // The question mark operator is equivalent to: + // let first = match rects.next() { + // None => return None, + // Some(x) => x, + // }; + let first = rects.next()?; fn intersect_helper(acc: Option, rect: Rectangle) -> Option { match acc { @@ -107,7 +107,7 @@ where } } - rects.fold(first, intersect_helper) + rects.fold(Some(first), intersect_helper) } #[cfg(test)]