@@ -408,13 +408,15 @@ pub enum ConstantOp {
408408/// Trait to identify [units][units] which have a [conversion factor][factor].
409409///
410410/// ## Generic Parameters
411- /// * `V `: Underlying storage type trait is implemented for.
412- ///
411+ /// * `T `: The type of the conversion factor. Usually same as `VT`, but for example complex storage types have this as float as it needs `PartialEq`
412+ /// * `VT`: Underlying storage type trait is implemented for. Does not have to implement `PartialEq`, so its viable for complex data types.
413413/// [units]: https://jcgm.bipm.org/vim/en/1.13.html
414414/// [factor]: https://jcgm.bipm.org/vim/en/1.24.html
415415pub trait Conversion < V > {
416416 /// Conversion factor type specific to the underlying storage type.
417- type T : ConversionFactor < V > ;
417+ type T : ConversionFactor < V > + PartialOrd ;
418+ /// Value type of the underlying type.
419+ type VT : ConversionFactor < V > + From < Self :: T > ;
418420
419421 /// Coefficient portion of [conversion factor](https://jcgm.bipm.org/vim/en/1.24.html) for
420422 /// converting the given unit. To convert to the base unit for the quantity use `(value +
@@ -436,21 +438,17 @@ pub trait Conversion<V> {
436438 #[ must_use = "method returns a new number and does not mutate the original value" ]
437439 #[ inline( always) ]
438440 #[ allow( unused_variables) ]
439- fn constant ( op : ConstantOp ) -> Self :: T {
440- <Self :: T as num:: Zero >:: zero ( )
441+ fn constant ( op : ConstantOp ) -> Self :: VT {
442+ <Self :: VT as num:: Zero >:: zero ( )
441443 }
442444
443445 /// Instance [conversion factor](https://jcgm.bipm.org/vim/en/1.24.html).
444446 ///
445447 /// Default implementation returns the coefficient: `Self::coefficient()`.
446448 #[ must_use = "method returns a new number and does not mutate the original value" ]
447- #[ inline( always) ]
448- fn conversion ( & self ) -> Self :: T
449+ fn conversion ( & self ) -> Self :: VT
449450 where
450- Self : Sized ,
451- {
452- Self :: coefficient ( )
453- }
451+ Self : Sized ;
454452}
455453
456454/// Trait representing a [conversion factor][factor].
@@ -461,8 +459,7 @@ pub trait Conversion<V> {
461459/// [factor]: https://jcgm.bipm.org/vim/en/1.24.html
462460#[ allow( unused_qualifications) ] // lib:cmp::PartialOrder false positive.
463461pub trait ConversionFactor < V > :
464- lib:: cmp:: PartialOrd
465- + lib:: ops:: Add < Self , Output = Self >
462+ lib:: ops:: Add < Self , Output = Self >
466463 + lib:: ops:: Sub < Self , Output = Self >
467464 + lib:: ops:: Mul < Self , Output = Self >
468465 + lib:: ops:: Div < Self , Output = Self >
@@ -515,19 +512,20 @@ pub trait Kind:
515512storage_types ! {
516513 types: Float ;
517514
518- impl crate :: Conversion <Self > for V {
515+ impl crate :: Conversion <V > for V {
519516 type T = Self ;
517+ type VT = Self :: T ;
520518
521519 #[ inline( always) ]
522- fn constant( op: crate :: ConstantOp ) -> Self :: T {
520+ fn constant( op: crate :: ConstantOp ) -> Self :: VT {
523521 match op {
524- crate :: ConstantOp :: Add => -<Self :: T as crate :: num:: Zero >:: zero( ) ,
525- crate :: ConstantOp :: Sub => <Self :: T as crate :: num:: Zero >:: zero( ) ,
522+ crate :: ConstantOp :: Add => -<Self :: VT as crate :: num:: Zero >:: zero( ) ,
523+ crate :: ConstantOp :: Sub => <Self :: VT as crate :: num:: Zero >:: zero( ) ,
526524 }
527525 }
528526
529527 #[ inline( always) ]
530- fn conversion( & self ) -> Self :: T {
528+ fn conversion( & self ) -> Self :: VT {
531529 * self
532530 }
533531 }
@@ -554,9 +552,10 @@ storage_types! {
554552
555553 impl crate :: Conversion <V > for V {
556554 type T = crate :: num:: rational:: Ratio <V >;
555+ type VT = Self :: T ;
557556
558557 #[ inline( always) ]
559- fn conversion( & self ) -> Self :: T {
558+ fn conversion( & self ) -> Self :: VT {
560559 ( * self ) . into( )
561560 }
562561 }
@@ -583,9 +582,10 @@ storage_types! {
583582
584583 impl crate :: Conversion <V > for V {
585584 type T = crate :: num:: rational:: Ratio <V >;
585+ type VT = Self :: T ;
586586
587587 #[ inline( always) ]
588- fn conversion( & self ) -> Self :: T {
588+ fn conversion( & self ) -> Self :: VT {
589589 self . clone( ) . into( )
590590 }
591591 }
@@ -612,9 +612,10 @@ storage_types! {
612612
613613 impl crate :: Conversion <V > for V {
614614 type T = V ;
615+ type VT = Self :: T ;
615616
616617 #[ inline( always) ]
617- fn conversion( & self ) -> Self :: T {
618+ fn conversion( & self ) -> Self :: VT {
618619 * self
619620 }
620621 }
@@ -637,9 +638,10 @@ storage_types! {
637638
638639 impl crate :: Conversion <V > for V {
639640 type T = V ;
641+ type VT = Self :: T ;
640642
641643 #[ inline( always) ]
642- fn conversion( & self ) -> Self :: T {
644+ fn conversion( & self ) -> Self :: VT {
643645 self . clone( )
644646 }
645647 }
@@ -665,20 +667,21 @@ storage_types! {
665667 types: Complex ;
666668 impl crate :: Conversion <V > for V {
667669 type T = VV ;
670+ type VT = V ;
668671
669672 #[ inline( always) ]
670- fn constant( op: crate :: ConstantOp ) -> Self :: T {
673+ fn constant( op: crate :: ConstantOp ) -> Self :: VT {
671674 match op {
672- crate :: ConstantOp :: Add => -<Self :: T as crate :: num:: Zero >:: zero( ) ,
673- crate :: ConstantOp :: Sub => <Self :: T as crate :: num:: Zero >:: zero( ) ,
675+ crate :: ConstantOp :: Add => -<Self :: VT as crate :: num:: Zero >:: zero( ) ,
676+ crate :: ConstantOp :: Sub => <Self :: VT as crate :: num:: Zero >:: zero( ) ,
674677 }
675678 }
676679
677680 #[ inline( always) ]
678- fn conversion( & self ) -> Self :: T {
681+ fn conversion( & self ) -> Self :: VT {
679682 // Conversion factor is the norm of the number. Scaling with length again yields the
680683 // same number.
681- self . norm ( )
684+ * self
682685 }
683686 }
684687
@@ -695,6 +698,18 @@ storage_types! {
695698 V :: new( self , 0.0 )
696699 }
697700 }
701+
702+ impl crate :: ConversionFactor <V > for V {
703+ #[ inline( always) ]
704+ fn powi( self , e: i32 ) -> Self {
705+ crate :: num:: complex:: Complex :: powi( & self , e)
706+ }
707+
708+ #[ inline( always) ]
709+ fn value( self ) -> V {
710+ self
711+ }
712+ }
698713}
699714
700715/// Utilities for formatting and printing quantities.
0 commit comments