在 Rust 中动态生成格式化程序的宏
Macro that generates formatters dynamically in Rust
我正在编写一个宏来为包含单个泛型类型的给定结构动态生成 Display
和 Debug
等格式化程序。代码如下:
macro_rules! create_formatters {
($type_name:ident < $gen_param:ident > , $t:path) => {
impl<$gen_param: $t> $t for $type_name<$gen_param> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let output = match stringify!($t) {
"std::fmt::Display" => format!("{}", self.0),
"std::fmt::Debug" => format!("{:?}", self.0),
// other formatters will be implemented soon
};
write!(f, "Content is: {}", output)
}
}
};
}
宏被create_formatters!(MyStruct<T>, std::fmt::Display);
或create_formatters!(MyStruct<T>, std::fmt::Debug);
调用
编译器报如下错误:
error[E0277]: the trait bound `T: std::fmt::Debug` is not satisfied
--> <anon>:8:58
|
8 | "std::fmt::Debug" => format!("{:?}", self.0),
| ^^^^^^ the trait `std::fmt::Debug` is not implemented for `T`
...
28 | create_formatters!(Swagger<T>, std::fmt::Display);
| -------------------------------------------------- in this macro invocation
|
= help: consider adding a `where T: std::fmt::Debug` bound
= note: required by `std::fmt::Debug::fmt`
我该如何解决?
为什么会出现这个错误?让我们看看create_formatters!(MyStruct<T>, std::fmt::Display);
的扩展:
impl<T: std::fmt::Display> std::fmt::Display for MyStruct<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let output = match "std::fmt::Display" {
"std::fmt::Display" => format!("{}", self.0),
"std::fmt::Debug" => format!("{:?}", self.0),
// other formatters will be implemented soon
};
write!(f, "Content is: {}", output)
}
}
这里,T
只能是 Display
,但是在 impl-body 内部的某处,您使用类型为 T
的 {:?}
格式化程序。是的,带有 {:?}
的匹配案例在运行时永远不会执行,但编译器在一般情况下无法知道。每个匹配臂的代码仍然需要生成!而这显然是不可能做到的。
如何解决?
可能最干净的解决方案是完全避免使用格式化程序字符串。如果你有一个 T
类型的变量实现了一个特征,你可以直接调用特征的方法:
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
我正在编写一个宏来为包含单个泛型类型的给定结构动态生成 Display
和 Debug
等格式化程序。代码如下:
macro_rules! create_formatters {
($type_name:ident < $gen_param:ident > , $t:path) => {
impl<$gen_param: $t> $t for $type_name<$gen_param> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let output = match stringify!($t) {
"std::fmt::Display" => format!("{}", self.0),
"std::fmt::Debug" => format!("{:?}", self.0),
// other formatters will be implemented soon
};
write!(f, "Content is: {}", output)
}
}
};
}
宏被create_formatters!(MyStruct<T>, std::fmt::Display);
或create_formatters!(MyStruct<T>, std::fmt::Debug);
编译器报如下错误:
error[E0277]: the trait bound `T: std::fmt::Debug` is not satisfied
--> <anon>:8:58
|
8 | "std::fmt::Debug" => format!("{:?}", self.0),
| ^^^^^^ the trait `std::fmt::Debug` is not implemented for `T`
...
28 | create_formatters!(Swagger<T>, std::fmt::Display);
| -------------------------------------------------- in this macro invocation
|
= help: consider adding a `where T: std::fmt::Debug` bound
= note: required by `std::fmt::Debug::fmt`
我该如何解决?
为什么会出现这个错误?让我们看看create_formatters!(MyStruct<T>, std::fmt::Display);
的扩展:
impl<T: std::fmt::Display> std::fmt::Display for MyStruct<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
let output = match "std::fmt::Display" {
"std::fmt::Display" => format!("{}", self.0),
"std::fmt::Debug" => format!("{:?}", self.0),
// other formatters will be implemented soon
};
write!(f, "Content is: {}", output)
}
}
这里,T
只能是 Display
,但是在 impl-body 内部的某处,您使用类型为 T
的 {:?}
格式化程序。是的,带有 {:?}
的匹配案例在运行时永远不会执行,但编译器在一般情况下无法知道。每个匹配臂的代码仍然需要生成!而这显然是不可能做到的。
如何解决?
可能最干净的解决方案是完全避免使用格式化程序字符串。如果你有一个 T
类型的变量实现了一个特征,你可以直接调用特征的方法:
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}