@@ -99,8 +99,6 @@ impl<T: Float> PartialEq for OrderedFloat<T> {
9999 fn eq ( & self , other : & OrderedFloat < T > ) -> bool {
100100 if self . as_ref ( ) . is_nan ( ) {
101101 other. as_ref ( ) . is_nan ( )
102- } else if other. as_ref ( ) . is_nan ( ) {
103- false
104102 } else {
105103 self . as_ref ( ) == other. as_ref ( )
106104 }
@@ -213,15 +211,13 @@ impl<T: Float> NotNan<T> {
213211
214212 /// Get the value out.
215213 pub fn into_inner ( self ) -> T {
216- let NotNan ( val) = self ;
217- val
214+ self . 0
218215 }
219216}
220217
221218impl < T : Float > AsRef < T > for NotNan < T > {
222219 fn as_ref ( & self ) -> & T {
223- let NotNan ( ref val) = * self ;
224- val
220+ & self . 0
225221 }
226222}
227223
@@ -246,15 +242,15 @@ impl<T: Float + fmt::Display> fmt::Display for NotNan<T> {
246242 }
247243}
248244
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 ( )
252248 }
253249}
254250
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 ( )
258254 }
259255}
260256
@@ -263,8 +259,7 @@ impl Into<f64> for NotNan<f64> {
263259/// Panics if the provided value is NaN or the computation results in NaN
264260impl < T : Float > From < T > for NotNan < T > {
265261 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" )
268263 }
269264}
270265
@@ -285,7 +280,7 @@ impl<T: Float> Add for NotNan<T> {
285280 type Output = Self ;
286281
287282 fn add ( self , other : Self ) -> Self {
288- NotNan :: new ( self . 0 + other. 0 ) . expect ( "Addition resulted in NaN" )
283+ self + other. 0
289284 }
290285}
291286
@@ -296,52 +291,31 @@ impl<T: Float> Add<T> for NotNan<T> {
296291 type Output = Self ;
297292
298293 fn add ( self , other : T ) -> Self {
299- assert ! ( !other. is_nan( ) ) ;
300294 NotNan :: new ( self . 0 + other) . expect ( "Addition resulted in NaN" )
301295 }
302296}
303297
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 > {
312299 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 ;
326301 }
327302}
328303
329304/// Adds a float directly.
330305///
331306/// 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 ) {
335309 self . 0 += other;
336- assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" )
310+ assert ! ( !self . 0 . is_nan( ) , "Addition resulted in NaN" ) ;
337311 }
338312}
339313
340314impl < T : Float > Sub for NotNan < T > {
341315 type Output = Self ;
342316
343317 fn sub ( self , other : Self ) -> Self {
344- NotNan :: new ( self . 0 - other. 0 ) . expect ( "Subtraction resulted in NaN" )
318+ self - other. 0
345319 }
346320}
347321
@@ -352,52 +326,31 @@ impl<T: Float> Sub<T> for NotNan<T> {
352326 type Output = Self ;
353327
354328 fn sub ( self , other : T ) -> Self {
355- assert ! ( !other. is_nan( ) ) ;
356329 NotNan :: new ( self . 0 - other) . expect ( "Subtraction resulted in NaN" )
357330 }
358331}
359332
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 > {
368334 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
371336 }
372337}
373338
374339/// Subtracts a float directly.
375340///
376341/// 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 ) {
380344 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" ) ;
393346 }
394347}
395348
396349impl < T : Float > Mul for NotNan < T > {
397350 type Output = Self ;
398351
399352 fn mul ( self , other : Self ) -> Self {
400- NotNan :: new ( self . 0 * other. 0 ) . expect ( "Multiplication resulted in NaN" )
353+ self * other. 0
401354 }
402355}
403356
@@ -408,51 +361,31 @@ impl<T: Float> Mul<T> for NotNan<T> {
408361 type Output = Self ;
409362
410363 fn mul ( self , other : T ) -> Self {
411- assert ! ( !other. is_nan( ) ) ;
412364 NotNan :: new ( self . 0 * other) . expect ( "Multiplication resulted in NaN" )
413365 }
414366}
415367
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 > {
424369 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
427371 }
428372}
429373
430374/// Multiplies a float directly.
431375///
432376/// 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 ) {
446379 self . 0 *= other;
447- assert ! ( !self . 0 . is_nan( ) , "Multiplication resulted in NaN" )
380+ assert ! ( !self . 0 . is_nan( ) , "Multiplication resulted in NaN" ) ;
448381 }
449382}
450383
451384impl < T : Float > Div for NotNan < T > {
452385 type Output = Self ;
453386
454387 fn div ( self , other : Self ) -> Self {
455- NotNan :: new ( self . 0 / other. 0 ) . expect ( "Division resulted in NaN" )
388+ self / other. 0
456389 }
457390}
458391
@@ -463,52 +396,31 @@ impl<T: Float> Div<T> for NotNan<T> {
463396 type Output = Self ;
464397
465398 fn div ( self , other : T ) -> Self {
466- assert ! ( !other. is_nan( ) ) ;
467399 NotNan :: new ( self . 0 / other) . expect ( "Division resulted in NaN" )
468400 }
469401}
470402
471- impl DivAssign for NotNan < f64 > {
403+ impl < T : Float + DivAssign > DivAssign for NotNan < T > {
472404 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 ;
482406 }
483407}
484408
485409/// Divides a float directly.
486410///
487411/// 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 ) {
491414 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" ) ;
504416 }
505417}
506418
507419impl < T : Float > Rem for NotNan < T > {
508420 type Output = Self ;
509421
510422 fn rem ( self , other : Self ) -> Self {
511- NotNan :: new ( self . 0 % other. 0 ) . expect ( "Rem resulted in NaN" )
423+ self % other. 0
512424 }
513425}
514426
@@ -519,52 +431,31 @@ impl<T: Float> Rem<T> for NotNan<T> {
519431 type Output = Self ;
520432
521433 fn rem ( self , other : T ) -> Self {
522- assert ! ( !other. is_nan( ) ) ;
523434 NotNan :: new ( self . 0 % other) . expect ( "Rem resulted in NaN" )
524435 }
525436}
526437
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 > {
535439 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
538441 }
539442}
540443
541444/// Calculates `%=` with a float directly.
542445///
543446/// 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 ) {
547449 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" ) ;
560451 }
561452}
562453
563454impl < T : Float > Neg for NotNan < T > {
564455 type Output = Self ;
565456
566457 fn neg ( self ) -> Self {
567- NotNan :: new ( -self . 0 ) . expect ( "Negation resulted in NaN" )
458+ NotNan ( -self . 0 )
568459 }
569460}
570461
@@ -699,10 +590,13 @@ impl<E: fmt::Debug> std::error::Error for ParseNotNanError<E> {
699590 fn description ( & self ) -> & str {
700591 return "Error parsing a not-NaN floating point value" ;
701592 }
593+
594+ // TODO: add an implementation of cause(). This will be breaking because it requires E: Error.
702595}
703596
704597impl < E : fmt:: Debug > fmt:: Display for ParseNotNanError < E > {
705598 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
599+ // TODO: replace this with a human readable fmt. Will require E: Display.
706600 <Self as fmt:: Debug >:: fmt ( self , f)
707601 }
708602}
0 commit comments