2 unstable releases

0.3.0 Feb 9, 2021
0.1.0 Dec 10, 2019

#397 in Rust patterns

Download history 131/week @ 2021-08-10 51/week @ 2021-08-17 68/week @ 2021-08-24 33/week @ 2021-08-31 91/week @ 2021-09-07 70/week @ 2021-09-14 70/week @ 2021-09-21 93/week @ 2021-09-28 128/week @ 2021-10-05 150/week @ 2021-10-12 164/week @ 2021-10-19 168/week @ 2021-10-26 240/week @ 2021-11-02 99/week @ 2021-11-09 112/week @ 2021-11-16 65/week @ 2021-11-23

575 downloads per month
Used in 5 crates (4 directly)

MIT license

29KB
275 lines

auto_ops Build Status Latest Version

Macros for easy operator overloading.

Documentation

This library is forked from the original impl_ops by brianwp3000.

This library makes writing multiple impl std::ops::<op> blocks much faster, especially when you want operators defined for both owned and borrowed variants of the inputs.

To use, import the macros with use auto_ops::*;. Remember that you can only overload operators between one or more types defined in the current crate (respecting Rust orphan rules).

Examples

use auto_ops::*;

#[derive(Clone, Debug, PartialEq)]
struct DonkeyKong {
    pub bananas: i32,
}
impl DonkeyKong {
    pub fn new(bananas: i32) -> DonkeyKong {
        DonkeyKong { bananas: bananas }
    }
}

impl_op_ex!(+ |a: &DonkeyKong, b: &DonkeyKong| -> DonkeyKong { DonkeyKong::new(a.bananas + b.bananas) });
impl_op_ex!(+= |a: &mut DonkeyKong, b: &DonkeyKong| { a.bananas += b.bananas });

fn main() {
    assert_eq!(DonkeyKong::new(5), DonkeyKong::new(4) + DonkeyKong::new(1));
    assert_eq!(DonkeyKong::new(5), DonkeyKong::new(4) + &DonkeyKong::new(1));
    assert_eq!(DonkeyKong::new(5), &DonkeyKong::new(4) + DonkeyKong::new(1));
    assert_eq!(DonkeyKong::new(5), &DonkeyKong::new(4) + &DonkeyKong::new(1));

    let mut dk = DonkeyKong::new(4);
    dk += DonkeyKong::new(1);
    dk += &DonkeyKong::new(1);
    assert_eq!(DonkeyKong::new(6), dk);
}

Roadmap

With Rust lifetime inference changes, implementations for generic (over types and lifetimes) impls are being worked on.

No runtime deps