当前位置:网站首页>Rust procedure macro simply imitates Lombok function
Rust procedure macro simply imitates Lombok function
2022-06-24 08:34:00 【8 pineapple】
Concept
Use JDK8 When developing applications , Often used Lombok Some annotation functions in , For example, use @Data
To generate class members Get/Set Method .
Try using today Rust To implement similar functions .
practice
technological process :
- Use Syn In the library
parse_macro_input
analysisDeriveInput
- Traverse the target object Fields Domain
- Use
format_ident!
Splice generation function name - Use quote In the library
quote!
Generate TokenStream paragraph - Generate the target impl Method
A little more complicated quote Subsegment , Attach to master lombok_data_fun
The process of .
Let's take an example :
// lib.rs #[proc_macro_derive(Data)] pub fn lombok_data(input: TokenStream)->TokenStream { let input = parse_macro_input!(input as DeriveInput); let data = input.data; let mut lombok_data_fun= quote! { }; match data { Data::Struct(s)=>{ // Traversal members for f in s.fields{ let attr_name = f.ident.to_token_stream(); let attr_ty = f.ty.to_token_stream(); // Splice function name let get_name = format_ident!("get_{}", attr_name.to_string()); let set_name = format_ident!("set_{}", attr_name.to_string()); let t = quote! { pub fn #get_name(&self)->&#attr_ty{ &self.#attr_name } pub fn #set_name(&mut self, val:#attr_ty){ self.#attr_name = val } }; // Sub segments are spliced into main segments lombok_data_fun = quote! { #lombok_data_fun #t } } }, _=>() } // Get structure name let name = input.ident.to_token_stream(); let expanded = quote! { impl #name { #lombok_data_fun } }; expanded.into() } // main.rs #[derive(Data)] struct LombokTest { name: String, age : i32, } fn main() { let mut test = LombokTest { name: "Baby".to_string(), age: 10, }; test.set_name(" Zhang San ".to_string()); println!("{}", test.get_name()); }
边栏推荐
- Common misconceptions in Tencent conference API - signature error_ code 200003
- Swift extension chainlayout (UI chain layout) (source code)
- ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
- ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis
- Swift foundation features unique to swift
- IIS build wordpress5.7 manually
- PAT 1157:校庆
- Industrial computer anti cracking
- C language_ Love and hate between string and pointer
- 2021-03-16 COMP9021第九节课笔记
猜你喜欢
2021-03-09 comp9021 class 7 Notes
5 minutes, excellent customer service chat handling skills
ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
Introduction to RCNN, fast RCNN and fast RCNN
小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
2021-03-04 comp9021 class 6 notes
独立站运营中如何提升客户留存率?客户细分很重要!
【关于运维和网工的差别,一文说透】
Question 3 - MessageBox pop-up box, modify the default background color
2021-03-16 COMP9021第九节课笔记
随机推荐
11-- longest substring without repeated characters
貸款五級分類
2022年流动式起重机司机特种作业证考试题库及在线模拟考试
IIS build wordpress5.7 manually
C language_ Love and hate between string and pointer
5分钟,客服聊天处理技巧,炉火纯青
Redis的Cluster集群数据倾斜
QT writing security video monitoring system 36 onvif continuous movement
Permission model DAC ACL RBAC ABAC
List of Li Bai's 20 most classic poems
软件过程与项目管理期末复习与重点
ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言
Nodejs redlock notes
Promise的使用场景
487. number of maximum consecutive 1 II ●●
2022 tea artist (intermediate) work license question bank and online simulation examination
OpenCV to realize the basic transformation of image
Opencv实现图像的基本变换
Common date formatter and QT method for obtaining current time
DHCP, TFTP Foundation