当前位置:网站首页>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_inputanalysisDeriveInput - 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());
}边栏推荐
- 将mysql的数据库导出xxx.sql,将xxx.sql文件导入到服务器的mysql中。项目部署。
- ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
- 【无标题】
- Take my brother to do the project. It's cold
- Chart list Performance Optimization: minimum resource consumption in the visualization area
- Swift foundation features unique to swift
- List of Li Bai's 20 most classic poems
- Redis的Cluster集群数据倾斜
- 2022 tea artist (intermediate) work license question bank and online simulation examination
- [xinliu-s6 new model +sa 3-star Xinghai] the new two-way server of the third generation chip was launched and the product was updated~
猜你喜欢

io模型初探

WCF TCP protocol transmission

根据网络上的视频的m3u8文件通过ffmpeg进行合成视频

Opencv实现图像的基本变换

ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler

longhorn安装与使用

Synthesize video through ffmpeg according to m3u8 file of video on the network

OpenCV to realize the basic transformation of image
![[untitled]](/img/94/792e8363dbfe67770e93b0dcdc8e72.png)
[untitled]

2021-03-09 comp9021 class 7 Notes
随机推荐
05 Ubuntu installing mysql8
【微服务~Nacos】Nacos服务提供者和服务消费者
Small sample fault diagnosis - attention mechanism code - Implementation of bigru code parsing
Opencv实现图像的基本变换
JUC个人简单笔记
For a detailed explanation of flex:1, flex:1
普通token
貸款五級分類
(pkcs1) RSA public private key PEM file parsing
How to replace the web player easyplayerproactivex Key in OCX?
独立站运营中如何提升客户留存率?客户细分很重要!
2021-03-11 COMP9021第八节课笔记
Scénarios d'utilisation de la promesse
WCF TCP protocol transmission
How to configure networkpolicy for nodeport in kubernetes
(PKCS1) RSA 公私钥 pem 文件解析
Tencent cloud ASR product PHP realizes real-time voice authentication request
Longhorn installation and use
软件过程与项目管理期末复习与重点
Cloudbase database migration scheme