5 个版本
| 0.2.1 | 2023年3月2日 |
|---|---|
| 0.2.0 | 2023年2月27日 |
| 0.1.2 | 2023年2月21日 |
| 0.1.1 | 2022年11月27日 |
| 0.1.0 | 2022年7月16日 |
#9 在 #overload
357 每月下载量
用于 4 个软件包 (2 直接)
43KB
1K SLoC
二进制操作自动实现进程宏
#[auto_ops] 使 T += U,T + U,T + &U,&T + U,&T + &U 从 T += &U 的实现中自动实现。
支持列表 (@ 是 +,-,*,/,%,&,|,^,<< 或 >>.)
T @= &U=>T @= U,&T @ &U,&T @ U,T @ &U,T @ UT @= U=>T @= &U,&T @ &U,&T @ U,T @ &U,T @ U&T @ &U=>T @= &U,T @= U,&T @ U,T @ &U,T @ U&T @ U=>T @= &U,T @= U,&T @ &U,T @ &U,T @ UT @ &U=>T @= &U,T @= U,&T @ &U,&T @ U,T @ UT @ U=>T @= &U,T @= U,&T @ U,T @ &U,T @ U
示例
use std::ops::*;
#
# #[derive(Clone, Default)]
# struct A<T>(T);
#[auto_impl_ops::auto_ops]
impl<M> AddAssign<&A<M>> for A<M>
where
for<'x> &'x M: Add<Output = M>,
{
fn add_assign(&mut self, other: &Self) {
self.0 = &self.0 + &other.0;
}
}
上述代码展开为以下代码。更多示例请参阅 examples/a.rs。
use std::ops::*;
#
# #[derive(Clone, Default)]
# struct A<T>(T);
impl<M> AddAssign<&A<M>> for A<M>
where
for<'x> &'x M: Add<Output = M>,
{
fn add_assign(&mut self, other: &Self) {
self.0 = &self.0 + &other.0;
}
}
#[allow(clippy::extra_unused_lifetimes)]
impl<M> AddAssign<A<M>> for A<M>
where
for<'x> &'x M: Add<Output = M>,
{
fn add_assign(&mut self, rhs: A<M>) {
let rhs = &rhs;
self.add_assign(rhs);
}
}
impl<M> Add<&A<M>> for &A<M>
where
for<'x> &'x M: Add<Output = M>,
A<M>: Clone,
{
type Output = A<M>;
fn add(self, rhs: &A<M>) -> Self::Output {
let mut lhs = self.clone();
lhs.add_assign(rhs);
lhs
}
}
impl<M> Add<A<M>> for &A<M>
where
for<'x> &'x M: Add<Output = M>,
A<M>: Clone,
{
type Output = A<M>;
fn add(self, rhs: A<M>) -> Self::Output {
let mut lhs = self.clone();
let rhs = &rhs;
lhs.add_assign(rhs);
lhs
}
}
impl<M> Add<&A<M>> for A<M>
where
for<'x> &'x M: Add<Output = M>,
{
type Output = A<M>;
fn add(self, rhs: &A<M>) -> Self::Output {
let mut lhs = self;
lhs.add_assign(rhs);
lhs
}
}
#[allow(clippy::extra_unused_lifetimes)]
impl<M> Add<A<M>> for A<M>
where
for<'x> &'x M: Add<Output = M>,
{
type Output = A<M>;
fn add(self, rhs: A<M>) -> Self::Output {
let mut lhs = self;
let rhs = &rhs;
lhs.add_assign(rhs);
lhs
}
}
许可证
auto-impl-ops 是 AGPL-3.0-or-later。此过程宏生成的代码属于 AGPL 例外。您可以根据自己的喜好选择其许可证。
依赖关系
~1.5MB
~36K SLoC