Skip to content

Commit 2894075

Browse files
authored
Auto merge of #463 - nical:intersect-non-empty, r=nical
(breaking change) Make intersection return Option<Self> Replaces #462 As I go through the webrender update I realize that returning `Option<NonEmpty<Self>>` adds way more friction than I anticipated. Pretty much all call sites have to be worked around in different ways. #462 helps for at least a good half of the call sites, but there are many other patterns, and I am not running into cases where NonEmpty is adding value, because we end up having to deal with storing something when the intersection is empty (the way webrender's code is shaped). I'm very woriied that `intersection` is just going to be a pain to use by default, with NonEmpty providing more friction than value, so considering 0.21 is just out, I would rather make a early 0.22 now before a significant part of downstream users have had time to update to 0.21, and get rid of the thorn before we are stuck with it. Note that for cases where `Option<NonEmpty<Box2D>>` would have been desirable, we can still write `b.intersection_unchecked(b2).to_non_empty(), which is not too inconvenient even if not the default.
2 parents 95c3bf9 + 32c352a commit 2894075

File tree

6 files changed

+26
-268
lines changed

6 files changed

+26
-268
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "euclid"
3-
version = "0.21.0"
3+
version = "0.22.0"
44
authors = ["The Servo Project Developers"]
55
edition = "2018"
66
description = "Geometry primitives"

src/box2d.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use super::UnknownUnit;
1111
use crate::approxord::{max, min};
12-
use crate::nonempty::NonEmpty;
1312
use crate::num::*;
1413
use crate::point::{point2, Point2D};
1514
use crate::rect::Rect;
@@ -162,18 +161,24 @@ where
162161
T: Copy + PartialOrd,
163162
{
164163
#[inline]
165-
pub fn to_non_empty(&self) -> Option<NonEmpty<Self>> {
164+
pub fn to_non_empty(&self) -> Option<Self> {
166165
if self.is_empty() {
167166
return None;
168167
}
169168

170-
Some(NonEmpty(*self))
169+
Some(*self)
171170
}
172171

173172
/// Computes the intersection of two boxes, returning `None` if the boxes do not intersect.
174173
#[inline]
175-
pub fn intersection(&self, other: &Self) -> Option<NonEmpty<Self>> {
176-
self.intersection_unchecked(other).to_non_empty()
174+
pub fn intersection(&self, other: &Self) -> Option<Self> {
175+
let b = self.intersection_unchecked(other);
176+
177+
if b.is_empty() {
178+
return None;
179+
}
180+
181+
Some(b)
177182
}
178183

179184
/// Computes the intersection of two boxes without check whether they do intersect.

src/box3d.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use super::UnknownUnit;
1111
use crate::approxord::{max, min};
12-
use crate::nonempty::NonEmpty;
1312
use crate::num::*;
1413
use crate::point::{point3, Point3D};
1514
use crate::scale::Scale;
@@ -140,17 +139,23 @@ where
140139
T: Copy + PartialOrd,
141140
{
142141
#[inline]
143-
pub fn to_non_empty(&self) -> Option<NonEmpty<Self>> {
142+
pub fn to_non_empty(&self) -> Option<Self> {
144143
if self.is_empty() {
145144
return None;
146145
}
147146

148-
Some(NonEmpty(*self))
147+
Some(*self)
149148
}
150149

151150
#[inline]
152-
pub fn intersection(&self, other: &Self) -> Option<NonEmpty<Self>> {
153-
self.intersection_unchecked(other).to_non_empty()
151+
pub fn intersection(&self, other: &Self) -> Option<Self> {
152+
let b = self.intersection_unchecked(other);
153+
154+
if b.is_empty() {
155+
return None;
156+
}
157+
158+
Some(b)
154159
}
155160

156161
pub fn intersection_unchecked(&self, other: &Self) -> Self {

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub use crate::angle::Angle;
4444
pub use crate::box2d::Box2D;
4545
pub use crate::homogen::HomogeneousVector;
4646
pub use crate::length::Length;
47-
pub use crate::nonempty::NonEmpty;
4847
pub use crate::point::{point2, point3, Point2D, Point3D};
4948
pub use crate::scale::Scale;
5049
pub use crate::transform2d::Transform2D;
@@ -71,7 +70,6 @@ mod box2d;
7170
mod box3d;
7271
mod homogen;
7372
mod length;
74-
mod nonempty;
7573
pub mod num;
7674
mod point;
7775
mod rect;

src/nonempty.rs

Lines changed: 0 additions & 250 deletions
This file was deleted.

src/rect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use super::UnknownUnit;
1111
use crate::box2d::Box2D;
12-
use crate::nonempty::NonEmpty;
1312
use crate::num::*;
1413
use crate::point::Point2D;
1514
use crate::scale::Scale;
@@ -215,13 +214,14 @@ where
215214
T: Copy + PartialOrd + Add<T, Output = T> + Sub<T, Output = T>,
216215
{
217216
#[inline]
218-
pub fn intersection(&self, other: &Self) -> Option<NonEmpty<Self>> {
217+
pub fn intersection(&self, other: &Self) -> Option<Self> {
219218
let box2d = self.to_box2d().intersection_unchecked(&other.to_box2d());
219+
220220
if box2d.is_empty() {
221221
return None;
222222
}
223223

224-
Some(NonEmpty(box2d.to_rect()))
224+
Some(box2d.to_rect())
225225
}
226226
}
227227

@@ -393,12 +393,12 @@ impl<T: Copy + Zero + PartialOrd, U> Rect<T, U> {
393393

394394
impl<T: Copy + Zero + PartialOrd, U> Rect<T, U> {
395395
#[inline]
396-
pub fn to_non_empty(&self) -> Option<NonEmpty<Self>> {
396+
pub fn to_non_empty(&self) -> Option<Self> {
397397
if self.is_empty() {
398398
return None;
399399
}
400400

401-
Some(NonEmpty(*self))
401+
Some(*self)
402402
}
403403
}
404404

0 commit comments

Comments
 (0)