2 unstable releases

0.3.0 Feb 9, 2021
0.1.0 Dec 10, 2019

#346 in Rust patterns

Download history 12141/week @ 2024-07-19 12494/week @ 2024-07-26 10541/week @ 2024-08-02 11406/week @ 2024-08-09 12181/week @ 2024-08-16 10931/week @ 2024-08-23 11578/week @ 2024-08-30 13604/week @ 2024-09-06 13127/week @ 2024-09-13 13049/week @ 2024-09-20 12819/week @ 2024-09-27 13847/week @ 2024-10-04 13894/week @ 2024-10-11 16411/week @ 2024-10-18 15192/week @ 2024-10-25 11900/week @ 2024-11-01

60,713 downloads per month
Used in 37 crates (19 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