struct - 如何以编程方式获取结构的字段数?

标签 struct rust rust-macros rust-proc-macros

我有一个如下所示的自定义结构:

struct MyStruct {
    first_field: i32,
    second_field: String,
    third_field: u16,
}

是否有可能以编程方式获取结构字段的数量(例如,通过方法调用 field_count()):

let my_struct = MyStruct::new(10, "second_field", 4);
let field_count = my_struct.field_count(); // Expecting to get 3

对于这个结构:

struct MyStruct2 {
    first_field: i32,
}

...以下调用应返回 1:

let my_struct_2 = MyStruct2::new(7);
let field_count = my_struct2.field_count(); // Expecting to get count 1

是否有类似 field_count() 的 API,或者是否只能通过宏获取?

如果这可以通过宏实现,应该如何实现?

最佳答案

Are there any possible API like field_count() or is it only possible to get that via macros?

没有这样的内置 API 可以让您在运行时获取此信息。 Rust 没有运行时反射(更多信息参见 this question)。但这确实可以通过 proc-macros 实现!

注意:proc-macros 不同于“示例宏”(通过 macro_rules! 声明)。后者不如 proc-macros 强大。

If this is achievable with macros, how should it be implemented?

(这不是对 proc-macros 的介绍;如果这个主题对您来说是全新的,请先阅读其他地方的介绍。)

在 proc-macro(例如自定义派生)中,您可能需要以某种方式获取作为 TokenStream 的结构定义。使用带有 Rust 语法的 TokenStream 的实际解决方案是通过 syn 解析它。 :

#[proc_macro_derive(FieldCount)]
pub fn derive_field_count(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);

    // ...
}

输入的类型是ItemStruct .如您所见,它具有 Fields 类型的字段 fields .在该字段上,您可以调用 iter() 来获取结构所有字段的迭代器,然后您可以在该字段上调用 ​​count():

let field_count = input.fields.iter().count();

现在你有了你想要的。

也许您想将此 field_count() 方法添加到您的类型中。您可以通过自定义派生来做到这一点(通过在此处使用 quote crate):

let name = &input.ident;

let output = quote! {
    impl #name {
        pub fn field_count() -> usize {
            #field_count
        }
    }
};

// Return output tokenstream
TokenStream::from(output)

然后,在你的应用程序中,你可以这样写:

#[derive(FieldCount)]
struct MyStruct {
    first_field: i32,
    second_field: String,
    third_field: u16,
}

MyStruct::field_count(); // returns 3

关于struct - 如何以编程方式获取结构的字段数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54177438/

相关文章:

methods - 在函数内部定义结构的方法

rust - 如何通过宏将字符串转换为原始字符串?

rust - 写!引用时宏不会在单独的方法中编译

rust - 无法使此宏规则编译 : type annotation needed

rust - 如何将可见性 token 传递给位标志!宏?

c - 使用 struct 从文本文件读取时出错

c - struct 有一个函数指针,指向一个函数,该函数接受所述结构的指针..需要预定义吗?

c - 在不知道元素数量的情况下迭代准备好的结构体数组 c 的正确简单声明

logging - 如何在Rust中将自定义数据传递到日志箱的宏?

rust - 为什么将移动值的成员分配给时编译不会失败?