Skip to content

Commit

Permalink
Auto merge of servo#485 - nical:finite, r=jrmuizel
Browse files Browse the repository at this point in the history
Implement is_finite for most types

Useful to detect NaNs and inf when sanitizing inputs.
  • Loading branch information
bors-servo authored May 18, 2021
2 parents 0af8085 + 22ef9b3 commit f271116
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "euclid"
version = "0.22.3"
version = "0.22.4"
authors = ["The Servo Project Developers"]
edition = "2018"
description = "Geometry primitives"
Expand Down
6 changes: 6 additions & 0 deletions src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ where
pub fn sin_cos(self) -> (T, T) {
self.radians.sin_cos()
}

/// Returns true if the angle is a finite number.
#[inline]
pub fn is_finite(self) -> bool {
self.radians.is_finite()
}
}

impl<T> Angle<T>
Expand Down
10 changes: 9 additions & 1 deletion src/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::side_offsets::SideOffsets2D;
use crate::size::Size2D;
use crate::vector::{vec2, Vector2D};

use num_traits::NumCast;
use num_traits::{NumCast, Float};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -571,6 +571,14 @@ impl<T: NumCast + Copy, U> Box2D<T, U> {
}
}

impl<T: Float, U> Box2D<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.min.is_finite() && self.max.is_finite()
}
}

impl<T, U> Box2D<T, U>
where
T: Round,
Expand Down
10 changes: 9 additions & 1 deletion src/box3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::scale::Scale;
use crate::size::Size3D;
use crate::vector::Vector3D;

use num_traits::NumCast;
use num_traits::{NumCast, Float};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -568,6 +568,14 @@ impl<T: NumCast + Copy, U> Box3D<T, U> {
}
}

impl<T: Float, U> Box3D<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.min.is_finite() && self.max.is_finite()
}
}

impl<T, U> Box3D<T, U>
where
T: Round,
Expand Down
16 changes: 16 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ impl<T: NumCast + Copy, U> Point2D<T, U> {
}
}

impl<T: Float, U> Point2D<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite()
}
}

impl<T: Copy + Add<T, Output = T>, U> Point2D<T, U> {
#[inline]
pub fn add_size(self, other: &Size2D<T, U>) -> Self {
Expand Down Expand Up @@ -1197,6 +1205,14 @@ impl<T: NumCast + Copy, U> Point3D<T, U> {
}
}

impl<T: Float, U> Point3D<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
}
}

impl<T: Copy + Add<T, Output = T>, U> Point3D<T, U> {
#[inline]
pub fn add_size(self, other: Size3D<T, U>) -> Self {
Expand Down
10 changes: 9 additions & 1 deletion src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::side_offsets::SideOffsets2D;
use crate::size::Size2D;
use crate::vector::Vector2D;

use num_traits::NumCast;
use num_traits::{NumCast, Float};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -594,6 +594,14 @@ impl<T: NumCast + Copy, U> Rect<T, U> {
}
}

impl<T: Float, U> Rect<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.origin.is_finite() && self.size.is_finite()
}
}

impl<T: Floor + Ceil + Round + Add<T, Output = T> + Sub<T, Output = T>, U> Rect<T, U> {
/// Return a rectangle with edges rounded to integer coordinates, such that
/// the returned rectangle has the same set of pixel centers as the original
Expand Down
18 changes: 17 additions & 1 deletion src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use core::hash::Hash;
use core::iter::Sum;
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use num_traits::{NumCast, Signed};
use num_traits::{NumCast, Signed, Float};
#[cfg(feature = "serde")]
use serde;

Expand Down Expand Up @@ -392,6 +392,14 @@ impl<T: NumCast + Copy, U> Size2D<T, U> {
}
}

impl<T: Float, U> Size2D<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.width.is_finite() && self.height.is_finite()
}
}

impl<T: Signed, U> Size2D<T, U> {
/// Computes the absolute value of each component.
///
Expand Down Expand Up @@ -1273,6 +1281,14 @@ impl<T: NumCast + Copy, U> Size3D<T, U> {
}
}

impl<T: Float, U> Size3D<T, U> {
/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.width.is_finite() && self.height.is_finite() && self.depth.is_finite()
}
}

impl<T: Signed, U> Size3D<T, U> {
/// Computes the absolute value of each component.
///
Expand Down
12 changes: 12 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ impl<T: Float, U> Vector2D<T, U> {
debug_assert!(min <= max);
self.with_min_length(min).with_max_length(max)
}

/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite()
}
}

impl<T, U> Vector2D<T, U>
Expand Down Expand Up @@ -1353,6 +1359,12 @@ impl<T: Float, U> Vector3D<T, U> {
debug_assert!(min <= max);
self.with_min_length(min).with_max_length(max)
}

/// Returns true if all members are finite.
#[inline]
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
}
}

impl<T, U> Vector3D<T, U>
Expand Down

0 comments on commit f271116

Please sign in to comment.