auto_ops_det

Crates.ioauto_ops_det
lib.rsauto_ops_det
version2.0.0
created_at2025-09-22 07:38:01.666819+00
updated_at2025-09-23 05:38:29.453016+00
descriptionMacros for easy operator overloading.
homepage
repository
max_upload_size
id1849626
size65,909
Mikhail Petrov (graviton-rs)

documentation

https://docs.rs/auto_ops_det

README

About The Project

This project is a fork of auto_ops.

Like the upstream, auto_ops_det provides macros for easy operator overloading, with more features.

Usage

Same as original.

License

The original auto_ops is licensed under


Original readme of auto_ops

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.

Commit count: 0

cargo fmt