From 82a12fc167b8a6ea27a2bcd8ee30e4abdc3b26a5 Mon Sep 17 00:00:00 2001 From: LastLeaf Date: Wed, 11 Mar 2026 16:25:48 +0800 Subject: [PATCH 1/4] optimize: better length expr boxing for better memory usage --- .../src/parser/property_value/calc.rs | 10 +- .../src/parser/property_value/mod.rs | 8 +- float-pigment-css/src/typing.rs | 25 ++++- float-pigment-css/src/typing_stringify.rs | 2 +- float-pigment-css/tests/calc.rs | 94 +++++++++---------- float-pigment-css/tests/env.rs | 8 +- 6 files changed, 80 insertions(+), 67 deletions(-) diff --git a/float-pigment-css/src/parser/property_value/calc.rs b/float-pigment-css/src/parser/property_value/calc.rs index 0b3bca0..5fc37f7 100644 --- a/float-pigment-css/src/parser/property_value/calc.rs +++ b/float-pigment-css/src/parser/property_value/calc.rs @@ -334,17 +334,11 @@ pub(crate) enum ExpectValueType { fn combine_calc_expr(operator: Operator, lhs: CalcExpr, rhs: CalcExpr) -> CalcExpr { // Unwrap nested `Length::Expr(Calc(...))` to flatten the expression tree. let final_lhs = match lhs { - CalcExpr::Length(length_expr) => match *length_expr { - Length::Expr(LengthExpr::Calc(calc_expr)) => calc_expr, - other => Box::new(CalcExpr::Length(Box::new(other))), - }, + CalcExpr::Length(length_expr) => length_expr.into_calc_expr(), other => Box::new(other), }; let final_rhs = match rhs { - CalcExpr::Length(length_expr) => match *length_expr { - Length::Expr(LengthExpr::Calc(calc_expr)) => calc_expr, - other => Box::new(CalcExpr::Length(Box::new(other))), - }, + CalcExpr::Length(length_expr) => length_expr.into_calc_expr(), other => Box::new(other), }; match operator { diff --git a/float-pigment-css/src/parser/property_value/mod.rs b/float-pigment-css/src/parser/property_value/mod.rs index afc82c9..53decad 100644 --- a/float-pigment-css/src/parser/property_value/mod.rs +++ b/float-pigment-css/src/parser/property_value/mod.rs @@ -223,10 +223,10 @@ fn parse_length_inner<'a, 't: 'a, 'i: 't>( env_default_value(parser, properties, st) }) })?; - return Ok(Length::Expr(LengthExpr::Env( + return Ok(Length::Expr(Box::new(LengthExpr::Env( name.into(), Box::new(default_value.unwrap_or(Length::Undefined)), - ))); + )))); } "calc" => { return parse_calc_inner(parser, properties, st, ExpectValueType::NumberAndLength) @@ -234,7 +234,7 @@ fn parse_length_inner<'a, 't: 'a, 'i: 't>( if let Some(r) = ComputeCalcExpr::::try_compute(&ret) { return r; } - Length::Expr(LengthExpr::Calc(Box::new(ret))) + Length::Expr(Box::new(LengthExpr::Calc(Box::new(ret)))) }); } _ => {} @@ -401,7 +401,7 @@ pub(crate) fn percentage<'a, 't: 'a, 'i: 't>( if let Some(r) = ComputeCalcExpr::::try_compute(&ret) { return r; } - Length::Expr(LengthExpr::Calc(Box::new(ret))) + Length::Expr(Box::new(LengthExpr::Calc(Box::new(ret)))) }); } } diff --git a/float-pigment-css/src/typing.rs b/float-pigment-css/src/typing.rs index 1b5b126..298318f 100644 --- a/float-pigment-css/src/typing.rs +++ b/float-pigment-css/src/typing.rs @@ -150,7 +150,7 @@ pub enum Length { Rpx(f32), Em(f32), Ratio(f32), - Expr(LengthExpr), + Expr(Box), Vmin(f32), Vmax(f32), } @@ -188,6 +188,25 @@ impl Length { } } + /// Create a new `Length` from a `CalcExpr`. + pub fn new_calc_expr(calc_expr: Box) -> Self { + Self::Expr(Box::new(LengthExpr::Calc(calc_expr))) + } + + /// Convert to `Box`. + /// + /// If the length is already a `CalcExpr`, it will be returned directly. + /// Otherwise, it will be wrapped into a new `CalcExpr`. + pub fn into_calc_expr(self) -> Box { + match self { + Length::Expr(x) => match *x { + LengthExpr::Calc(x) => x, + x => Box::new(CalcExpr::Length(Box::new(Length::Expr(Box::new(x))))), + }, + x => Box::new(CalcExpr::Length(Box::new(x))), + } + } + /// Resolve the length value to `f32`. /// /// The `relative_length` is used to calculate `...%` length. @@ -214,7 +233,7 @@ impl Length { } } Length::Ratio(x) => relative_length * *x, - Length::Expr(x) => match x { + Length::Expr(x) => match &**x { LengthExpr::Invalid => None?, LengthExpr::Env(name, default_value) => match name.as_str() { "safe-area-inset-left" => media_query_status.env.safe_area_inset_left.to_f32(), @@ -268,7 +287,7 @@ impl Length { ) -> Option { let r = match self { Length::Undefined | Length::Auto => None?, - Length::Expr(x) => match x { + Length::Expr(x) => match &**x { LengthExpr::Invalid => None?, LengthExpr::Env(name, default_value) => match name.as_str() { "safe-area-inset-left" => media_query_status.env.safe_area_inset_left, diff --git a/float-pigment-css/src/typing_stringify.rs b/float-pigment-css/src/typing_stringify.rs index 68be2e1..1a17262 100644 --- a/float-pigment-css/src/typing_stringify.rs +++ b/float-pigment-css/src/typing_stringify.rs @@ -273,7 +273,7 @@ impl fmt::Display for Length { &tmp } Length::Expr(expr) => { - match expr { + match &**expr { LengthExpr::Calc(calc_expr) => { tmp = calc_expr.to_string(); &tmp diff --git a/float-pigment-css/tests/calc.rs b/float-pigment-css/tests/calc.rs index e80c9c6..c334dbb 100644 --- a/float-pigment-css/tests/calc.rs +++ b/float-pigment-css/tests/calc.rs @@ -53,7 +53,7 @@ pub fn calc() { // margin_left, // "margin-left", // "calc(10px + 20vh + 30px)", - // Length::Expr(LengthExpr::Calc(CalcExpr::Plus( + // Length::new_calc_expr(CalcExpr::Plus( // Box::new(CalcExpr::Length(Box::new(Length::Px(40.)))), // Box::new(CalcExpr::Length(Box::new(Length::Vh(20.)))) // ))) @@ -62,13 +62,13 @@ pub fn calc() { margin_left, "margin-left", "calc(10rpx + (20vh + 30px))", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Rpx(10.)))), Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Vh(20.)))), Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))) )) - )))) + ))) ); test_parse_property!( transform, @@ -175,7 +175,7 @@ pub fn calc_ch_ex_lang() { left, "left", "calc(1vh + 5vw + 10px + 1rem)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Plus( Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Vh(1.)))), @@ -184,7 +184,7 @@ pub fn calc_ch_ex_lang() { Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))) )), Box::new(CalcExpr::Length(Box::new(Length::Rem(1.)))) - )))) + ))) ); // FIXME @@ -192,7 +192,7 @@ pub fn calc_ch_ex_lang() { // font_size, // "font-size", // "calc(17px + 0.5 * (1rem - 16px))", - // Length::Expr(LengthExpr::Calc(CalcExpr::Plus( + // Length::new_calc_expr(CalcExpr::Plus( // Box::new(CalcExpr::Length(Box::new(Length::Px(9.)))), // Box::new(CalcExpr::Length(Box::new(Length::Rem(0.5)))) // ))) @@ -207,20 +207,20 @@ pub fn calc_height_block_1() { height, "height", "calc(25px + 50%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(25.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - )))) + ))) ); test_parse_property!( height, "height", "calc(150% / 2 - 30px)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.75)))), Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))), - )))) + ))) ); // FIXME @@ -235,10 +235,10 @@ pub fn calc_height_block_1() { height, "height", "calc(40px - 10%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Px(40.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))), - )))) + ))) ); } @@ -255,13 +255,13 @@ pub fn calc_in_calc() { height, "height", "calc(calc(10px + 10%) + 10px)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))) )), Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))) - )))) + ))) ); } @@ -271,55 +271,55 @@ pub fn calc_margin_block_1() { margin_top, "margin", "calc(10px + 1%) 0 0 0", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), - )))) + ))) ); test_parse_property!( margin_right, "margin", "0 calc(10px + 1%) 0 0", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), - )))) + ))) ); test_parse_property!( margin_bottom, "margin", "0 0 calc(10px + 1%) 0", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), - )))) + ))) ); test_parse_property!( margin_left, "margin", "0 0 0 calc(10px + 1%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), - )))) + ))) ); test_parse_property!( margin_left, "margin", "calc(10px + 1%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), - )))) + ))) ); test_parse_property!( margin_bottom, "margin", "calc(10px + 1%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), - )))) + ))) ); } @@ -331,19 +331,19 @@ pub fn calc_max_height_block_1() { max_height, "max-height", "calc(25px + 50%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Px(25.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - )))) + ))) ); test_parse_property!( max_height, "max-height", "calc(150% / 2 - 30px)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.75)))), Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))), - )))) + ))) ); // FIXME // test_parse_property!( @@ -356,10 +356,10 @@ pub fn calc_max_height_block_1() { max_height, "max-height", "calc(40px - 10%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Px(40.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))), - )))) + ))) ); } @@ -369,10 +369,10 @@ pub fn calc_max_width_block_1() { max_width, "max-width", "calc(50% - 3px)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), - )))) + ))) ); // FIXME @@ -380,7 +380,7 @@ pub fn calc_max_width_block_1() { // max_width, // "max-width", // "calc(25% - 3px + 25%)", - // Length::Expr(LengthExpr::Calc(CalcExpr::Sub( + // Length::new_calc_expr(CalcExpr::Sub( // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), // ))) @@ -391,7 +391,7 @@ pub fn calc_max_width_block_1() { // max_width, // "max-width", // "calc(25% - 3px + 12.5% * 2)", - // Length::Expr(LengthExpr::Calc(CalcExpr::Sub( + // Length::new_calc_expr(CalcExpr::Sub( // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), // ))) @@ -402,7 +402,7 @@ pub fn calc_max_width_block_1() { // max_width, // "max-width", // "calc(25% - 3px + 12.5%*2)", - // Length::Expr(LengthExpr::Calc(CalcExpr::Sub( + // Length::new_calc_expr(CalcExpr::Sub( // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), // ))) @@ -413,7 +413,7 @@ pub fn calc_max_width_block_1() { // max_width, // "max-width", // "calc(25% - 3px + 2*12.5%)", - // Length::Expr(LengthExpr::Calc(CalcExpr::Sub( + // Length::new_calc_expr(CalcExpr::Sub( // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), // ))) @@ -424,7 +424,7 @@ pub fn calc_max_width_block_1() { // max_width, // "max-width", // "calc(25% - 3px + 2 * 12.5%)", - // Length::Expr(LengthExpr::Calc(CalcExpr::Sub( + // Length::new_calc_expr(CalcExpr::Sub( // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), // ))) @@ -446,19 +446,19 @@ pub fn calc_offset_absolute_left_1() { left, "left", "calc(-25px - 50%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Px(-25.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - )))) + ))) ); test_parse_property!( left, "left", "calc(-150% / 2 - 30px)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Ratio(-0.75)))), Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))), - )))) + ))) ); // FIXME // test_parse_property!( @@ -471,10 +471,10 @@ pub fn calc_offset_absolute_left_1() { left, "left", "calc(-40px - 10%)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Sub( + Length::new_calc_expr(Box::new(CalcExpr::Sub( Box::new(CalcExpr::Length(Box::new(Length::Px(-40.)))), Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))), - )))) + ))) ); } @@ -486,10 +486,10 @@ pub fn calc_rem_lang() { left, "left", "calc(1rem + 1em)", - Length::Expr(LengthExpr::Calc(Box::new(CalcExpr::Plus( + Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Length(Box::new(Length::Rem(1.)))), Box::new(CalcExpr::Length(Box::new(Length::Em(1.)))), - )))) + ))) ); } diff --git a/float-pigment-css/tests/env.rs b/float-pigment-css/tests/env.rs index d76bbe9..5daed16 100644 --- a/float-pigment-css/tests/env.rs +++ b/float-pigment-css/tests/env.rs @@ -23,19 +23,19 @@ pub fn env() { margin_left, "margin-left", "env(safe-area-inset-bottom, 10px)", - Length::Expr(LengthExpr::Env( + Length::Expr(Box::new(LengthExpr::Env( "safe-area-inset-bottom".into(), Box::new(Length::Px(10.)) - )) + ))) ); test_parse_property!( margin_left, "margin-left", "env(safe-area-inset-bottom, 10p)", - Length::Expr(LengthExpr::Env( + Length::Expr(Box::new(LengthExpr::Env( "safe-area-inset-bottom".into(), Box::new(Length::Px(0.)) - )) + ))) ); } From 8b4ac033383708c80a28fb4e49bd83770a69f4fa Mon Sep 17 00:00:00 2001 From: LastLeaf Date: Wed, 11 Mar 2026 16:37:20 +0800 Subject: [PATCH 2/4] optimize: remove length boxing in CalcExpr for better performance --- .../src/parser/property_value/calc.rs | 12 +- float-pigment-css/src/typing.rs | 8 +- float-pigment-css/tests/calc.rs | 116 +++++++++--------- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/float-pigment-css/src/parser/property_value/calc.rs b/float-pigment-css/src/parser/property_value/calc.rs index 5fc37f7..55caa62 100644 --- a/float-pigment-css/src/parser/property_value/calc.rs +++ b/float-pigment-css/src/parser/property_value/calc.rs @@ -38,7 +38,7 @@ impl CalcExpr { CalcExpr::Angle(angle) => !matches!(angle.as_ref(), Angle::Calc(_)), CalcExpr::Number(num) => !matches!(num.as_ref(), Number::Calc(_)), CalcExpr::Length(length) => !matches!( - length.as_ref(), + length, Length::Expr(_) | Length::Auto | Length::Undefined ), _ => false, @@ -69,7 +69,7 @@ impl CalcExpr { } else { length.to_f32() / rhs }; - let ret = match length.as_ref() { + let ret = match length { Length::Px(_) => Length::Px(v), Length::Em(_) => Length::Em(v), Length::Rpx(_) => Length::Rpx(v), @@ -81,7 +81,7 @@ impl CalcExpr { Length::Vmin(_) => Length::Vmin(v), _ => unreachable!(), }; - CalcExpr::Length(Box::new(ret)) + CalcExpr::Length(ret) } CalcExpr::Number(num) => { let ret = if mul { @@ -176,7 +176,7 @@ impl ComputeCalcExpr { _ => None, } } - CalcExpr::Length(l) => match l.as_ref() { + CalcExpr::Length(l) => match l { Length::Ratio(ratio) => Some(Angle::from_ratio(*ratio)), _ => None, }, @@ -250,7 +250,7 @@ impl LengthUnit { impl ComputeCalcExpr { pub(crate) fn try_compute(expr: &CalcExpr) -> Option { match expr { - CalcExpr::Length(l) => Some(*l.clone()), + CalcExpr::Length(l) => Some(l.clone()), CalcExpr::Angle(_) | CalcExpr::Number(_) => None, CalcExpr::Plus(l, r) | CalcExpr::Sub(l, r) => { let l = Self::try_compute(l)?; @@ -495,7 +495,7 @@ fn parse_calc_value<'a, 't: 'a, 'i: 't>( if expect_type == ExpectValueType::NumberAndLength || expect_type == ExpectValueType::AngleAndLength { - return Ok(CalcExpr::Length(Box::new(length))); + return Ok(CalcExpr::Length(length)); } } // match angle diff --git a/float-pigment-css/src/typing.rs b/float-pigment-css/src/typing.rs index 298318f..d926925 100644 --- a/float-pigment-css/src/typing.rs +++ b/float-pigment-css/src/typing.rs @@ -41,7 +41,7 @@ pub enum ImportantBitSet { #[cfg_attr(debug_assertions, derive(CompatibilityEnumCheck))] pub enum CalcExpr { /// A length, e.g. `2px`. - Length(Box), + Length(Length), /// A number, e.g. `0.1`. Number(Box), /// An angle, e.g. `45deg`. @@ -58,7 +58,7 @@ pub enum CalcExpr { impl Default for CalcExpr { fn default() -> Self { - Self::Length(Box::new(Length::Undefined)) + Self::Length(Length::Undefined) } } @@ -201,9 +201,9 @@ impl Length { match self { Length::Expr(x) => match *x { LengthExpr::Calc(x) => x, - x => Box::new(CalcExpr::Length(Box::new(Length::Expr(Box::new(x))))), + x => Box::new(CalcExpr::Length(Length::Expr(Box::new(x)))), }, - x => Box::new(CalcExpr::Length(Box::new(x))), + x => Box::new(CalcExpr::Length(x)), } } diff --git a/float-pigment-css/tests/calc.rs b/float-pigment-css/tests/calc.rs index c334dbb..b3e4548 100644 --- a/float-pigment-css/tests/calc.rs +++ b/float-pigment-css/tests/calc.rs @@ -54,8 +54,8 @@ pub fn calc() { // "margin-left", // "calc(10px + 20vh + 30px)", // Length::new_calc_expr(CalcExpr::Plus( - // Box::new(CalcExpr::Length(Box::new(Length::Px(40.)))), - // Box::new(CalcExpr::Length(Box::new(Length::Vh(20.)))) + // Box::new(CalcExpr::Length(Length::Px(40.)))), + // Box::new(CalcExpr::Length(Length::Vh(20.)))) // ))) // ); test_parse_property!( @@ -63,10 +63,10 @@ pub fn calc() { "margin-left", "calc(10rpx + (20vh + 30px))", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Rpx(10.)))), + Box::new(CalcExpr::Length(Length::Rpx(10.))), Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Vh(20.)))), - Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))) + Box::new(CalcExpr::Length(Length::Vh(20.))), + Box::new(CalcExpr::Length(Length::Px(30.))) )) ))) ); @@ -178,12 +178,12 @@ pub fn calc_ch_ex_lang() { Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Plus( Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Vh(1.)))), - Box::new(CalcExpr::Length(Box::new(Length::Vw(5.)))) + Box::new(CalcExpr::Length(Length::Vh(1.))), + Box::new(CalcExpr::Length(Length::Vw(5.))) )), - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))) + Box::new(CalcExpr::Length(Length::Px(10.))) )), - Box::new(CalcExpr::Length(Box::new(Length::Rem(1.)))) + Box::new(CalcExpr::Length(Length::Rem(1.))) ))) ); @@ -193,8 +193,8 @@ pub fn calc_ch_ex_lang() { // "font-size", // "calc(17px + 0.5 * (1rem - 16px))", // Length::new_calc_expr(CalcExpr::Plus( - // Box::new(CalcExpr::Length(Box::new(Length::Px(9.)))), - // Box::new(CalcExpr::Length(Box::new(Length::Rem(0.5)))) + // Box::new(CalcExpr::Length(Length::Px(9.)))), + // Box::new(CalcExpr::Length(Length::Rem(0.5)))) // ))) // ); } @@ -208,8 +208,8 @@ pub fn calc_height_block_1() { "height", "calc(25px + 50%)", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(25.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), + Box::new(CalcExpr::Length(Length::Px(25.))), + Box::new(CalcExpr::Length(Length::Ratio(0.5))), ))) ); @@ -218,8 +218,8 @@ pub fn calc_height_block_1() { "height", "calc(150% / 2 - 30px)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.75)))), - Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))), + Box::new(CalcExpr::Length(Length::Ratio(0.75))), + Box::new(CalcExpr::Length(Length::Px(30.))), ))) ); @@ -236,8 +236,8 @@ pub fn calc_height_block_1() { "height", "calc(40px - 10%)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Px(40.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))), + Box::new(CalcExpr::Length(Length::Px(40.))), + Box::new(CalcExpr::Length(Length::Ratio(0.1))), ))) ); } @@ -257,10 +257,10 @@ pub fn calc_in_calc() { "calc(calc(10px + 10%) + 10px)", Length::new_calc_expr(Box::new(CalcExpr::Plus( Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))) + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.1))) )), - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))) + Box::new(CalcExpr::Length(Length::Px(10.))) ))) ); } @@ -272,8 +272,8 @@ pub fn calc_margin_block_1() { "margin", "calc(10px + 1%) 0 0 0", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.01))), ))) ); test_parse_property!( @@ -281,8 +281,8 @@ pub fn calc_margin_block_1() { "margin", "0 calc(10px + 1%) 0 0", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.01))), ))) ); test_parse_property!( @@ -290,8 +290,8 @@ pub fn calc_margin_block_1() { "margin", "0 0 calc(10px + 1%) 0", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.01))), ))) ); test_parse_property!( @@ -299,8 +299,8 @@ pub fn calc_margin_block_1() { "margin", "0 0 0 calc(10px + 1%)", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.01))), ))) ); test_parse_property!( @@ -308,8 +308,8 @@ pub fn calc_margin_block_1() { "margin", "calc(10px + 1%)", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.01))), ))) ); test_parse_property!( @@ -317,8 +317,8 @@ pub fn calc_margin_block_1() { "margin", "calc(10px + 1%)", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(10.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.01)))), + Box::new(CalcExpr::Length(Length::Px(10.))), + Box::new(CalcExpr::Length(Length::Ratio(0.01))), ))) ); } @@ -332,8 +332,8 @@ pub fn calc_max_height_block_1() { "max-height", "calc(25px + 50%)", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Px(25.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), + Box::new(CalcExpr::Length(Length::Px(25.))), + Box::new(CalcExpr::Length(Length::Ratio(0.5))), ))) ); test_parse_property!( @@ -341,8 +341,8 @@ pub fn calc_max_height_block_1() { "max-height", "calc(150% / 2 - 30px)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.75)))), - Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))), + Box::new(CalcExpr::Length(Length::Ratio(0.75))), + Box::new(CalcExpr::Length(Length::Px(30.))), ))) ); // FIXME @@ -357,8 +357,8 @@ pub fn calc_max_height_block_1() { "max-height", "calc(40px - 10%)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Px(40.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))), + Box::new(CalcExpr::Length(Length::Px(40.))), + Box::new(CalcExpr::Length(Length::Ratio(0.1))), ))) ); } @@ -370,8 +370,8 @@ pub fn calc_max_width_block_1() { "max-width", "calc(50% - 3px)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), + Box::new(CalcExpr::Length(Length::Ratio(0.5))), + Box::new(CalcExpr::Length(Length::Px(3.))), ))) ); @@ -381,8 +381,8 @@ pub fn calc_max_width_block_1() { // "max-width", // "calc(25% - 3px + 25%)", // Length::new_calc_expr(CalcExpr::Sub( - // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), + // Box::new(CalcExpr::Length(Length::Ratio(0.5))), + // Box::new(CalcExpr::Length(Length::Px(3.))), // ))) // ); @@ -392,8 +392,8 @@ pub fn calc_max_width_block_1() { // "max-width", // "calc(25% - 3px + 12.5% * 2)", // Length::new_calc_expr(CalcExpr::Sub( - // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), + // Box::new(CalcExpr::Length(Length::Ratio(0.5))), + // Box::new(CalcExpr::Length(Length::Px(3.))), // ))) // ); @@ -403,8 +403,8 @@ pub fn calc_max_width_block_1() { // "max-width", // "calc(25% - 3px + 12.5%*2)", // Length::new_calc_expr(CalcExpr::Sub( - // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), + // Box::new(CalcExpr::Length(Length::Ratio(0.5))), + // Box::new(CalcExpr::Length(Length::Px(3.))), // ))) // ); @@ -414,8 +414,8 @@ pub fn calc_max_width_block_1() { // "max-width", // "calc(25% - 3px + 2*12.5%)", // Length::new_calc_expr(CalcExpr::Sub( - // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), + // Box::new(CalcExpr::Length(Length::Ratio(0.5))), + // Box::new(CalcExpr::Length(Length::Px(3.))), // ))) // ); @@ -425,8 +425,8 @@ pub fn calc_max_width_block_1() { // "max-width", // "calc(25% - 3px + 2 * 12.5%)", // Length::new_calc_expr(CalcExpr::Sub( - // Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), - // Box::new(CalcExpr::Length(Box::new(Length::Px(3.)))), + // Box::new(CalcExpr::Length(Length::Ratio(0.5))), + // Box::new(CalcExpr::Length(Length::Px(3.))), // ))) // ); @@ -447,8 +447,8 @@ pub fn calc_offset_absolute_left_1() { "left", "calc(-25px - 50%)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Px(-25.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.5)))), + Box::new(CalcExpr::Length(Length::Px(-25.))), + Box::new(CalcExpr::Length(Length::Ratio(0.5))), ))) ); test_parse_property!( @@ -456,8 +456,8 @@ pub fn calc_offset_absolute_left_1() { "left", "calc(-150% / 2 - 30px)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Ratio(-0.75)))), - Box::new(CalcExpr::Length(Box::new(Length::Px(30.)))), + Box::new(CalcExpr::Length(Length::Ratio(-0.75))), + Box::new(CalcExpr::Length(Length::Px(30.))), ))) ); // FIXME @@ -472,8 +472,8 @@ pub fn calc_offset_absolute_left_1() { "left", "calc(-40px - 10%)", Length::new_calc_expr(Box::new(CalcExpr::Sub( - Box::new(CalcExpr::Length(Box::new(Length::Px(-40.)))), - Box::new(CalcExpr::Length(Box::new(Length::Ratio(0.1)))), + Box::new(CalcExpr::Length(Length::Px(-40.))), + Box::new(CalcExpr::Length(Length::Ratio(0.1))), ))) ); } @@ -487,8 +487,8 @@ pub fn calc_rem_lang() { "left", "calc(1rem + 1em)", Length::new_calc_expr(Box::new(CalcExpr::Plus( - Box::new(CalcExpr::Length(Box::new(Length::Rem(1.)))), - Box::new(CalcExpr::Length(Box::new(Length::Em(1.)))), + Box::new(CalcExpr::Length(Length::Rem(1.))), + Box::new(CalcExpr::Length(Length::Em(1.))), ))) ); } From 9c7b48c01e96d01616856652a08c6768289cc79b Mon Sep 17 00:00:00 2001 From: LastLeaf Date: Wed, 11 Mar 2026 16:46:31 +0800 Subject: [PATCH 3/4] fmt: clippy --- float-pigment-css/src/parser/property_value/calc.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/float-pigment-css/src/parser/property_value/calc.rs b/float-pigment-css/src/parser/property_value/calc.rs index 55caa62..5c2df76 100644 --- a/float-pigment-css/src/parser/property_value/calc.rs +++ b/float-pigment-css/src/parser/property_value/calc.rs @@ -176,10 +176,7 @@ impl ComputeCalcExpr { _ => None, } } - CalcExpr::Length(l) => match l { - Length::Ratio(ratio) => Some(Angle::from_ratio(*ratio)), - _ => None, - }, + CalcExpr::Length(Length::Ratio(ratio)) => Some(Angle::from_ratio(*ratio)), _ => None, } } From 9313c29db4843140fdf8bd181034e0a6fa229685 Mon Sep 17 00:00:00 2001 From: LastLeaf Date: Wed, 11 Mar 2026 16:52:38 +0800 Subject: [PATCH 4/4] fmt: clippy --- float-pigment-css/src/sheet/borrow_resource.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/float-pigment-css/src/sheet/borrow_resource.rs b/float-pigment-css/src/sheet/borrow_resource.rs index 2ceac08..f6130a1 100644 --- a/float-pigment-css/src/sheet/borrow_resource.rs +++ b/float-pigment-css/src/sheet/borrow_resource.rs @@ -10,6 +10,7 @@ use super::{borrow::Array, str_store::*}; #[repr(C)] #[derive(Debug, Serialize, Deserialize)] +#[allow(dead_code)] pub(crate) enum StyleSheetImportIndex { None, V1(StyleSheetImportIndexV1), @@ -17,6 +18,7 @@ pub(crate) enum StyleSheetImportIndex { #[repr(C)] #[derive(Debug)] +#[allow(dead_code)] pub(crate) struct StyleSheetImportIndexV1 { buf: StrBuffer, deps: Array<(StrRef, Array)>,