diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-08-17 22:04:18 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-08-17 22:20:37 +0200 |
| commit | 594809e35b9e768f1a50926cf5e7a9df41ba7d16 (patch) | |
| tree | 488f201599a67329d7916b9b3ecb73dd27ad24d7 /src/library/elements.rs | |
| parent | c53d98a22f367a9eecfb45d1b22f1be5c6cf908d (diff) | |
Library functions behave more imperatively
- Templates scope state changes
- State-modifying function operate in place instead of returning a template
- Internal template representation contains actual owned nodes instead of a pointer to a syntax tree + an expression map
- No more wide calls
Diffstat (limited to 'src/library/elements.rs')
| -rw-r--r-- | src/library/elements.rs | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/src/library/elements.rs b/src/library/elements.rs index f4577084..f90363bb 100644 --- a/src/library/elements.rs +++ b/src/library/elements.rs @@ -23,9 +23,11 @@ pub fn image(ctx: &mut EvalContext, args: &mut Arguments) -> TypResult<Value> { }) })?; - Ok(Value::template(move |ctx| { - ctx.inline(ImageNode { id, width, height }) - })) + Ok(Value::Template(Template::from_inline(move |_| ImageNode { + id, + width, + height, + }))) } /// `rect`: A rectangle with optional content. @@ -61,22 +63,23 @@ fn rect_impl( fill: Option<Color>, body: Template, ) -> Value { - Value::template(move |ctx| { - let mut stack = ctx.exec_template(&body); + Value::Template(Template::from_inline(move |state| { + let mut stack = body.to_stack(state); stack.aspect = aspect; - let fixed = FixedNode { width, height, child: stack.into() }; + let mut node = FixedNode { width, height, child: stack.into() }.into(); if let Some(fill) = fill { - ctx.inline(BackgroundNode { + node = BackgroundNode { shape: BackgroundShape::Rect, fill: Paint::Color(fill), - child: fixed.into(), - }); - } else { - ctx.inline(fixed); + child: node, + } + .into(); } - }) + + node + })) } /// `ellipse`: An ellipse with optional content. @@ -112,15 +115,15 @@ fn ellipse_impl( fill: Option<Color>, body: Template, ) -> Value { - Value::template(move |ctx| { + Value::Template(Template::from_inline(move |state| { // This padding ratio ensures that the rectangular padded region fits // perfectly into the ellipse. const PAD: f64 = 0.5 - SQRT_2 / 4.0; - let mut stack = ctx.exec_template(&body); + let mut stack = body.to_stack(state); stack.aspect = aspect; - let fixed = FixedNode { + let mut node = FixedNode { width, height, child: PadNode { @@ -128,16 +131,18 @@ fn ellipse_impl( child: stack.into(), } .into(), - }; + } + .into(); if let Some(fill) = fill { - ctx.inline(BackgroundNode { + node = BackgroundNode { shape: BackgroundShape::Ellipse, fill: Paint::Color(fill), - child: fixed.into(), - }); - } else { - ctx.inline(fixed); + child: node, + } + .into(); } - }) + + node + })) } |
