diff --git a/src/layout/rect.rs b/src/layout/rect.rs index 89a0b191..6a9fea30 100644 --- a/src/layout/rect.rs +++ b/src/layout/rect.rs @@ -155,8 +155,8 @@ impl Rect { Rect { x: x1, y: y1, - width: x2 - x1, - height: y2 - y1, + width: x2.saturating_sub(x1), + height: y2.saturating_sub(y1), } } @@ -280,6 +280,14 @@ mod tests { ); } + #[test] + fn intersection_underflow() { + assert_eq!( + Rect::new(1, 1, 2, 2).intersection(Rect::new(4, 4, 2, 2)), + Rect::new(4, 4, 0, 0) + ); + } + #[test] fn intersects() { assert!(Rect::new(1, 2, 3, 4).intersects(Rect::new(2, 3, 4, 5)));