200行代码讲透 Rust Futures
Rust 是 k8s 的不错选择
proc-macro-error
use proc_macro_error::*;use proc_macro::TokenStream;use syn::{spanned::Spanned, DeriveInput, ItemStruct, Fields, Attribute , parse_macro_input};use quote::quote;
fn process_attrs(attrs: &[Attribute]) -> Vec<Attribute> { attrs .iter() .filter_map(|attr| match process_attr(attr) { Ok(res) => Some(res), Err(msg) => { emit_error!(attr, "Invalid attribute: {}", msg); None } }) .collect()}
fn process_fields(_attrs: &Fields) -> Vec<TokenStream> { // processing fields in pretty much the same way as attributes unimplemented!()}
#[proc_macro]#[proc_macro_error]pub fn make_answer(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemStruct); let attrs = process_attrs(&input.attrs);
// abort right now if some errors were encountered // at the attributes processing stage abort_if_dirty();
let fields = process_fields(&input.fields);
// no need to think about emitted errors // #[proc_macro_error] will handle them for you // // just return a TokenStream as you normally would quote!(/* stuff */).into()}
本文分享自微信公众号 - Rust语言中文社区(rust-china)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。php