2个版本 (1个稳定版)
1.0.0 | 2024年5月26日 |
---|---|
0.1.0 | 2024年4月1日 |
#323 in 过程宏
每月55次下载
24KB
390 行
smile-marco提供了getter、setter、wither、builder等宏
这些宏为结构体中的属性生成get、set、with、builder等方法,提供快速访问和操作。方便日常开发。
[dependencies]
smile-marco = { version = "1.0.0", features = ["full"] }
#or
smile-marco = { version = "1.0.0"}
如需,使用部分宏。
[dependencies]
smile-marco = { version = "1.0.0", features = ["builder","getter","setter","wither"] }
示例
use smile_marco::{Builder, Getter, Setter, Wither};
#[derive(Builder, Getter, Setter, Wither)]
struct Book {
title: String,
#[name(cost)] // rename
price: i32,
#[exclude] // Do not generate set methods for author
author: String,
}
fn main() {
let mut book = Book::builder()
.title("Rust Programming".to_string())
.price(100)
.author("rust".to_string())
.build();
assert_eq!(book.title, "Rust Programming".to_string());
book.set_title("Rust Book".into());
assert_eq!(book.title, "Rust Book".to_string());
assert_eq!(book.get_cost(), &100);
let mut book = book.with_cost(|c| c + 100);
assert_eq!(book.price, 200);
}
如需更多使用示例,请参阅示例。
依赖关系
~275–720KB
~17K SLoC