@@ -99,8 +99,6 @@ impl<T: Float> PartialEq for OrderedFloat<T> {
99
99
fn eq ( & self , other : & OrderedFloat < T > ) -> bool {
100
100
if self . as_ref ( ) . is_nan ( ) {
101
101
other. as_ref ( ) . is_nan ( )
102
- } else if other. as_ref ( ) . is_nan ( ) {
103
- false
104
102
} else {
105
103
self . as_ref ( ) == other. as_ref ( )
106
104
}
@@ -213,15 +211,13 @@ impl<T: Float> NotNan<T> {
213
211
214
212
/// Get the value out.
215
213
pub fn into_inner ( self ) -> T {
216
- let NotNan ( val) = self ;
217
- val
214
+ self . 0
218
215
}
219
216
}
220
217
221
218
impl < T : Float > AsRef < T > for NotNan < T > {
222
219
fn as_ref ( & self ) -> & T {
223
- let NotNan ( ref val) = * self ;
224
- val
220
+ & self . 0
225
221
}
226
222
}
227
223
@@ -246,15 +242,15 @@ impl<T: Float + fmt::Display> fmt::Display for NotNan<T> {
246
242
}
247
243
}
248
244
249
- impl Into < f32 > for NotNan < f32 > {
250
- fn into ( self ) -> f32 {
251
- self . into_inner ( )
245
+ impl From < NotNan < f32 > > for f32 {
246
+ fn from ( value : NotNan < f32 > ) -> Self {
247
+ value . into_inner ( )
252
248
}
253
249
}
254
250
255
- impl Into < f64 > for NotNan < f64 > {
256
- fn into ( self ) -> f64 {
257
- self . into_inner ( )
251
+ impl From < NotNan < f64 > > for f64 {
252
+ fn from ( value : NotNan < f64 > ) -> Self {
253
+ value . into_inner ( )
258
254
}
259
255
}
260
256
@@ -263,8 +259,7 @@ impl Into<f64> for NotNan<f64> {
263
259
/// Panics if the provided value is NaN or the computation results in NaN
264
260
impl < T : Float > From < T > for NotNan < T > {
265
261
fn from ( v : T ) -> Self {
266
- assert ! ( !v. is_nan( ) ) ;
267
- NotNan ( v)
262
+ NotNan :: new ( v) . expect ( "Tried to create a NotNan from a NaN" )
268
263
}
269
264
}
270
265
@@ -285,7 +280,7 @@ impl<T: Float> Add for NotNan<T> {
285
280
type Output = Self ;
286
281
287
282
fn add ( self , other : Self ) -> Self {
288
- NotNan :: new ( self . 0 + other. 0 ) . expect ( "Addition resulted in NaN" )
283
+ self + other. 0
289
284
}
290
285
}
291
286
@@ -296,52 +291,31 @@ impl<T: Float> Add<T> for NotNan<T> {
296
291
type Output = Self ;
297
292
298
293
fn add ( self , other : T ) -> Self {
299
- assert ! ( !other. is_nan( ) ) ;
300
294
NotNan :: new ( self . 0 + other) . expect ( "Addition resulted in NaN" )
301
295
}
302
296
}
303
297
304
- impl AddAssign for NotNan < f64 > {
305
- fn add_assign ( & mut self , other : Self ) {
306
- self . 0 += other. 0 ;
307
- assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" )
308
- }
309
- }
310
-
311
- impl AddAssign for NotNan < f32 > {
298
+ impl < T : Float + AddAssign > AddAssign for NotNan < T > {
312
299
fn add_assign ( & mut self , other : Self ) {
313
- self . 0 += other. 0 ;
314
- assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" )
315
- }
316
- }
317
-
318
- /// Adds a float directly.
319
- ///
320
- /// Panics if the provided value is NaN or the computation results in NaN
321
- impl AddAssign < f64 > for NotNan < f64 > {
322
- fn add_assign ( & mut self , other : f64 ) {
323
- assert ! ( !other. is_nan( ) ) ;
324
- self . 0 += other;
325
- assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" )
300
+ * self += other. 0 ;
326
301
}
327
302
}
328
303
329
304
/// Adds a float directly.
330
305
///
331
306
/// Panics if the provided value is NaN.
332
- impl AddAssign < f32 > for NotNan < f32 > {
333
- fn add_assign ( & mut self , other : f32 ) {
334
- assert ! ( !other. is_nan( ) ) ;
307
+ impl < T : Float + AddAssign > AddAssign < T > for NotNan < T > {
308
+ fn add_assign ( & mut self , other : T ) {
335
309
self . 0 += other;
336
- assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" )
310
+ assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" ) ;
337
311
}
338
312
}
339
313
340
314
impl < T : Float > Sub for NotNan < T > {
341
315
type Output = Self ;
342
316
343
317
fn sub ( self , other : Self ) -> Self {
344
- NotNan :: new ( self . 0 - other. 0 ) . expect ( "Subtraction resulted in NaN" )
318
+ self - other. 0
345
319
}
346
320
}
347
321
@@ -352,52 +326,31 @@ impl<T: Float> Sub<T> for NotNan<T> {
352
326
type Output = Self ;
353
327
354
328
fn sub ( self , other : T ) -> Self {
355
- assert ! ( !other. is_nan( ) ) ;
356
329
NotNan :: new ( self . 0 - other) . expect ( "Subtraction resulted in NaN" )
357
330
}
358
331
}
359
332
360
- impl SubAssign for NotNan < f64 > {
361
- fn sub_assign ( & mut self , other : Self ) {
362
- self . 0 -= other. 0 ;
363
- assert ! ( !self . 0 . is_nan( ) , "Subtraction resulted in NaN" )
364
- }
365
- }
366
-
367
- impl SubAssign for NotNan < f32 > {
333
+ impl < T : Float + SubAssign > SubAssign for NotNan < T > {
368
334
fn sub_assign ( & mut self , other : Self ) {
369
- self . 0 -= other. 0 ;
370
- assert ! ( !self . 0 . is_nan( ) , "Subtraction resulted in NaN" )
335
+ * self -= other. 0
371
336
}
372
337
}
373
338
374
339
/// Subtracts a float directly.
375
340
///
376
341
/// Panics if the provided value is NaN or the computation results in NaN
377
- impl SubAssign < f64 > for NotNan < f64 > {
378
- fn sub_assign ( & mut self , other : f64 ) {
379
- assert ! ( !other. is_nan( ) ) ;
342
+ impl < T : Float + SubAssign > SubAssign < T > for NotNan < T > {
343
+ fn sub_assign ( & mut self , other : T ) {
380
344
self . 0 -= other;
381
- assert ! ( !self . 0 . is_nan( ) , "Subtraction resulted in NaN" )
382
- }
383
- }
384
-
385
- /// Subtracts a float directly.
386
- ///
387
- /// Panics if the provided value is NaN or the computation results in NaN
388
- impl SubAssign < f32 > for NotNan < f32 > {
389
- fn sub_assign ( & mut self , other : f32 ) {
390
- assert ! ( !other. is_nan( ) ) ;
391
- self . 0 -= other;
392
- assert ! ( !self . 0 . is_nan( ) , "Subtraction resulted in NaN" )
345
+ assert ! ( !self . 0 . is_nan( ) , "Subtraction resulted in NaN" ) ;
393
346
}
394
347
}
395
348
396
349
impl < T : Float > Mul for NotNan < T > {
397
350
type Output = Self ;
398
351
399
352
fn mul ( self , other : Self ) -> Self {
400
- NotNan :: new ( self . 0 * other. 0 ) . expect ( "Multiplication resulted in NaN" )
353
+ self * other. 0
401
354
}
402
355
}
403
356
@@ -408,51 +361,31 @@ impl<T: Float> Mul<T> for NotNan<T> {
408
361
type Output = Self ;
409
362
410
363
fn mul ( self , other : T ) -> Self {
411
- assert ! ( !other. is_nan( ) ) ;
412
364
NotNan :: new ( self . 0 * other) . expect ( "Multiplication resulted in NaN" )
413
365
}
414
366
}
415
367
416
- impl MulAssign for NotNan < f64 > {
417
- fn mul_assign ( & mut self , other : Self ) {
418
- self . 0 *= other. 0 ;
419
- assert ! ( !self . 0 . is_nan( ) , "Multiplication resulted in NaN" )
420
- }
421
- }
422
-
423
- impl MulAssign for NotNan < f32 > {
368
+ impl < T : Float + MulAssign > MulAssign for NotNan < T > {
424
369
fn mul_assign ( & mut self , other : Self ) {
425
- self . 0 *= other. 0 ;
426
- assert ! ( !self . 0 . is_nan( ) , "Multiplication resulted in NaN" )
370
+ * self *= other. 0
427
371
}
428
372
}
429
373
430
374
/// Multiplies a float directly.
431
375
///
432
376
/// Panics if the provided value is NaN.
433
- impl MulAssign < f64 > for NotNan < f64 > {
434
- fn mul_assign ( & mut self , other : f64 ) {
435
- assert ! ( !other. is_nan( ) ) ;
436
- self . 0 *= other;
437
- }
438
- }
439
-
440
- /// Multiplies a float directly.
441
- ///
442
- /// Panics if the provided value is NaN or the computation results in NaN
443
- impl MulAssign < f32 > for NotNan < f32 > {
444
- fn mul_assign ( & mut self , other : f32 ) {
445
- assert ! ( !other. is_nan( ) ) ;
377
+ impl < T : Float + MulAssign > MulAssign < T > for NotNan < T > {
378
+ fn mul_assign ( & mut self , other : T ) {
446
379
self . 0 *= other;
447
- assert ! ( !self . 0 . is_nan( ) , "Multiplication resulted in NaN" )
380
+ assert ! ( !self . 0 . is_nan( ) , "Multiplication resulted in NaN" ) ;
448
381
}
449
382
}
450
383
451
384
impl < T : Float > Div for NotNan < T > {
452
385
type Output = Self ;
453
386
454
387
fn div ( self , other : Self ) -> Self {
455
- NotNan :: new ( self . 0 / other. 0 ) . expect ( "Division resulted in NaN" )
388
+ self / other. 0
456
389
}
457
390
}
458
391
@@ -463,52 +396,31 @@ impl<T: Float> Div<T> for NotNan<T> {
463
396
type Output = Self ;
464
397
465
398
fn div ( self , other : T ) -> Self {
466
- assert ! ( !other. is_nan( ) ) ;
467
399
NotNan :: new ( self . 0 / other) . expect ( "Division resulted in NaN" )
468
400
}
469
401
}
470
402
471
- impl DivAssign for NotNan < f64 > {
403
+ impl < T : Float + DivAssign > DivAssign for NotNan < T > {
472
404
fn div_assign ( & mut self , other : Self ) {
473
- self . 0 /= other. 0 ;
474
- assert ! ( !self . 0 . is_nan( ) , "Division resulted in NaN" )
475
- }
476
- }
477
-
478
- impl DivAssign for NotNan < f32 > {
479
- fn div_assign ( & mut self , other : Self ) {
480
- self . 0 /= other. 0 ;
481
- assert ! ( !self . 0 . is_nan( ) , "Division resulted in NaN" )
405
+ * self /= other. 0 ;
482
406
}
483
407
}
484
408
485
409
/// Divides a float directly.
486
410
///
487
411
/// Panics if the provided value is NaN or the computation results in NaN
488
- impl DivAssign < f64 > for NotNan < f64 > {
489
- fn div_assign ( & mut self , other : f64 ) {
490
- assert ! ( !other. is_nan( ) ) ;
412
+ impl < T : Float + DivAssign > DivAssign < T > for NotNan < T > {
413
+ fn div_assign ( & mut self , other : T ) {
491
414
self . 0 /= other;
492
- assert ! ( !self . 0 . is_nan( ) , "Division resulted in NaN" )
493
- }
494
- }
495
-
496
- /// Divides a float directly.
497
- ///
498
- /// Panics if the provided value is NaN or the computation results in NaN
499
- impl DivAssign < f32 > for NotNan < f32 > {
500
- fn div_assign ( & mut self , other : f32 ) {
501
- assert ! ( !other. is_nan( ) ) ;
502
- self . 0 /= other;
503
- assert ! ( !self . 0 . is_nan( ) , "Division resulted in NaN" )
415
+ assert ! ( !self . 0 . is_nan( ) , "Division resulted in NaN" ) ;
504
416
}
505
417
}
506
418
507
419
impl < T : Float > Rem for NotNan < T > {
508
420
type Output = Self ;
509
421
510
422
fn rem ( self , other : Self ) -> Self {
511
- NotNan :: new ( self . 0 % other. 0 ) . expect ( "Rem resulted in NaN" )
423
+ self % other. 0
512
424
}
513
425
}
514
426
@@ -519,52 +431,31 @@ impl<T: Float> Rem<T> for NotNan<T> {
519
431
type Output = Self ;
520
432
521
433
fn rem ( self , other : T ) -> Self {
522
- assert ! ( !other. is_nan( ) ) ;
523
434
NotNan :: new ( self . 0 % other) . expect ( "Rem resulted in NaN" )
524
435
}
525
436
}
526
437
527
- impl RemAssign for NotNan < f64 > {
528
- fn rem_assign ( & mut self , other : Self ) {
529
- self . 0 %= other. 0 ;
530
- assert ! ( !self . 0 . is_nan( ) , "Rem resulted in NaN" )
531
- }
532
- }
533
-
534
- impl RemAssign for NotNan < f32 > {
438
+ impl < T : Float + RemAssign > RemAssign for NotNan < T > {
535
439
fn rem_assign ( & mut self , other : Self ) {
536
- self . 0 %= other. 0 ;
537
- assert ! ( !self . 0 . is_nan( ) , "Rem resulted in NaN" )
440
+ * self %= other. 0
538
441
}
539
442
}
540
443
541
444
/// Calculates `%=` with a float directly.
542
445
///
543
446
/// Panics if the provided value is NaN or the computation results in NaN
544
- impl RemAssign < f64 > for NotNan < f64 > {
545
- fn rem_assign ( & mut self , other : f64 ) {
546
- assert ! ( !other. is_nan( ) ) ;
447
+ impl < T : Float + RemAssign > RemAssign < T > for NotNan < T > {
448
+ fn rem_assign ( & mut self , other : T ) {
547
449
self . 0 %= other;
548
- assert ! ( !self . 0 . is_nan( ) , "Rem resulted in NaN" )
549
- }
550
- }
551
-
552
- /// Calculates `%=` with a float directly.
553
- ///
554
- /// Panics if the provided value is NaN or the computation results in NaN
555
- impl RemAssign < f32 > for NotNan < f32 > {
556
- fn rem_assign ( & mut self , other : f32 ) {
557
- assert ! ( !other. is_nan( ) ) ;
558
- self . 0 %= other;
559
- assert ! ( !self . 0 . is_nan( ) , "Rem resulted in NaN" )
450
+ assert ! ( !self . 0 . is_nan( ) , "Rem resulted in NaN" ) ;
560
451
}
561
452
}
562
453
563
454
impl < T : Float > Neg for NotNan < T > {
564
455
type Output = Self ;
565
456
566
457
fn neg ( self ) -> Self {
567
- NotNan :: new ( -self . 0 ) . expect ( "Negation resulted in NaN" )
458
+ NotNan ( -self . 0 )
568
459
}
569
460
}
570
461
@@ -699,10 +590,13 @@ impl<E: fmt::Debug> std::error::Error for ParseNotNanError<E> {
699
590
fn description ( & self ) -> & str {
700
591
return "Error parsing a not-NaN floating point value" ;
701
592
}
593
+
594
+ // TODO: add an implementation of cause(). This will be breaking because it requires E: Error.
702
595
}
703
596
704
597
impl < E : fmt:: Debug > fmt:: Display for ParseNotNanError < E > {
705
598
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
599
+ // TODO: replace this with a human readable fmt. Will require E: Display.
706
600
<Self as fmt:: Debug >:: fmt ( self , f)
707
601
}
708
602
}
0 commit comments