Skip to content

Commit

Permalink
steven_blocks: Use the ? operator instead of * in macro
Browse files Browse the repository at this point in the history
Previously, only the * and + operators were available, for 0 or more and
1 or more, respectively, so steven_blocks used * for optional tokens
even though only one would be expected in most cases.

Rust version 1.32.0 added a new operator, ?, for zero or one:

https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1320-2019-01-17
> You can now use the ? operator in macro definitions. The ? operator allows
> you to specify zero or one repetitions similar to the * and + operators.
rust-lang/rust#56245

Change to use ? instead of * for these optional repetitions.
Found this while investigating #174.
  • Loading branch information
iceiix committed May 31, 2019
1 parent 19948ff commit 90ff48b
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions blocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ macro_rules! define_blocks {
(
$(
$name:ident {
$(modid $modid:expr,)*
$(modid $modid:expr,)?
props {
$(
$fname:ident : $ftype:ty = [$($val:expr),+],
)*
},
$(data $datafunc:expr,)*
$(offset $offsetfunc:expr,)*
$(material $mat:expr,)*
$(data $datafunc:expr,)?
$(offset $offsetfunc:expr,)?
$(material $mat:expr,)?
model $model:expr,
$(variant $variant:expr,)*
$(tint $tint:expr,)*
$(collision $collision:expr,)*
$(update_state ($world:ident, $pos:ident) => $update_state:expr,)*
$(multipart ($mkey:ident, $mval:ident) => $multipart:expr,)*
$(variant $variant:expr,)?
$(tint $tint:expr,)?
$(collision $collision:expr,)?
$(update_state ($world:ident, $pos:ident) => $update_state:expr,)?
$(multipart ($mkey:ident, $mval:ident) => $multipart:expr,)?
}
)+
) => (
Expand All @@ -76,7 +76,7 @@ macro_rules! define_blocks {
$name {
$(
$fname : $ftype,
)*
)?
},
)+
}
Expand All @@ -90,7 +90,7 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
internal_ids::$name
}
Expand All @@ -103,12 +103,12 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(
let data: Option<usize> = ($datafunc).map(|v| v);
return data;
)*
)?
Some(0)
}
)+
Expand All @@ -120,16 +120,16 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(
let offset: Option<usize> = ($offsetfunc).map(|v| v);
return offset;
)*
)?
$(
let data: Option<usize> = ($datafunc).map(|v| v);
return data;
)*
)?
Some(0)
}
)+
Expand All @@ -141,11 +141,11 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(
return Some($modid);
)*
)?
None
}
)+
Expand Down Expand Up @@ -181,9 +181,9 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(return $mat;)*
$(return $mat;)?
material::SOLID
}
)+
Expand All @@ -195,7 +195,7 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
let parts = $model;
(String::from(parts.0), String::from(parts.1))
Expand All @@ -209,9 +209,9 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(return String::from($variant);)*
$(return String::from($variant);)?
"normal".to_owned()
}
)+
Expand All @@ -223,9 +223,9 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(return $tint;)*
$(return $tint;)?
TintType::Default
}
)+
Expand All @@ -237,9 +237,9 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(return $collision;)*
$(return $collision;)?
vec![Aabb3::new(
Point3::new(0.0, 0.0, 0.0),
Point3::new(1.0, 1.0, 1.0)
Expand All @@ -254,15 +254,15 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(
let $world = world;
let $pos = pos;
return $update_state;
)*
)?
Block::$name {
$($fname: $fname,)*
$($fname: $fname,)?
}
}
)+
Expand All @@ -274,13 +274,13 @@ macro_rules! define_blocks {
match *self {
$(
Block::$name {
$($fname,)*
$($fname,)?
} => {
$(
let $mkey = key;
let $mval = val;
return $multipart;
)*
)?
false
}
)+
Expand All @@ -307,15 +307,15 @@ macro_rules! define_blocks {
}
#[allow(non_camel_case_types)]
struct CombinationIterState<$($fname),*> {
$($fname: $fname,)*
$($fname: $fname,)?
}
#[allow(non_camel_case_types)]
struct CombinationIterOrig<$($fname),*> {
$($fname: $fname,)*
$($fname: $fname,)?
}
#[allow(non_camel_case_types)]
struct CombinationIterCurrent {
$($fname: $ftype,)*
$($fname: $ftype,)?
}

#[allow(non_camel_case_types)]
Expand All @@ -332,7 +332,7 @@ macro_rules! define_blocks {
return Some(Block::$name {
$(
$fname: self.current.$fname,
)*
)?
});
}
let mut has_value = false;
Expand All @@ -345,15 +345,15 @@ macro_rules! define_blocks {
}
self.state.$fname = self.orig.$fname.clone();
self.current.$fname = self.state.$fname.next().unwrap();
)*
)?
self.finished = true;
return None;
}
if has_value {
Some(Block::$name {
$(
$fname: self.current.$fname,
)*
)?
})
} else {
None
Expand All @@ -368,13 +368,13 @@ macro_rules! define_blocks {
finished: false,
first: true,
orig: CombinationIterOrig {
$($fname: $fname.clone(),)*
$($fname: $fname.clone(),)?
},
current: CombinationIterCurrent {
$($fname: $fname.next().unwrap(),)*
$($fname: $fname.next().unwrap(),)?
},
state: CombinationIterState {
$($fname: $fname,)*
$($fname: $fname,)?
}
}
}
Expand Down

0 comments on commit 90ff48b

Please sign in to comment.