2 个版本

0.1.1 2019 年 8 月 10 日
0.1.0 2019 年 8 月 6 日

#175 in Rust 模式

Download history 976654/week @ 2024-03-14 982364/week @ 2024-03-21 958377/week @ 2024-03-28 988032/week @ 2024-04-04 1016112/week @ 2024-04-11 1028093/week @ 2024-04-18 985734/week @ 2024-04-25 1016215/week @ 2024-05-02 997982/week @ 2024-05-09 1050815/week @ 2024-05-16 1021812/week @ 2024-05-23 1130545/week @ 2024-05-30 1099933/week @ 2024-06-06 1126556/week @ 2024-06-13 1141260/week @ 2024-06-20 950620/week @ 2024-06-27

每月 4,542,204 次下载
用于 382 个crate (6 直接)

MIT 许可证

25KB
112

提供宏简化操作符重载。有关详细信息和支持的操作符,请参阅文档

示例

extern crate overload;
use overload::overload;
use std::ops; // <- don't forget this or you'll get nasty errors

#[derive(PartialEq, Debug)]
struct Val {
    v: i32
}

overload!((a: ?Val) + (b: ?Val) -> Val { Val { v: a.v + b.v } });

上面片段中的宏调用生成以下代码

impl ops::Add<Val> for Val {
    type Output = Val;
    fn add(self, b: Val) -> Self::Output {
        let a = self;
        Val { v: a.v + b.v }
    }
}
impl ops::Add<&Val> for Val {
    type Output = Val;
    fn add(self, b: &Val) -> Self::Output {
        let a = self;
        Val { v: a.v + b.v }
    }
}
impl ops::Add<Val> for &Val {
    type Output = Val;
    fn add(self, b: Val) -> Self::Output {
        let a = self;
        Val { v: a.v + b.v }
    }
}
impl ops::Add<&Val> for &Val {
    type Output = Val;
    fn add(self, b: &Val) -> Self::Output {
        let a = self;
        Val { v: a.v + b.v }
    }
}

现在我们可以以任何组合添加 Val&Val

assert_eq!(Val{v:3} + Val{v:5}, Val{v:8});
assert_eq!(Val{v:3} + &Val{v:5}, Val{v:8});
assert_eq!(&Val{v:3} + Val{v:5}, Val{v:8});
assert_eq!(&Val{v:3} + &Val{v:5}, Val{v:8});

无运行时依赖