#getter-setter #builder #macro #get #derive-builder

过程宏 smile-marco

包含许多用于生成常用代码的有用宏

2个版本 (1个稳定版)

1.0.0 2024年5月26日
0.1.0 2024年4月1日

#323 in 过程宏

Download history 3/week @ 2024-04-10 153/week @ 2024-05-22 19/week @ 2024-05-29 3/week @ 2024-06-05 2/week @ 2024-06-26 52/week @ 2024-07-03 1/week @ 2024-07-24

每月55次下载

MIT/Apache

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