When implementing a primitive vector type of fixed size (e.g. float2 ), I want to support the Add and Sub attributes. Later I want to support Mul and *Assign .
Picking up the documentation and other examples, I came up with the following:
use std::ops::{Add, Sub};
This works for basic examples, but in practice I often float2 across links passed as arguments, as well as local float2 on the stack.
To mix them, I needed either:
- Link variables (OK, but makes the code a little less readable).
- Declare an operator by overloading link combinations.
Example:
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 */
Although this allows you to write expressions without dereference. listing of each combination becomes quite tedious, especially when adding more operations and types ( float3 , float4 ...).
Is there a generally accepted way ...
- Automatic type enforcement for operator overloading?
- Use macros or any other language feature to avoid tedious repetition?
Or developers are also expected to:
- Explicitly access variables as references as needed.
- Explicitly deactivate variables if necessary.
- Write many repetitive operator overload functions.
Please note that I am currently a beginner, I checked out some pretty advanced math libraries in Rust, they went through my head, although I could use them. I would like to understand how to write operator overloading for my own types.
operator-overloading rust
ideasman42
source share