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