diff --git a/src/examples.rs b/src/examples.rs index 28ada6b..9c4f1e0 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -50,12 +50,13 @@ pub fn cube_thing() -> Rule<()> { pub fn barbs() -> Rule<()> { - let (b0, b1); + let (b0, bn); let base_verts: Vec = vec_indexed![ - @b0: VertexUnion::Vertex(vertex(-0.5, -0.5, 0.0)), - VertexUnion::Vertex(vertex(-0.5, 0.5, 0.0)), - VertexUnion::Vertex(vertex( 0.5, 0.5, 0.0)), - @b1: VertexUnion::Vertex(vertex( 0.5, -0.5, 0.0)), + @b0 VertexUnion::Vertex(vertex(-0.5, -0.5, 0.0)), + VertexUnion::Vertex(vertex(-0.5, 0.5, 0.0)), + VertexUnion::Vertex(vertex( 0.5, 0.5, 0.0)), + VertexUnion::Vertex(vertex( 0.5, -0.5, 0.0)), + @bn, ]; let n = base_verts.len(); @@ -71,7 +72,7 @@ pub fn barbs() -> Rule<()> { &mut (0..4).map(|i| VertexUnion::Arg(i)).collect() ); - let geom = util::parallel_zigzag(next_verts.clone(), b0..b1+1, a0..a1); + let geom = util::parallel_zigzag(next_verts.clone(), b0..bn, a0..a1); let final_geom = MeshFunc { verts: (0..4).map(|i| VertexUnion::Arg(i)).collect(), faces: vec![ 0, 2, 1, 0, 3, 2 ], diff --git a/src/util.rs b/src/util.rs index 2bd933a..b7779c4 100644 --- a/src/util.rs +++ b/src/util.rs @@ -4,17 +4,21 @@ use crate::xform::{Vertex}; //use crate::rule::{Rule, Child}; /// This is like `vec!`, but it can handle elements that are given -/// with `@var: element` rather than `element`, e.g. like -/// `vec_indexed![foo, bar, @a: baz, quux]`. The variable (which must +/// with `@var element` rather than `element`, e.g. like +/// `vec_indexed![foo, bar, @a baz, quux]`. The variable (which must /// already be declared and a `usize`) is then assigned the index of the -/// element it precedes. This can be used any number of times with +/// element it precedes (2). This can be used any number of times with /// different elements and indices. +/// +/// It can also be used like `vec_indexed![foo, bar, baz, @b,]` in which +/// case `b` is the index after 'baz' (3) rather than before. This still +/// requires a trailing comma. #[macro_export] macro_rules! vec_indexed { // Thank you to GhostOfSteveJobs and Rantanen in the Rust discord. - ($( $(@ $Index:ident :)? $Value:expr,)*) => {{ + ($( $(@ $Index:ident)? $($Value:expr)?,)*) => {{ let mut v = Vec::new(); - $( $($Index = v.len();)? v.push($Value); )* + $( $($Index = v.len();)? $(v.push($Value);)? )* v }}; }