2022-11-14:rust语言,请使用过程宏给结构体AAA生成结构体AAABuilder和创建AAABuilder

2022-11-14:rust语言,请使用过程宏给结构体AAA生成结构体AAABuilder和创建AAABuilder实例的方法。
宏使用如下:

#[derive(Builder)]
pub struct AAA {
    a: String,
    b: i32,   
    c: f64,
    d: Vec<bool>,
}

宏展开后变成如下代码:

pub struct AAA {
    a: String,
    b: i32,   
    c: f64,
    d: Vec<bool>,
}
pub struct AAABuilder {
    a: std::option::Option<String>,
    b: std::option::Option<i32>,
    c: std::option::Option<f64>,
    d: std::option::Option<Vec<bool>>,
}
impl AAA {
    pub fn builder() -> AAABuilder {
        AAABuilder {
            a: std::option::Option::None,
            b: std::option::Option::None,
            c: std::option::Option::None,
            d: std::option::Option::None,
        }
    }
}

答案2022-11-14:

这题没啥技巧,自然智慧即可。

代码用rust编写。代码如下:

// builder/src/lib.rs
use proc_macro::TokenStream;
use quote;
use syn::spanned::Spanned;
#[proc_macro_derive(Builder)]
pub fn derive(input: TokenStream) -> TokenStream {
    let st = syn::parse_macro_input!(input as syn::DeriveInput);
    match expand_code(&st) {
        Ok(token_stream) => token_stream.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

fn get_fields(
    st: &syn::DeriveInput,
) -> syn::Result<&syn::punctuated::Punctuated<syn::Field, syn::Token![,]>> {
    if let syn::Data::Struct(syn::DataStruct {
        fields: syn::Fields::Named(syn::FieldsNamed { ref named, .. }),
        ..
    }) = st.data
    {
        return Ok(named);
    };
    Err(syn::Error::new_spanned(st, "必须定义在结构体上"))
}

fn gen_fields(st: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    let fields = get_fields(st)?;
    let idents: Vec<_> = fields.iter().map(|f| &f.ident).collect();
    let types: Vec<_> = fields.iter().map(|f| &f.ty).collect();
    let ret = quote::quote!(
        #( #idents: std::option::Option<#types>),*
    );
    return Ok(ret);
}

fn gen_init_clauses(st: &syn::DeriveInput) -> syn::Result<Vec<proc_macro2::TokenStream>> {
    let fields = get_fields(st)?;
    let init_cluase: Vec<_> = fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            quote::quote!(
                #ident: std::option::Option::None
            )
        })
        .collect();
    Ok(init_cluase)
}

fn expand_code(st: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    let struct_name_literal = st.ident.to_string();
    let builder_name_literal = format!("{}Builder", struct_name_literal);
    let builder_name_ident = syn::Ident::new(&builder_name_literal, st.span());
    let struct_ident = &st.ident;
    let builder_struct_field_def = gen_fields(st)?;
    let builder_struct_factory_init_clauses = gen_init_clauses(st)?;
    let ret = quote::quote!(
        pub struct #builder_name_ident{
            #builder_struct_field_def
        }

        impl #struct_ident {
            pub fn builder() -> #builder_name_ident {
                #builder_name_ident {
                    #(#builder_struct_factory_init_clauses),*
                }
            }
        }
    );
    Ok(ret)
}
# builder.Cargo.toml
[package]
name = "derive_builder"
version = "0.0.0"
autotests = false
edition = "2021"
publish = false

[lib]
proc-macro = true

[[test]]
name = "tests"
path = "tests/progress.rs"

[dev-dependencies]
trybuild = { version = "1.0.49", features = ["diff"] }

[dependencies]
syn = {version="1.0",features=["extra-traits"]}
proc-macro2 = {version="1.0"}
quote = {version="1.0"}
use derive_builder::Builder;

#[derive(Builder)]
pub struct AAA {
    a: String,
    b: i32,
    c: f64,
    d: Vec<bool>,
}

fn main() {
    let builder = AAA::builder();

    let _ = builder;
}
# Cargo.toml
[package]
name = "proc-macro-workshop"
version = "0.0.0"
edition = "2021"
publish = false

[workspace]

[[bin]]
name = "workshop"
path = "main.rs"

[dependencies]
bitfield = { path = "bitfield" }
derive_builder = { path = "builder" }
derive_debug = { path = "debug" }
seq = { path = "seq" }
sorted = { path = "sorted" }

敲cargo expand命令后,打印如下:
在这里插入图片描述

本作品采用《CC 协议》,转载必须注明作者和本文链接
微信公众号:福大大架构师每日一题。最新面试题,涉及golang,rust,mysql,redis,云原生,算法,分布式,网络,操作系统。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
169
粉丝
10
喜欢
16
收藏
5
排名:985
访问:4972
私信
所有博文
社区赞助商