From f23947331bf3e6bd6f5e84bdbf5e67cd7b2f06f5 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Thu, 14 May 2020 16:41:30 -0400 Subject: [PATCH] Added some (tentative) things to rule_fn macro --- src/examples.rs | 14 ++++++-------- src/rule.rs | 15 ++++++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/examples.rs b/src/examples.rs index 3cf9ddd..00f66c2 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -63,9 +63,8 @@ pub fn barbs() -> Rule<()> { rotate(&Vector3::y_axis(), -0.2). scale(0.8); - let b = base_verts.clone(); - let barb = rule_fn!((), self_ => { - let mut next_verts = b.clone(); + let barb = rule_fn!(() => |self_, base_verts| { + let mut next_verts = base_verts; let (a0, a1) = next_verts.append_indexed(vert_args(0..4)); let geom = util::parallel_zigzag(next_verts.clone(), b0..bn, a0..a1); @@ -91,9 +90,8 @@ pub fn barbs() -> Rule<()> { rotate(&Vector3::x_axis(), 0.1). scale(0.95); - let b = base_verts.clone(); - let main = rule_fn!((), self_ => { - let mut next_verts = b.clone(); + let main = rule_fn!(() => |self_, base_verts| { + let mut next_verts = base_verts; let (a0, a1) = next_verts.append_indexed(vert_args(0..4)); // This contributes no faces of its own - just vertices. @@ -118,10 +116,10 @@ pub fn barbs() -> Rule<()> { } }); - let base = rule_fn!((), _s => { + let base = rule_fn!(() => |_s, base_verts| { RuleEval { geom: Rc::new(MeshFunc { - verts: base_verts.clone(), + verts: base_verts, faces: vec![ b0, b0 + 1, b0 + 2, b0, b0 + 2, b0 + 3 ], }), // TODO: This might be buggy and leave some vertices lying around diff --git a/src/rule.rs b/src/rule.rs index 8ce3d1b..89cc371 100644 --- a/src/rule.rs +++ b/src/rule.rs @@ -114,15 +114,20 @@ macro_rules! rule { #[macro_export] macro_rules! rule_fn { - ( $Ty:ty, $Self:ident => $Body:expr ) => { - std::rc::Rc::new(move |$Self: std::rc::Rc>| -> RuleEval<$Ty> { - let $Self = $Self.clone(); - $Body - }) + ( $Ty:ty => |$Self:ident $(,$x:ident)*| $Body:expr ) => { + { + $(let $x = $x.clone();)* + std::rc::Rc::new(move |$Self: std::rc::Rc>| -> RuleEval<$Ty> { + $(let $x = $x.clone();)* + let $Self = $Self.clone(); + $Body + }) + } } } // TODO: Shouldn't I fully-qualify Rule & RuleEval? // TODO: Document all of the above macros +// TODO: Why must I clone twice? impl Rule {