Crates.io | smile-marco |
lib.rs | smile-marco |
version | 1.0.0 |
source | src |
created_at | 2024-04-01 03:21:46.913716 |
updated_at | 2024-05-26 04:12:23.878672 |
description | One contains many useful macros for generating common code |
homepage | |
repository | https://github.com/shunlingsmile/smile-marco |
max_upload_size | |
id | 1192096 |
size | 39,236 |
These macros generate methods such as get, set, with, and builder for properties within a struct, providing quick access and manipulation. Convenient for daily development.
[dependencies]
smile-marco = { version = "1.0.0", features = ["full"] }
#or
smile-marco = { version = "1.0.0"}
If needed, use partial macros.
[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);
}
If you need more usage examples, please refer to examples。