Modify vec_indexed! syntax a little

This commit is contained in:
Chris Hodapp 2020-05-13 14:08:57 -04:00
parent a6449a2b94
commit ce3ca34b70
2 changed files with 16 additions and 11 deletions

View File

@ -50,12 +50,13 @@ pub fn cube_thing() -> Rule<()> {
pub fn barbs() -> Rule<()> {
let (b0, b1);
let (b0, bn);
let base_verts: Vec<VertexUnion> = vec_indexed![
@b0: 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)),
@b1: 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 ],

View File

@ -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
}};
}