Rust macros for operator overloading of generic types.
The macros need four statements
Note All statements end with a semicolon except the where clause.
```rust
struct Pair
fn sub_pair
genops!(
let a = Pair(2, 3); let b = Pair(1, 8);
println!("a + b = {:?}", a + b); //a + b = Pair(3, 11) println!("a - b = {:?}", a - b); //a - b = Pair(1, -5) ```
The primary macro for all operators.
```rust
struct Pair
gen_ops!(
let a = Pair(10, 5); let b = Pair(8, 9);
println!("a + b = {:?}", a + b); // a + b = Pair(18, 14) println!("a - b = {:?}", a - b); // a - b = Pair(2, -4) ```
Implements commutative operations.
```rust
struct Pair
genopscomm!(
println!("a * 5 = {:?}", a * 5); //a * 5 = Pair(60, 15) println!("5 * a = {:?}", 5 * a); //5 * a = Pair(60, 15) println!("a & 2 = {:?}", a & 2); //a & 2 = Pair(0, 2) println!("2 & a = {:?}", 2 & a); //2 & a = Pair(0, 2) ```
Implements trait for borrowed types.
```rust
struct Pair
genopsex!(
let mut a = Pair(12, 3); { let mut b = &mut a; println!("&mut a * 2 = {:?}", b * 2);// &mut a * 2 = Pair(24, 6) } println!("&a * 2 = {:?}", &a * 2);// &a * 2 = Pair(24, 6) println!("a * 2 = {:?}", a * 2);// a * 2 = Pair(24, 6) ```
Implements commutative operations for borrowed types.
```rust
struct Pair
genopscomm_ex!(
let a = Pair(12, 3); println!("a * 5 = {:?}", a * 5); //a * 5 = Pair(60, 15) println!("5 * a = {:?}", 5 * a); //5 * a = Pair(60, 15) println!("5 * &a = {:?}", 5 * &a); //5 * &a = Pair(60, 15) println!("&a * 5 = {:?}", &a * 5); //&a * 5 = Pair(60, 15) ```
For more details see docs.
To do: - [ ] Where clause for each operator - [x] Const generic parameters - [ ] Lifetime parameters - [x] Better testing - [ ] Adding github wiki
Warning The vesions 0.1.x might not be compatible with each other
This project is inspired by auto_ops