I'm trying to create macros to make it easier to create small instances of a custom tree type. To do this, I would like to specify the child objects as either nodes or integers (without explicitly converting them to type Node). My attempt below is that it does not work, since the type is $xresolved as MDTree, with the message expected enum MDTree, found integral variable.
pub struct MultiNode {
children: Vec<MDTree>
}
impl MultiNode {
pub fn new(children: Vec<MDTree>) -> Box<MultiNode> {
return Box::new(MultiNode { children: children });
}
}
pub enum MDTree {
Single(u32),
Prime(Box<MultiNode>),
Degenerate(Box<MultiNode>),
}
macro_rules! mod_single {
($x:expr) => { MDTree::Single($x) }
}
macro_rules! mod_multi {
($($x:expr),*) => {{
let mut children: Vec<MDTree> = Vec::new();
$(
match $x {
0...4294967295 => { children.push(mod_single!($x)); }
_ => { children.push($x); }
}
)*
MultiNode::new(children)
}}
}
macro_rules! mod_prime {
($($x:expr),*) => { MDTree::Prime(mod_multi!($($x),*)) }
}
macro_rules! mod_degen {
($($x:expr),*) => { MDTree::Degenerate(mod_multi!($($x),*)) }
}
fn main() {
let md: MDTree = mod_prime!(0, mod_degen!(1,2));
}
Is there a way to fix this without recording mod_prime!(mod_single(0), mod_degen!(mod_single(1),mod_single(2)))or similar?
source
share