How to use proc-macro-error of Rust
This article mainly explains "how to use proc-macro-error in Rust". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use proc-macro-error in Rust".
Rust is a good choice for k8s for two reasons: 1. Rust provides some of the best support for WebAssembly compilation (described in more detail later). 1. To prove that the advantages of Rust can be applied to the Kubernetes ecosystem. The goal of proc-macro-errorproc-macro-error is to make error reporting in process macros easy and convenient. Quick overview of using examples:
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 {attrs .iter () .filter _ map (| attr | match process_attr (attr) {Ok (res) = > Some (res), Err (msg) = > {emit_error! (attr, "Invalid attribute: {}", msg) None}}) .pub fn make_answer ()} fn process_fields (_ attrs: & Fields)-> Vec {/ / 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 ()} Thank you for reading. This is the content of "how to use proc-macro-error in Rust". After the study of this article, I believe you have a deeper understanding of how to use proc-macro-error in Rust. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!