@@ -10,15 +10,15 @@ pub enum Angle {
1010 Deg ( f32 ) ,
1111 Grad ( f32 ) ,
1212 Rad ( f32 ) ,
13- Turn ( f32 ) ,
14- Calc ( Calc < Angle > )
13+ Turn ( f32 )
1514}
1615
1716impl Parse for Angle {
1817 fn parse < ' i , ' t > ( input : & mut Parser < ' i , ' t > ) -> Result < Self , ParseError < ' i , ( ) > > {
1918 match input. try_parse ( Calc :: parse) {
2019 Ok ( Calc :: Value ( v) ) => return Ok ( * v) ,
21- Ok ( calc) => return Ok ( Angle :: Calc ( calc) ) ,
20+ // Angles are always compatible, so they will always compute to a value.
21+ Ok ( _) => unreachable ! ( ) ,
2222 _ => { }
2323 }
2424
@@ -45,20 +45,16 @@ impl ToCss for Angle {
4545 Angle :: Deg ( val) => ( * val, "deg" ) ,
4646 Angle :: Grad ( val) => ( * val, "grad" ) ,
4747 Angle :: Rad ( val) => {
48- if let Some ( deg) = self . to_degrees ( ) {
49- // We print 5 digits of precision by default.
50- // Switch to degrees if there are an even number of them.
51- if ( deg * 100000.0 ) . round ( ) . fract ( ) == 0.0 {
52- ( deg, "deg" )
53- } else {
54- ( * val, "rad" )
55- }
48+ let deg = self . to_degrees ( ) ;
49+ // We print 5 digits of precision by default.
50+ // Switch to degrees if there are an even number of them.
51+ if ( deg * 100000.0 ) . round ( ) . fract ( ) == 0.0 {
52+ ( deg, "deg" )
5653 } else {
5754 ( * val, "rad" )
5855 }
5956 } ,
60- Angle :: Turn ( val) => ( * val, "turn" ) ,
61- Angle :: Calc ( calc) => return calc. to_css ( dest)
57+ Angle :: Turn ( val) => ( * val, "turn" )
6258 } ;
6359
6460 use cssparser:: ToCss ;
@@ -93,47 +89,42 @@ impl Angle {
9389 use Angle :: * ;
9490 match self {
9591 Deg ( v) | Rad ( v) | Grad ( v) | Turn ( v) => * v == 0.0 ,
96- Calc ( _) => false
9792 }
9893 }
9994
100- pub fn to_radians ( & self ) -> Option < f32 > {
95+ pub fn to_radians ( & self ) -> f32 {
10196 const RAD_PER_DEG : f32 = PI / 180.0 ;
102- let r = match self {
97+ match self {
10398 Angle :: Deg ( deg) => deg * RAD_PER_DEG ,
10499 Angle :: Rad ( rad) => * rad,
105100 Angle :: Grad ( grad) => grad * 180.0 / 200.0 * RAD_PER_DEG ,
106101 Angle :: Turn ( turn) => turn * 360.0 * RAD_PER_DEG ,
107- Angle :: Calc ( _) => return None
108- } ;
109- Some ( r)
102+ }
110103 }
111104
112- pub fn to_degrees ( & self ) -> Option < f32 > {
105+ pub fn to_degrees ( & self ) -> f32 {
113106 const DEG_PER_RAD : f32 = 180.0 / PI ;
114- let d = match self {
107+ match self {
115108 Angle :: Deg ( deg) => * deg,
116109 Angle :: Rad ( rad) => rad * DEG_PER_RAD ,
117110 Angle :: Grad ( grad) => grad * 180.0 / 200.0 ,
118111 Angle :: Turn ( turn) => turn * 360.0 ,
119- Angle :: Calc ( _) => return None
120- } ;
121- Some ( d)
112+ }
122113 }
123114}
124115
125116impl std:: convert:: Into < Calc < Angle > > for Angle {
126117 fn into ( self ) -> Calc < Angle > {
127- match self {
128- Angle :: Calc ( c) => c,
129- b => Calc :: Value ( Box :: new ( b) )
130- }
118+ Calc :: Value ( Box :: new ( self ) )
131119 }
132120}
133121
134122impl std:: convert:: From < Calc < Angle > > for Angle {
135123 fn from ( calc : Calc < Angle > ) -> Angle {
136- Angle :: Calc ( calc)
124+ match calc {
125+ Calc :: Value ( v) => * v,
126+ _ => unreachable ! ( )
127+ }
137128 }
138129}
139130
@@ -146,7 +137,6 @@ impl std::ops::Mul<f32> for Angle {
146137 Angle :: Rad ( v) => Angle :: Deg ( v * other) ,
147138 Angle :: Grad ( v) => Angle :: Deg ( v * other) ,
148139 Angle :: Turn ( v) => Angle :: Deg ( v * other) ,
149- Angle :: Calc ( c) => Angle :: Calc ( c * other)
150140 }
151141 }
152142}
@@ -155,20 +145,14 @@ impl std::ops::Add<Angle> for Angle {
155145 type Output = Self ;
156146
157147 fn add ( self , other : Angle ) -> Angle {
158- match ( self , other) {
159- ( Angle :: Calc ( a) , Angle :: Calc ( b) ) => Angle :: Calc ( a + b) ,
160- ( Angle :: Calc ( a) , b) => Angle :: Calc ( a + Calc :: Value ( Box :: new ( b) ) ) ,
161- ( a, Angle :: Calc ( b) ) => Angle :: Calc ( Calc :: Value ( Box :: new ( a) ) + b) ,
162- ( a, b) => Angle :: Deg ( a. to_degrees ( ) . unwrap ( ) + b. to_degrees ( ) . unwrap ( ) )
163- }
148+ Angle :: Deg ( self . to_degrees ( ) + other. to_degrees ( ) )
164149 }
165150}
166151
167152impl std:: cmp:: PartialEq < f32 > for Angle {
168153 fn eq ( & self , other : & f32 ) -> bool {
169154 match self {
170155 Angle :: Deg ( a) | Angle :: Rad ( a) | Angle :: Grad ( a) | Angle :: Turn ( a) => a == other,
171- Angle :: Calc ( _) => false
172156 }
173157 }
174158}
@@ -177,7 +161,6 @@ impl std::cmp::PartialOrd<f32> for Angle {
177161 fn partial_cmp ( & self , other : & f32 ) -> Option < std:: cmp:: Ordering > {
178162 match self {
179163 Angle :: Deg ( a) | Angle :: Rad ( a) | Angle :: Grad ( a) | Angle :: Turn ( a) => a. partial_cmp ( other) ,
180- Angle :: Calc ( _) => None
181164 }
182165 }
183166}
0 commit comments