有没有办法在宏中获取结构的字段名称?
Is there a way to get the field names of a struct in a macro?
考虑以下示例:
struct S {
a: String,
b: String,
}
我有一个这样调用的宏:
my_macro!(S);
我想像这样访问宏中结构的字段名称:
macro_rules! my_macro {
($t:ty) => {{
let field_names = get_field_names($t);
// do something with field_names
}};
}
我是 Rust 和宏的新手,所以我可能遗漏了一些明显的东西。
一个宏在解析过程中或多或少被扩展;它无法访问 AST 或类似的东西——它只能访问你传递给它的东西,对于 my_macro!(S)
来说纯粹是应该有一个名为 S
.[= 的类型13=]
如果您将结构定义为宏的一部分,那么您可以了解字段:
macro_rules! my_macro {
(struct $name:ident {
$($field_name:ident: $field_type:ty,)*
}) => {
struct $name {
$($field_name: $field_type,)*
}
impl $name {
// This is purely an example—not a good one.
fn get_field_names() -> Vec<&'static str> {
vec![$(stringify!($field_name)),*]
}
}
}
}
my_macro! {
struct S {
a: String,
b: String,
}
}
// S::get_field_names() == vec!["a", "b"]
…但这虽然可能有用,但通常是一件可疑的事情。
这里还有一种不需要写宏的可能性(不过字段名会在运行时解析):
extern crate rustc_serialize;
use rustc_serialize::json::{Encoder, Json};
use rustc_serialize::json::Json::Object;
use rustc_serialize::Encodable;
#[derive(Default, RustcEncodable)]
struct S {
a: String,
b: String,
}
fn main() {
let mut json = "".to_owned();
{
let mut encoder = Encoder::new(&mut json);
S::default().encode(&mut encoder).unwrap();
}
let json = Json::from_str(&json).unwrap();
if let Object(object) = json {
let field_names: Vec<_> = object.keys().collect();
println!("{:?}", field_names);
}
}
(此解决方案需要 rustc-serialize
crate)
已添加 derive(Default)
以避免必须根据需要手动创建结构(但仍会创建结构)。
此解决方案的工作原理是将结构编码为 JSON 格式的 String
并将其解码为 Json
。从 Json
对象中,我们可以提取字段名称(如果它是 Object
变体)。
一种可能更有效的方法是编写自己的编码器:
struct FieldNames {
names: Vec<String>,
}
impl FieldNames {
fn new() -> FieldNames {
FieldNames {
names: vec![],
}
}
}
struct FieldsEncoder<'a> {
fields: &'a mut FieldNames,
}
impl<'a> FieldsEncoder<'a> {
fn new(fields: &mut FieldNames) -> FieldsEncoder {
FieldsEncoder {
fields: fields,
}
}
}
type EncoderError = ();
impl<'a> Encoder for FieldsEncoder<'a> {
fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> {
f(self)
}
fn emit_struct_field<F>(&mut self, f_name: &str, _f_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> {
self.fields.names.push(f_name.to_owned());
Ok(())
}
type Error = EncoderError;
fn emit_nil(&mut self) -> Result<(), Self::Error> { Err(()) }
fn emit_usize(&mut self, _v: usize) -> Result<(), Self::Error> { Err(()) }
fn emit_u64(&mut self, _v: u64) -> Result<(), Self::Error> { Err(()) }
fn emit_u32(&mut self, _v: u32) -> Result<(), Self::Error> { Err(()) }
fn emit_u16(&mut self, _v: u16) -> Result<(), Self::Error> { Err(()) }
fn emit_u8(&mut self, _v: u8) -> Result<(), Self::Error> { Err(()) }
fn emit_isize(&mut self, _v: isize) -> Result<(), Self::Error> { Err(()) }
fn emit_i64(&mut self, _v: i64) -> Result<(), Self::Error> { Err(()) }
fn emit_i32(&mut self, _v: i32) -> Result<(), Self::Error> { Err(()) }
fn emit_i16(&mut self, _v: i16) -> Result<(), Self::Error> { Err(()) }
fn emit_i8(&mut self, _v: i8) -> Result<(), Self::Error> { Err(()) }
fn emit_bool(&mut self, _v: bool) -> Result<(), Self::Error> { Err(()) }
fn emit_f64(&mut self, _v: f64) -> Result<(), Self::Error> { Err(()) }
fn emit_f32(&mut self, _v: f32) -> Result<(), Self::Error> { Err(()) }
fn emit_char(&mut self, _v: char) -> Result<(), Self::Error> { Err(()) }
fn emit_str(&mut self, _v: &str) -> Result<(), Self::Error> { Err(()) }
fn emit_enum<F>(&mut self, _name: &str, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_variant<F>(&mut self, _v_name: &str, _v_id: usize, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_struct_variant<F>(&mut self, _v_name: &str, _v_id: usize, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_struct_variant_field<F>(&mut self, _f_name: &str, _f_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple<F>(&mut self, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple_arg<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple_struct<F>(&mut self, _name: &str, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple_struct_arg<F>(&mut self, _f_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_option<F>(&mut self, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_option_none(&mut self) -> Result<(), Self::Error> { Err(()) }
fn emit_option_some<F>(&mut self, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_seq<F>(&mut self, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_seq_elt<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_map<F>(&mut self, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_map_elt_key<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_map_elt_val<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
}
可以这样使用:
fn main() {
let mut fields = FieldNames::new();
{
let mut encoder = FieldsEncoder::new(&mut fields);
S::default().encode(&mut encoder).unwrap();
}
println!("{:?}", fields.names);
}
考虑以下示例:
struct S {
a: String,
b: String,
}
我有一个这样调用的宏:
my_macro!(S);
我想像这样访问宏中结构的字段名称:
macro_rules! my_macro {
($t:ty) => {{
let field_names = get_field_names($t);
// do something with field_names
}};
}
我是 Rust 和宏的新手,所以我可能遗漏了一些明显的东西。
一个宏在解析过程中或多或少被扩展;它无法访问 AST 或类似的东西——它只能访问你传递给它的东西,对于 my_macro!(S)
来说纯粹是应该有一个名为 S
.[= 的类型13=]
如果您将结构定义为宏的一部分,那么您可以了解字段:
macro_rules! my_macro {
(struct $name:ident {
$($field_name:ident: $field_type:ty,)*
}) => {
struct $name {
$($field_name: $field_type,)*
}
impl $name {
// This is purely an example—not a good one.
fn get_field_names() -> Vec<&'static str> {
vec![$(stringify!($field_name)),*]
}
}
}
}
my_macro! {
struct S {
a: String,
b: String,
}
}
// S::get_field_names() == vec!["a", "b"]
…但这虽然可能有用,但通常是一件可疑的事情。
这里还有一种不需要写宏的可能性(不过字段名会在运行时解析):
extern crate rustc_serialize;
use rustc_serialize::json::{Encoder, Json};
use rustc_serialize::json::Json::Object;
use rustc_serialize::Encodable;
#[derive(Default, RustcEncodable)]
struct S {
a: String,
b: String,
}
fn main() {
let mut json = "".to_owned();
{
let mut encoder = Encoder::new(&mut json);
S::default().encode(&mut encoder).unwrap();
}
let json = Json::from_str(&json).unwrap();
if let Object(object) = json {
let field_names: Vec<_> = object.keys().collect();
println!("{:?}", field_names);
}
}
(此解决方案需要 rustc-serialize
crate)
已添加 derive(Default)
以避免必须根据需要手动创建结构(但仍会创建结构)。
此解决方案的工作原理是将结构编码为 JSON 格式的 String
并将其解码为 Json
。从 Json
对象中,我们可以提取字段名称(如果它是 Object
变体)。
一种可能更有效的方法是编写自己的编码器:
struct FieldNames {
names: Vec<String>,
}
impl FieldNames {
fn new() -> FieldNames {
FieldNames {
names: vec![],
}
}
}
struct FieldsEncoder<'a> {
fields: &'a mut FieldNames,
}
impl<'a> FieldsEncoder<'a> {
fn new(fields: &mut FieldNames) -> FieldsEncoder {
FieldsEncoder {
fields: fields,
}
}
}
type EncoderError = ();
impl<'a> Encoder for FieldsEncoder<'a> {
fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> {
f(self)
}
fn emit_struct_field<F>(&mut self, f_name: &str, _f_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> {
self.fields.names.push(f_name.to_owned());
Ok(())
}
type Error = EncoderError;
fn emit_nil(&mut self) -> Result<(), Self::Error> { Err(()) }
fn emit_usize(&mut self, _v: usize) -> Result<(), Self::Error> { Err(()) }
fn emit_u64(&mut self, _v: u64) -> Result<(), Self::Error> { Err(()) }
fn emit_u32(&mut self, _v: u32) -> Result<(), Self::Error> { Err(()) }
fn emit_u16(&mut self, _v: u16) -> Result<(), Self::Error> { Err(()) }
fn emit_u8(&mut self, _v: u8) -> Result<(), Self::Error> { Err(()) }
fn emit_isize(&mut self, _v: isize) -> Result<(), Self::Error> { Err(()) }
fn emit_i64(&mut self, _v: i64) -> Result<(), Self::Error> { Err(()) }
fn emit_i32(&mut self, _v: i32) -> Result<(), Self::Error> { Err(()) }
fn emit_i16(&mut self, _v: i16) -> Result<(), Self::Error> { Err(()) }
fn emit_i8(&mut self, _v: i8) -> Result<(), Self::Error> { Err(()) }
fn emit_bool(&mut self, _v: bool) -> Result<(), Self::Error> { Err(()) }
fn emit_f64(&mut self, _v: f64) -> Result<(), Self::Error> { Err(()) }
fn emit_f32(&mut self, _v: f32) -> Result<(), Self::Error> { Err(()) }
fn emit_char(&mut self, _v: char) -> Result<(), Self::Error> { Err(()) }
fn emit_str(&mut self, _v: &str) -> Result<(), Self::Error> { Err(()) }
fn emit_enum<F>(&mut self, _name: &str, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_variant<F>(&mut self, _v_name: &str, _v_id: usize, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_struct_variant<F>(&mut self, _v_name: &str, _v_id: usize, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_enum_struct_variant_field<F>(&mut self, _f_name: &str, _f_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple<F>(&mut self, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple_arg<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple_struct<F>(&mut self, _name: &str, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_tuple_struct_arg<F>(&mut self, _f_idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_option<F>(&mut self, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_option_none(&mut self) -> Result<(), Self::Error> { Err(()) }
fn emit_option_some<F>(&mut self, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_seq<F>(&mut self, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_seq_elt<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_map<F>(&mut self, _len: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_map_elt_key<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
fn emit_map_elt_val<F>(&mut self, _idx: usize, _f: F) -> Result<(), Self::Error> where F: FnOnce(&mut Self) -> Result<(), Self::Error> { Err(()) }
}
可以这样使用:
fn main() {
let mut fields = FieldNames::new();
{
let mut encoder = FieldsEncoder::new(&mut fields);
S::default().encode(&mut encoder).unwrap();
}
println!("{:?}", fields.names);
}