From b6ac6d3df05ea7ce71813a1506442d5f5dd73608 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 1 Apr 2021 11:57:47 +0100 Subject: [PATCH] html, SvgAttrs: Change type of SvgAttrs Amazingly this seems to *save* 2k of wasm code. Signed-off-by: Ian Jackson --- base/html.rs | 2 ++ base/misc.rs | 12 ++++++------ daemon/session.rs | 6 +++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/base/html.rs b/base/html.rs index 20f3b130..caff21c7 100644 --- a/base/html.rs +++ b/base/html.rs @@ -48,6 +48,8 @@ impl Debug for Html { pub struct HtmlStr(str); #[derive(Hash,Eq,Ord,PartialEq,PartialOrd)] +#[derive(Serialize,Deserialize)] +#[serde(transparent)] pub struct HtmlLit(&'static str); impl From for &'static HtmlStr { diff --git a/base/misc.rs b/base/misc.rs index 963dc676..426c4ca9 100644 --- a/base/misc.rs +++ b/base/misc.rs @@ -47,20 +47,20 @@ macro_rules! display_as_debug { } pub use crate::display_as_debug; -pub type SvgAttrs = Vec<(String,String)>; +pub type SvgAttrs = Vec<(HtmlLit,Html)>; pub fn space_table_attrs(table_size: PosC) -> SvgAttrs { let PosC { coords: [x, y] } = table_size.into(); vec![ - ("viewBox".to_owned(), format!("0 0 {x} {y}", x=x, y=y) ), - ("width" .to_owned(), (SVG_SCALE * x).to_string() ), - ("height" .to_owned(), (SVG_SCALE * y).to_string() ), + (Html::lit("viewBox"), hformat!("0 0 {} {}", x, y) ), + (Html::lit("width" ), (SVG_SCALE * x).to_html() ), + (Html::lit("height" ), (SVG_SCALE * y).to_html() ), ] } pub fn space_rect_attrs(table_size: PosC) -> SvgAttrs { vec![ - ("width" .to_owned(), table_size.x().to_string() ), - ("height" .to_owned(), table_size.y().to_string() ), + (Html::lit("width" ), table_size.x().to_html() ), + (Html::lit("height"), table_size.y().to_html() ), ] } diff --git a/daemon/session.rs b/daemon/session.rs index 4076a5be..14444f79 100644 --- a/daemon/session.rs +++ b/daemon/session.rs @@ -74,11 +74,11 @@ fn session(form: Json, #[ext] impl SvgAttrs { fn to_html(&self) -> Html { - let mut o = String::new(); + let mut o = Html::new(); for (k,v) in self { - write!(o, r##"{}="{}""##, k, v).unwrap(); + hwrite!(&mut o, r##"{}="{}""##, k, v).unwrap(); } - Html::from_html_string(o) + o } } -- 2.30.2