Skip to content

Commit 428ab8c

Browse files
authored
Merge pull request #167 from Ten0/less_implicit_panic_on_binary_operations
Remove implicit panics in NotNan<T> x T operators
2 parents facb424 + 86f0b79 commit 428ab8c

File tree

2 files changed

+66
-195
lines changed

2 files changed

+66
-195
lines changed

src/lib.rs

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,10 +1227,7 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
12271227
/// ```
12281228
/// use ordered_float::NotNan;
12291229
///
1230-
/// let mut v = [
1231-
/// NotNan::new(2.0).unwrap(),
1232-
/// NotNan::new(1.0).unwrap(),
1233-
/// ];
1230+
/// let mut v = [NotNan::new(2.0).unwrap(), NotNan::new(1.0).unwrap()];
12341231
/// v.sort();
12351232
/// assert_eq!(v, [1.0, 2.0]);
12361233
/// ```
@@ -1270,7 +1267,6 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
12701267
/// [transmute](core::mem::transmute) or pointer casts to convert between any type `T` and
12711268
/// `NotNan<T>`, as long as this does not create a NaN value.
12721269
/// However, consider using [`bytemuck`] as a safe alternative if possible.
1273-
///
12741270
#[cfg_attr(
12751271
not(feature = "bytemuck"),
12761272
doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
@@ -1384,9 +1380,10 @@ impl NotNan<f64> {
13841380
/// Note: For the reverse conversion (from `NotNan<f32>` to `NotNan<f64>`), you can use
13851381
/// `.into()`.
13861382
pub fn as_f32(self) -> NotNan<f32> {
1387-
// This is not destroying invariants, as it is a pure rounding operation. The only two special
1388-
// cases are where f32 would be overflowing, then the operation yields Infinity, or where
1389-
// the input is already NaN, in which case the invariant is already broken elsewhere.
1383+
// This is not destroying invariants, as it is a pure rounding operation. The only two
1384+
// special cases are where f32 would be overflowing, then the operation yields
1385+
// Infinity, or where the input is already NaN, in which case the invariant is
1386+
// already broken elsewhere.
13901387
NotNan(self.0 as f32)
13911388
}
13921389
}
@@ -1472,13 +1469,13 @@ impl<T: FloatCore> PartialEq<T> for NotNan<T> {
14721469

14731470
/// Adds a float directly.
14741471
///
1475-
/// Panics if the provided value is NaN or the computation results in NaN
1472+
/// This returns a `T` and not a `NotNan<T>` because if the added value is NaN, this will be NaN
14761473
impl<T: FloatCore> Add<T> for NotNan<T> {
1477-
type Output = Self;
1474+
type Output = T;
14781475

14791476
#[inline]
1480-
fn add(self, other: T) -> Self {
1481-
NotNan::new(self.0 + other).expect("Addition resulted in NaN")
1477+
fn add(self, other: T) -> Self::Output {
1478+
self.0 + other
14821479
}
14831480
}
14841481

@@ -1500,25 +1497,27 @@ impl<'a, T: FloatCore + Sum + 'a> Sum<&'a NotNan<T>> for NotNan<T> {
15001497

15011498
/// Subtracts a float directly.
15021499
///
1503-
/// Panics if the provided value is NaN or the computation results in NaN
1500+
/// This returns a `T` and not a `NotNan<T>` because if the substracted value is NaN, this will be
1501+
/// NaN
15041502
impl<T: FloatCore> Sub<T> for NotNan<T> {
1505-
type Output = Self;
1503+
type Output = T;
15061504

15071505
#[inline]
1508-
fn sub(self, other: T) -> Self {
1509-
NotNan::new(self.0 - other).expect("Subtraction resulted in NaN")
1506+
fn sub(self, other: T) -> Self::Output {
1507+
self.0 - other
15101508
}
15111509
}
15121510

15131511
/// Multiplies a float directly.
15141512
///
1515-
/// Panics if the provided value is NaN or the computation results in NaN
1513+
/// This returns a `T` and not a `NotNan<T>` because if the multiplied value is NaN, this will be
1514+
/// NaN
15161515
impl<T: FloatCore> Mul<T> for NotNan<T> {
1517-
type Output = Self;
1516+
type Output = T;
15181517

15191518
#[inline]
1520-
fn mul(self, other: T) -> Self {
1521-
NotNan::new(self.0 * other).expect("Multiplication resulted in NaN")
1519+
fn mul(self, other: T) -> Self::Output {
1520+
self.0 * other
15221521
}
15231522
}
15241523

@@ -1537,25 +1536,26 @@ impl<'a, T: FloatCore + Product + 'a> Product<&'a NotNan<T>> for NotNan<T> {
15371536

15381537
/// Divides a float directly.
15391538
///
1540-
/// Panics if the provided value is NaN or the computation results in NaN
1539+
/// This returns a `T` and not a `NotNan<T>` because if the divided-by value is NaN, this will be
1540+
/// NaN
15411541
impl<T: FloatCore> Div<T> for NotNan<T> {
1542-
type Output = Self;
1542+
type Output = T;
15431543

15441544
#[inline]
1545-
fn div(self, other: T) -> Self {
1546-
NotNan::new(self.0 / other).expect("Division resulted in NaN")
1545+
fn div(self, other: T) -> Self::Output {
1546+
self.0 / other
15471547
}
15481548
}
15491549

15501550
/// Calculates `%` with a float directly.
15511551
///
1552-
/// Panics if the provided value is NaN or the computation results in NaN
1552+
/// This returns a `T` and not a `NotNan<T>` because if the RHS is NaN, this will be NaN
15531553
impl<T: FloatCore> Rem<T> for NotNan<T> {
1554-
type Output = Self;
1554+
type Output = T;
15551555

15561556
#[inline]
1557-
fn rem(self, other: T) -> Self {
1558-
NotNan::new(self.0 % other).expect("Rem resulted in NaN")
1557+
fn rem(self, other: T) -> Self::Output {
1558+
self.0 % other
15591559
}
15601560
}
15611561

@@ -1566,12 +1566,13 @@ macro_rules! impl_not_nan_binop {
15661566

15671567
#[inline]
15681568
fn $method(self, other: Self) -> Self {
1569-
self.$method(other.0)
1569+
NotNan::new(self.0.$method(other.0))
1570+
.expect("Operation on two NotNan resulted in NaN")
15701571
}
15711572
}
15721573

15731574
impl<T: FloatCore> $imp<&T> for NotNan<T> {
1574-
type Output = NotNan<T>;
1575+
type Output = T;
15751576

15761577
#[inline]
15771578
fn $method(self, other: &T) -> Self::Output {
@@ -1584,7 +1585,7 @@ macro_rules! impl_not_nan_binop {
15841585

15851586
#[inline]
15861587
fn $method(self, other: &Self) -> Self::Output {
1587-
self.$method(other.0)
1588+
self.$method(*other)
15881589
}
15891590
}
15901591

@@ -1593,7 +1594,7 @@ macro_rules! impl_not_nan_binop {
15931594

15941595
#[inline]
15951596
fn $method(self, other: Self) -> Self::Output {
1596-
(*self).$method(other.0)
1597+
(*self).$method(*other)
15971598
}
15981599
}
15991600

@@ -1602,12 +1603,12 @@ macro_rules! impl_not_nan_binop {
16021603

16031604
#[inline]
16041605
fn $method(self, other: NotNan<T>) -> Self::Output {
1605-
(*self).$method(other.0)
1606+
(*self).$method(other)
16061607
}
16071608
}
16081609

16091610
impl<T: FloatCore> $imp<T> for &NotNan<T> {
1610-
type Output = NotNan<T>;
1611+
type Output = T;
16111612

16121613
#[inline]
16131614
fn $method(self, other: T) -> Self::Output {
@@ -1616,39 +1617,25 @@ macro_rules! impl_not_nan_binop {
16161617
}
16171618

16181619
impl<T: FloatCore> $imp<&T> for &NotNan<T> {
1619-
type Output = NotNan<T>;
1620+
type Output = T;
16201621

16211622
#[inline]
16221623
fn $method(self, other: &T) -> Self::Output {
16231624
(*self).$method(*other)
16241625
}
16251626
}
16261627

1627-
impl<T: FloatCore + $assign_imp> $assign_imp<T> for NotNan<T> {
1628-
#[inline]
1629-
fn $assign_method(&mut self, other: T) {
1630-
*self = (*self).$method(other);
1631-
}
1632-
}
1633-
1634-
impl<T: FloatCore + $assign_imp> $assign_imp<&T> for NotNan<T> {
1635-
#[inline]
1636-
fn $assign_method(&mut self, other: &T) {
1637-
*self = (*self).$method(*other);
1638-
}
1639-
}
1640-
16411628
impl<T: FloatCore + $assign_imp> $assign_imp for NotNan<T> {
16421629
#[inline]
16431630
fn $assign_method(&mut self, other: Self) {
1644-
(*self).$assign_method(other.0);
1631+
*self = (*self).$method(other);
16451632
}
16461633
}
16471634

16481635
impl<T: FloatCore + $assign_imp> $assign_imp<&Self> for NotNan<T> {
16491636
#[inline]
16501637
fn $assign_method(&mut self, other: &Self) {
1651-
(*self).$assign_method(other.0);
1638+
*self = (*self).$method(*other);
16521639
}
16531640
}
16541641
};

0 commit comments

Comments
 (0)