【发布时间】:2016-12-30 06:15:08
【问题描述】:
在宏中使用ty 时,这几乎适用于我尝试过的所有情况。
但是,它似乎不能用于声明新的struct 实例。
例如:$my_type { some_member: some_value }
一个更全面的例子
macro_rules! generic_impl {
($my_type:ty) => {
impl $rect_ty {
pub fn flip(&self) -> $my_type { self.some_method() }
pub fn swap(&self, &other: $my_type) -> { self.some_swap_method(other) }
// so far so good!
// now our troubles start :(
pub fn create(&self) -> $my_type {
return $my_type { foo: 1, bar: 2, baz: 2 };
// ^^^^^^^^ this fails!!!
}
}
}
}
// example use
generic_impl(MyStruct);
generic_impl(MyOtherStruct);
错误是:
error: expected expression, found `MyStruct`
将ty 更改为expr 意味着我不能使用impl $my_type。
除了传入 2x 参数之外,一个是 ty,另一个是 expr:
有没有办法根据宏的 ty 参数构造结构?
【问题讨论】: