24 releases
0.1.23 | Apr 30, 2024 |
---|---|
0.1.22 | Apr 30, 2024 |
#163 in Procedural macros
54 downloads per month
16KB
154 lines
PAY ATTENTION! THE CRATE IS UNFINISHED!
Rust macro-library for Don't Repeating Yourself
DRYlib is a library that designed for reducing the amount of duplicate code.
Crates.io: https://crates.io/crates/drylib
Take a look at clones macros example from examples/clones.rs:
extern crate drylib;
use drylib::{clones, mutclones}; // Use the best library in the world
fn main() {
// You can define variables that you want to clone:
let digit = 2;
let vector = vec![1, 2, 3];
let string = "this is a string generated by the `clones`".to_owned();
// And you can clone them with the `clones` macro:
clones!(digit, vector, string); // Just type what variables you want to clone
// The `clones` macro creates new variables using the following formula:
// format!({CLONES_PREFIX}{identifier(name) of the variable}).
// By default CLONES_PREFIX is 'c', but you can specify it with following features:
// [clones-prefix-c, clones-prefix-cl, and so on until clones-prefix-clone]
// Select the one and prefixes will be appropriate.
//
// Therefore, the `clones` macro expands as follows:
//
// let cdigit = digit.clone();
// let cvector = vector.clone();
// let cstring = string.clone();
// We can print them:
println!("cdigit: {cdigit}, cvector: {cvector:?}, cstring: {cstring}");
// This will print: cdigit: 2, cvector: [1, 2, 3], cstring: this is a string generated by the `clones`
// By the way, you can use the `clones` macro specifying
// mutability of the variables that you want to clone as in here:
clones!(mut digit, vector, mut string);
// this one ^^^^^ and this one ^^^^^^ will be created as mutable variables,
// with the formula already described up above.
//
// This macro call expands as follows:
//
// let mut cdigit = digit.clone();
// let cvector = vector.clone();
// let mut cstring = string.clone();
// The variables are mutable, so you can easily reassign them:
cdigit = 4;
cstring = "this is a mutable cloned string generated by the `clones` macro".to_owned();
// And print:
println!("cdigit: {cdigit}, cvector: {cvector:?}, cstring: {cstring}");
// This will print: cdigit: 4, cvector: [1, 2, 3], cstring: this is a mutable cloned string generated by the `clones` macro
// Here is another one, the `mutclones` macro, it does the same thing as the clones macro,
// but created variables are all mutable.
mutclones!(digit, vector, string); // The `mutclones` macro expands in this code:
//
// let mut cdigit = digit.clone();
// let mut cvector = vector.clone();
// let mut cstring = string.clone();
// The variables are mutable, therefore you can easily reassign them:
cdigit = 4;
cvector = vec![4, 5, 6];
cstring = "this is a mutable cloned string generated by the `mutclones` macro".to_owned();
// And print them:
println!("cdigit: {cdigit}, cvector: {cvector:?}, cstring: {cstring}");
// This will print: cdigit: 4, cvector: [4, 5, 6], cstring: this is a mutable cloned string generated by the `mutclones` macro
}
An then at the pubstruct macro example from examples/structs.rs:
extern crate drylib; // Import the drylib library
use drylib::*; // Bring all of the macros from the drylib into scope
fn main() {
// <++++++++++++ Let's start with creating tuple structs ++++++++++++>
// You can create a pub tuple struct using the pubstruct macro like that:
pubstruct!{
#[derive(Debug)]
Tuple(usize, i32, u32) // Define a tuple with types usize, i32, and u32
};
// This ^ will expand into this:
// pub struct Tuple(pub usize, pub i32, pub u32);
// So that's kinda the point of the macro, create pub struct with all of the fields
// in it are public as well.
let tuple = Tuple(0, 1, 2); // Let's create an instance of the Tuple
println!("{tuple:?}"); // Prints: Tuple(0, 1, 2)
// You can create a pub tuple struct with a generic type T as well:
pubstruct!{
#[derive(Debug)]
TupleT<T>(T, i32, u32)
};
let tuple_t = TupleT(0, 1, 2);
println!("{tuple_t:?}"); // Prints: TupleT(0, 1, 2)
// Also you create a struct with a lifetime:
pubstruct!{
#[derive(Debug)]
StructureLT<'a> {
greet: &'a str,
digit: i32,
}
}
let structure_lt = StructureLT { greet: "hello again", digit: 1 };
println!("{structure_lt:?}"); // Prints: StructureLT { greet: "hello again", digit: 1 }
// Create a struct with both of generics and lifetimes, that's within your power as well:
pubstruct!{
#[derive(Debug)]
StructureLTTU<'a, 'b, T, U> {
greet: &'a T,
array: &'b Vec<U>,
}
}
// And this ^ expands like this:
// pub struct StructureLTTU<'a, 'b, T, U> {
// pub greet: &'a T,
// pub array: &'b Vec<U>,
// }
let structure_lttu = StructureLTTU { greet: &"hello again", array: &vec![1, 2, 3] };
println!("{structure_lttu:?}"); // Prints: StructureLTTU { greet: "hello again", array: [1, 2, 3] }
// ... Continued in file examples/structs.rs
}