繁体   English   中英

如何在 Rust 中为值和引用实现惯用的运算符重载?

[英]How to implement idiomatic operator overloading for values and references in Rust?

在实现基本的固定大小向量类型(例如float2 )时,我想支持AddSub特征。 稍后,我将要支持Mul*Assign

查找文档和其他示例,我想出了这个:

use std::ops::{Add, Sub};

#[derive(Copy, Clone)]
struct float2(f64, f64);

impl Add for float2 {
    type Output = float2;
    fn add(self, _rhs: float2) -> float2 {
        float2(self.0 + _rhs.0, self.1 + _rhs.1)
    }
}

impl Sub for float2 {
    type Output = float2;
    fn sub(self, _rhs: float2) -> float2 {
        float2(self.0 - _rhs.0, self.1 - _rhs.1)
    }
}

这适用于基本示例,但是我发现在实践中我经常以引用作为参数以及堆栈上的本地float2的引用结束。

要混合这些,我需要:

  • 取消引用变量(好的,但会使代码的可读性稍差)。
  • 也声明引用的运算符重载组合。

例子:

impl<'a, 'b> Add<&'b float2> for &'a float2 {
    type Output = float2;
    fn add(self, _rhs: &'b float2) -> float2 {
        float2(self.0 + _rhs.0, self.1 + _rhs.1)
    }
}
impl<'a> Add<float2> for &'a float2 {
    type Output = float2;
    fn add(self, _rhs: float2) -> float2 {
        float2(self.0 + _rhs.0, self.1 + _rhs.1)
    }
}
impl<'b> Add<&'b float2> for float2 {
    type Output = float2;
    fn add(self, _rhs: &'b float2) -> float2 {
        float2(self.0 + _rhs.0, self.1 + _rhs.1)
    }
}

/*... and again for Sub */

虽然这允许在不取消引用的情况下编写表达式。 枚举每个组合变得非常乏味,尤其是在添加更多操作和类型( float3float4 ...)时。

有没有普遍接受的方法来...

  • 为运算符重载自动强制类型?
  • 使用宏或语言的其他一些特性来避免繁琐的重复?

或者是否期望开发人员:

  • 根据需要显式访问变量作为引用。
  • 根据需要显式取消引用变量。
  • 写了很多重复的操作符重载函数。

请注意,我目前是一名初学者,我在 Rust 中检查了一些非常高级的数学库,它们超出了我的脑海,而我可以使用它们 - 我想了解如何为我自己的类型编写运算符重载。

Rust 的伟大之处在于它是开源的。 这意味着您可以看到该语言的作者如何解决问题。 最接近的模拟是原始整数类型

macro_rules! add_impl {
    ($($t:ty)*) => ($(
        #[stable(feature = "rust1", since = "1.0.0")]
        impl Add for $t {
            type Output = $t;

            #[inline]
            fn add(self, other: $t) -> $t { self + other }
        }

        forward_ref_binop! { impl Add, add for $t, $t }
    )*)
}

forward_ref_binop定义为

macro_rules! forward_ref_binop {
    (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a> $imp<$u> for &'a $t {
            type Output = <$t as $imp<$u>>::Output;

            #[inline]
            fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
                $imp::$method(*self, other)
            }
        }

        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a> $imp<&'a $u> for $t {
            type Output = <$t as $imp<$u>>::Output;

            #[inline]
            fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
                $imp::$method(self, *other)
            }
        }

        #[stable(feature = "rust1", since = "1.0.0")]
        impl<'a, 'b> $imp<&'a $u> for &'b $t {
            type Output = <$t as $imp<$u>>::Output;

            #[inline]
            fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
                $imp::$method(*self, *other)
            }
        }
    }
}

为引用编写特征的包装器实现当然是有效的,这些引用只是取消引用并调用面向值的版本。

我建议您为此目的使用impl_os crate,请参阅我写的其他答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM