proc-macro-workshop:builder-6

审题

// Some fields may not always need to be specified. Typically these would be
// represented as Option<T> in the struct being built.
//
// Have your macro identify fields in the macro input whose type is Option and
// make the corresponding builder method optional for the caller. In the test
// case below, current_dir is optional and not passed to one of the builders in
// main.
//
// Be aware that the Rust compiler performs name resolution only after macro
// expansion has finished completely. That means during the evaluation of a
// procedural macro, "types" do not exist yet, only tokens. In general many
// different token representations may end up referring to the same type: for
// example `Option<T>` and `std::option::Option<T>` and `<Vec<Option<T>> as
// IntoIterator>::Item` are all different names for the same type. Conversely,
// a single token representation may end up referring to many different types in
// different places; for example the meaning of `Error` will depend on whether
// the surrounding scope has imported std::error::Error or std::io::Error. As a
// consequence, it isn't possible in general for a macro to compare two token
// representations and tell whether they refer to the same type.
//
// In the context of the current test case, all of this means that there isn't
// some compiler representation of Option that our macro can compare fields
// against to find out whether they refer to the eventual Option type after name
// resolution. Instead all we get to look at are the tokens of how the user has
// described the type in their code. By necessity, the macro will look for
// fields whose type is written literally as Option<...> and will not realize
// when the same type has been written in some different way.
//
// The syntax tree for types parsed from tokens is somewhat complicated because
// there is such a large variety of type syntax in Rust, so here is the nested
// data structure representation that your macro will want to identify:
//
//     Type::Path(
//         TypePath {
//             qself: None,
//             path: Path {
//                 segments: [
//                     PathSegment {
//                         ident: "Option",
//                         arguments: PathArguments::AngleBracketed(
//                             AngleBracketedGenericArguments {
//                                 args: [
//                                     GenericArgument::Type(
//                                         ...
//                                     ),
//                                 ],
//                             },
//                         ),
//                     },
//                 ],
//             },
//         },
//     )

use derive_builder::Builder;

#[derive(Builder)]
pub struct Command {
    executable: String,
    args: Vec<String>,
    env: Vec<String>,
    current_dir: Option<String>,
}

fn main() {
    let command = Command::builder()
        .executable("cargo".to_owned())
        .args(vec!["build".to_owned(), "--release".to_owned()])
        .env(vec![])
        .build()
        .unwrap();
    assert!(command.current_dir.is_none());

    let command = Command::builder()
        .executable("cargo".to_owned())
        .args(vec!["build".to_owned(), "--release".to_owned()])
        .env(vec![])
        .current_dir("..".to_owned())
        .build()
        .unwrap();
    assert!(command.current_dir.is_some());
}

这一关主要表明的就是所谓的缺省字段,这一下基本上把前面的实现都已经打乱了

  • XBuilder-fields:不能简单包裹,否则会产生Option<Option<T>>
  • setter:设置的是T,而不是Option<T>
  • build:字段检查可以为空

由此看来,我们基本上要把之前的方法全部实现一遍,并且要把Option<T>解析出来。
通过相关的提示,就是通过解析字段类型的Path,获取Option<T>中的T

方法

// common.rs
// 解析X::Y::ty<T>中的T
pub(crate) fn option_type_with_ident<'a>(
    ty: &'a syn::Type,
    ident: &str,
) -> std::option::Option<&'a syn::Type> {
    if let syn::Type::Path(
        syn::TypePath { 
            ref path,
            .. 
        }
    ) = ty {
        // 专门匹配路径的最后一个
        if let std::option::Option::Some(seg) = path.segments.last() {
            // 特征符号
            if seg.ident == ident {
                // <?> 中的?
                if let syn::PathArguments::AngleBracketed(
                    syn::AngleBracketedGenericArguments {
                        ref args,
                        ..
                    }
                ) = seg.arguments{
                    // 匹配提取T
                    if let Some(syn::GenericArgument::Type(inner_type)) = args.first() {
                        return std::option::Option::Some(inner_type);
                    }
                }
            }
        }
    }
    std::option::Option::None
}

// 提取指定的XX::YY::Option<T>中的T
pub(crate) fn option_type(ty: &syn::Type) -> std::option::Option<&syn::Type> {
    option_type_with_ident(ty, "Option".into())
}

这样,我们后续就好改写之前的方法了

题解

按照之前的分类,我们还是分步作答,不过基础方法都要进行重写

// solution6.rs
pub(super) fn solution(
    fields: &crate::common::FieldsType,
    builder_ident: &syn::Ident,
    origin_ident: &syn::Ident,
) -> proc_macro2::TokenStream {
    let mut token_stream = proc_macro2::TokenStream::new();

    // solution2
    let refactor_solution2_stream = solution2(fields, builder_ident, origin_ident);
    token_stream.extend(refactor_solution2_stream);
    // solution35
    let refactor_solution35_stream = solution35(fields, builder_ident, origin_ident);
    token_stream.extend(refactor_solution35_stream);
    // solution4
    let refactor_solution4_stream = solution4(fields, builder_ident, origin_ident);
    token_stream.extend(refactor_solution4_stream);
    return token_stream;
}

// 重写builder
fn solution2(
    fields: &crate::common::FieldsType,
    builder_ident: &syn::Ident,
    origin_ident: &syn::Ident,
) -> proc_macro2::TokenStream {
    let builder_fields_stream_vec: Vec<_> = fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            let ty = &f.ty;
            match crate::common::option_type(ty) {
                // 如果是包装类型Option<T>,直接使用T
                std::option::Option::Some(inner_type) => {
                    quote::quote! {
                        pub #ident: std::option::Option<#inner_type>
                    }
                },
                // 否则直接使用T
                std::option::Option::None => {
                    quote::quote! {
                        pub #ident: std::option::Option<#ty>
                    }
                }
            }
        })
        .collect();
    let idents: Vec<_> = fields.iter().map(|f| &f.ident).collect();
    quote::quote! {
        pub struct #builder_ident {
            #(
                #builder_fields_stream_vec
            ),*
        }

        impl #origin_ident {
            pub fn builder() -> #builder_ident {
                #builder_ident {
                    #(
                        #idents: std::option::Option::None
                    ),*
                }
            }
        }
    }
}

// 重写setter
fn solution35(
    fields: &crate::common::FieldsType,
    builder_ident: &syn::Ident,
    _origin_ident: &syn::Ident,
) -> proc_macro2::TokenStream {
    let setter_method_stream_vec: Vec<_> = fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            let ty = &f.ty;
            match crate::common::option_type(ty) {
                // 如果是Option<T>,使用T
                std::option::Option::Some(inner_type) => {
                    quote::quote! {
                        pub fn #ident(&mut self, #ident: #inner_type) -> &mut Self {
                            self.#ident = std::option::Option::Some(#ident);
                            self
                        }
                    }
                },
                // 直接使用T
                std::option::Option::None => {
                    quote::quote! {
                        pub fn #ident(&mut self, #ident: #ty) -> &mut Self {
                            self.#ident = std::option::Option::Some(#ident);
                            self
                        }
                    }
                }
            }
        })
        .collect();

    quote::quote! {
        impl #builder_ident {
            #(
                #setter_method_stream_vec
            )*
        }
    }
}

// 重写build:检查和回传
fn solution4(
    fields: &crate::common::FieldsType,
    builder_ident: &syn::Ident,
    origin_ident: &syn::Ident,
) -> proc_macro2::TokenStream {
    let construct_if_stream_vec: Vec<_> = fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            let ty = &f.ty;
            // 专门针对非Option<T>进行条件检查
            // 对于缺省字段无需检查
            if let std::option::Option::None = crate::common::option_type(ty) {
                return std::option::Option::Some(quote::quote! {
                    if self.#ident.is_none() {
                        let err = std::format!("field {} missing", std::stringify!(#ident));
                        return std::result::Result::Err(err.into());
                    }
                });
            }
            std::option::Option::None
        })
        .filter(|e| e.is_some())
        .collect();
    let construct_instance_stream_vec: Vec<_> = fields
        .iter()
        .map(|f| {
            let ident = &f.ident;
            let ty = &f.ty;
            // 如果是Option<T>,直接复制即可
            match crate::common::option_type(ty) {
                std::option::Option::Some(_) => {
                    quote::quote! {
                        #ident: self.#ident.clone()
                    }
                },
                // 如果是T,需要从builder中的Option<T>解出来进行设置
                std::option::Option::None => {
                    quote::quote! {
                        #ident: self.#ident.clone().unwrap()
                    }
                }
            }
        })
        .collect();
    quote::quote! {
        impl #builder_ident {
            pub fn build(&self) -> std::result::Result<#origin_ident, std::boxed::Box<dyn std::error::Error>> {
                #(
                    #construct_if_stream_vec
                )*
                let res = #origin_ident {
                    #(
                       #construct_instance_stream_vec
                    ),*
                };
                std::result::Result::Ok(res)
            }
        }
    }
}

其中需要注意的是#()*,后面的,if中是不需要的。

整体

// lib.rs
mod solution2;
mod solution35;
mod solution4;
mod common;
fn solution1(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ast = syn::parse_macro_input!(input as syn::DeriveInput);
    let fields = {
        match common::parse_fields(&ast) {
            Ok(f) => f,
            Err(_e) => std::panic!(std::stringify!(_e)),
        }
    };

    let origin_ident = &ast.ident;
    let builder_ident = &quote::format_ident!("{}Builder", origin_ident);
    let mut token_stream = proc_macro2::TokenStream::new();

    // solution2
    let solution2_stream = solution2::solution(fields, origin_ident, builder_ident);
    token_stream.extend(solution2_stream);

    // solution35
    let solution35_stream = solution35::soultion(fields, builder_ident);
    token_stream.extend(solution35_stream);

    // solution4
    let solution4_stream = solution4::solution(fields, builder_ident, origin_ident);
    token_stream.extend(solution4_stream);

    // solution6
    let token_stream = solution6::solution(fields, builder_ident, origin_ident);

    proc_macro::TokenStream::from(token_stream)
}

虽然前面的

  • solution2
  • solution35
  • solution4

都已经被重新实现了,完全不需要添加,但是为了清晰的还原解题步骤,还是原样保留。

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!