#[op_variants]Expand description
Procedural macro to generate variants for arithmetic operations.
Supports binary operations (like Add), binary assign operations (like
AddAssign), and unary operations (like Neg).
For binary operations (impl with -> Self::Output), it requires Self op &RHS;
for unary operations it assumes op Self.
There are three supported variants:
owned: All operands owned - implemented by referencing rhs.borrowed: All operands borrowed - implemented by cloning Self.flipped:&Self op RHS- implemented by cloning lhs and referencing rhs (binary ops only).flipped_commutative:&Self op RHS- implemented asrhs.op(lhs). To be used only in commutative ops (e.g., Add & Mul, but not Sub | Div).
ยงUsage
#[derive(Clone, Debug)]
struct Point {
x: i32,
y: i32,
}
// Binary operation: Point + &Point (base impl)
#[macros::op_variants(owned, borrowed, flipped)]
impl std::ops::Add<&Point> for Point {
type Output = Point;
fn add(self, other: &Point) -> Point {
Point {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
// Generates: Point + Point, &Point + &Point, &Point + Point
// Binary assign operation: Point += &Point (base impl)
#[macros::op_variants(owned)]
impl std::ops::AddAssign<&Point> for Point {
fn add_assign(&mut self, other: &Point) {
self.x += other.x;
self.y += other.y;
}
}
// Generates: Point += Point
// Unary operation: -&Point (base impl)
#[macros::op_variants(owned)]
impl std::ops::Neg for &Point {
type Output = Point;
fn neg(self) -> Point {
Point {
x: -self.x,
y: -self.y,
}
}
}
// Generates: -Point