Apply some clippy lints

This commit is contained in:
expectocode 2020-01-01 17:01:01 +00:00
parent 7e91144ae2
commit a5da537bce
3 changed files with 42 additions and 45 deletions

View File

@ -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<Rectangle> {
@ -161,32 +161,28 @@ 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()) {
} 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();
}
}
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()) {
} 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());
}
}
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]

View File

@ -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);
}

View File

@ -52,8 +52,8 @@ pub fn get_all_intersecting<T>(rects: T, rectangle: &Rectangle) -> impl Iterator
where
T: Iterator<Item = Rectangle>,
{
// 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<T>(mut rects: T) -> Option<Rectangle>
where
T: Iterator<Item = Rectangle>,
{
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<Rectangle>, rect: Rectangle) -> Option<Rectangle> {
match acc {
@ -107,7 +107,7 @@ where
}
}
rects.fold(first, intersect_helper)
rects.fold(Some(first), intersect_helper)
}
#[cfg(test)]