@@ -150,7 +150,7 @@ pub enum Length {
150150 Rpx ( f32 ) ,
151151 Em ( f32 ) ,
152152 Ratio ( f32 ) ,
153- Expr ( LengthExpr ) ,
153+ Expr ( Box < LengthExpr > ) ,
154154 Vmin ( f32 ) ,
155155 Vmax ( f32 ) ,
156156}
@@ -188,6 +188,25 @@ impl Length {
188188 }
189189 }
190190
191+ /// Create a new `Length` from a `CalcExpr`.
192+ pub fn new_calc_expr ( calc_expr : Box < CalcExpr > ) -> Self {
193+ Self :: Expr ( Box :: new ( LengthExpr :: Calc ( calc_expr) ) )
194+ }
195+
196+ /// Convert to `Box<CalcExpr>`.
197+ ///
198+ /// If the length is already a `CalcExpr`, it will be returned directly.
199+ /// Otherwise, it will be wrapped into a new `CalcExpr`.
200+ pub fn into_calc_expr ( self ) -> Box < CalcExpr > {
201+ match self {
202+ Length :: Expr ( x) => match * x {
203+ LengthExpr :: Calc ( x) => x,
204+ x => Box :: new ( CalcExpr :: Length ( Box :: new ( Length :: Expr ( Box :: new ( x) ) ) ) ) ,
205+ } ,
206+ x => Box :: new ( CalcExpr :: Length ( Box :: new ( x) ) ) ,
207+ }
208+ }
209+
191210 /// Resolve the length value to `f32`.
192211 ///
193212 /// The `relative_length` is used to calculate `...%` length.
@@ -214,7 +233,7 @@ impl Length {
214233 }
215234 }
216235 Length :: Ratio ( x) => relative_length * * x,
217- Length :: Expr ( x) => match x {
236+ Length :: Expr ( x) => match & * * x {
218237 LengthExpr :: Invalid => None ?,
219238 LengthExpr :: Env ( name, default_value) => match name. as_str ( ) {
220239 "safe-area-inset-left" => media_query_status. env . safe_area_inset_left . to_f32 ( ) ,
@@ -268,7 +287,7 @@ impl Length {
268287 ) -> Option < L > {
269288 let r = match self {
270289 Length :: Undefined | Length :: Auto => None ?,
271- Length :: Expr ( x) => match x {
290+ Length :: Expr ( x) => match & * * x {
272291 LengthExpr :: Invalid => None ?,
273292 LengthExpr :: Env ( name, default_value) => match name. as_str ( ) {
274293 "safe-area-inset-left" => media_query_status. env . safe_area_inset_left ,
0 commit comments