在Rust中,您可以重载运算符(+
,-
,/
,*
,+=
等)。我的Vec3
类型有一个简单的添加实现:
use std::ops::Add;
struct Vec3 {
e0: f32,
e1: f32,
e2: f32,
}
impl Add<f32> for &Vec3 {
type Output = Vec3;
fn add(self, other: f32) -> Vec3 {
Vec3 {
e0: self.e0 + other,
e1: self.e1 + other,
e2: self.e2 + other,
}
}
}
我可以通过以下方式使用它:
let result = my_vec_3 + 43f32;
但是以另一种方式出错:
let this_wont_compile = 43f32 + my_vec_3;
error[E0277]: cannot add `Vec3` to `f32`
--> src/lib.rs:23:35
|
23 | let this_wont_compile = 43f32 + my_vec_3;
| ^ no implementation for `f32 + Vec3`
|
= help: the trait `std::ops::Add<Vec3>` is not implemented for `f32`
我知道我可以为impl Add<&Vec3> for f32
编写一个实现,但这就是我想要实现的自动化。
如何编写实现,使得LHS
和RHS
可以互换? Rust中有可能吗?
答案 0 :(得分:2)
该语言无法自动为您完成
。您的选择是:
zrzka非常友好,可以构造一个Rust Playground,它为此特定用例提供示例宏。
此question还提供了一些有用的提示,因为Rust Source Code本身使用这些宏来自动执行一些乏味的操作。
我担心的一个问题是,如果使用宏,则必须将其内联(例如vec![]
)。由于宏是在编译时扩展的,因此您的宏将为您生成可以正常调用的函数。 RLS仍将提供语法支持,并且一切都会按您预期的那样工作。
这是我最终要执行的实现。我敢肯定,还有更多可以实现自动化的功能(forward_ref_binop
之一),但我对此感到满意。
/// Generates the operations for vector methods. `let result = my_vec_3 + my_other_vec3`
/// Handles `Vec3, Vec3`, `Vec3, &Vec3`, `&Vec3, Vec3`, `&Vec3, &Vec3`
/// `vec3_vec3_op(ops::AddAssign, add_assign)` (note the camelcase add_assign name)
macro_rules! vec3_vec3_op {
($($path:ident)::+, $fn:ident) => {
impl $($path)::+<Vec3> for Vec3 {
type Output = Vec3;
fn $fn(self, other: Vec3) -> Self::Output {
Vec3 {
e0: self.e0.$fn(other.e0),
e1: self.e1.$fn(other.e1),
e2: self.e2.$fn(other.e2),
}
}
}
impl $($path)::+<&Vec3> for &Vec3 {
type Output = Vec3;
fn $fn(self, other: &Vec3) -> Self::Output {
Vec3 {
e0: self.e0.$fn(other.e0),
e1: self.e1.$fn(other.e1),
e2: self.e2.$fn(other.e2),
}
}
}
impl $($path)::+<&Vec3> for Vec3 {
type Output = Vec3;
fn $fn(self, other: &Vec3) -> Self::Output {
Vec3 {
e0: self.e0.$fn(other.e0),
e1: self.e1.$fn(other.e1),
e2: self.e2.$fn(other.e2),
}
}
}
impl $($path)::+<Vec3> for &Vec3 {
type Output = Vec3;
fn $fn(self, other: Vec3) -> Self::Output {
Vec3 {
e0: self.e0.$fn(other.e0),
e1: self.e1.$fn(other.e1),
e2: self.e2.$fn(other.e2),
}
}
}
};
}
/// Generates the operations for vector method assignment. `my_vec += my_other_vec`
/// Handles `Vec3, Vec3` and `Vec3, &Vec3`
/// `vec3_vec3_opassign(ops::AddAssign, add_assign)` (note the camelcase add_assign name)
macro_rules! vec3_vec3_opassign {
($($path:ident)::+, $fn:ident) => {
impl $($path)::+<Vec3> for Vec3 {
fn $fn(&mut self, other: Vec3) {
self.e0.$fn(other.e0);
self.e1.$fn(other.e1);
self.e2.$fn(other.e2);
}
}
impl $($path)::+<&Vec3> for Vec3 {
fn $fn(&mut self, other: &Vec3) {
self.e0.$fn(other.e0);
self.e1.$fn(other.e1);
self.e2.$fn(other.e2);
}
}
};
}
/// Generates the operations for method assignment. `my_vec += f32`
/// `vec3_opassign(ops:AddAssign, add_assign)` (note the camelcase add_assign name)
macro_rules! vec3_opassign {
($($path:ident)::+, $fn:ident, $ty:ty) => {
impl $($path)::+<$ty> for Vec3 {
fn $fn(&mut self, other: $ty) {
self.e0.$fn(other);
self.e1.$fn(other);
self.e2.$fn(other);
}
}
}
}
/// Generates the operations for the method. `let result = my_vec + 4f32`
/// Handles `Vec3, T`, `T, Vec3`, `&Vec3, T`, `T, &Vec3`
/// `vec3_op!(ops:Add, add, f32)`
macro_rules! vec3_op {
($($path:ident)::+, $fn:ident, $ty:ty) => {
// impl ops::Add::add for Vec3
impl $($path)::+<$ty> for Vec3 {
type Output = Vec3;
// fn add(self, other: f32) -> Self::Output
fn $fn(self, other: $ty) -> Self::Output {
Vec3 {
// e0: self.e0.add(other)
e0: self.e0.$fn(other),
e1: self.e1.$fn(other),
e2: self.e2.$fn(other),
}
}
}
impl $($path)::+<$ty> for &Vec3 {
type Output = Vec3;
fn $fn(self, other: $ty) -> Self::Output {
Vec3 {
e0: self.e0.$fn(other),
e1: self.e1.$fn(other),
e2: self.e2.$fn(other),
}
}
}
impl $($path)::+<Vec3> for $ty {
type Output = Vec3;
fn $fn(self, other: Vec3) -> Self::Output {
Vec3 {
e0: self.$fn(other.e0),
e1: self.$fn(other.e1),
e2: self.$fn(other.e2),
}
}
}
impl $($path)::+<&Vec3> for $ty {
type Output = Vec3;
fn $fn(self, other: &Vec3) -> Self::Output {
Vec3 {
e0: self.$fn(other.e0),
e1: self.$fn(other.e1),
e2: self.$fn(other.e2),
}
}
}
}
}
macro_rules! vec3_op_for {
($ty: ty) => {
vec3_op!(ops::Add, add, $ty);
vec3_op!(ops::Sub, sub, $ty);
vec3_op!(ops::Mul, mul, $ty);
vec3_op!(ops::Div, div, $ty);
vec3_opassign!(ops::AddAssign, add_assign, $ty);
vec3_opassign!(ops::SubAssign, sub_assign, $ty);
vec3_opassign!(ops::MulAssign, mul_assign, $ty);
vec3_opassign!(ops::DivAssign, div_assign, $ty);
};
}
vec3_vec3_op!(ops::Add, add);
vec3_vec3_op!(ops::Sub, sub);
vec3_vec3_op!(ops::Mul, mul);
vec3_vec3_op!(ops::Div, div);
vec3_vec3_opassign!(ops::AddAssign, add_assign);
vec3_vec3_opassign!(ops::SubAssign, sub_assign);
vec3_vec3_opassign!(ops::MulAssign, mul_assign);
vec3_vec3_opassign!(ops::DivAssign, div_assign);
vec3_op_for!(f32);
从这里开始,如果我扩展了Vec3
类以处理泛型,那么添加其他类型将是微不足道的。