|
| 1 | +- Feature Name: op_assign |
| 2 | +- Start Date: 2015-03-08 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Add the family of `[Op]Assign` traits to allow overloading assignment |
| 9 | +operations like `a += b`. |
| 10 | + |
| 11 | +# Motivation |
| 12 | + |
| 13 | +We already let users overload the binary operations, letting them overload the |
| 14 | +assignment version is the next logical step. Plus, this sugar is important to |
| 15 | +make mathematical libraries more palatable. |
| 16 | + |
| 17 | +# Detailed design |
| 18 | + |
| 19 | +Add the following **unstable** traits to libcore and reexported them in stdlib: |
| 20 | + |
| 21 | +``` |
| 22 | +// `+=` |
| 23 | +#[lang = "add_assign"] |
| 24 | +trait AddAssign<Rhs=Self> { |
| 25 | + fn add_assign(&mut self, &Rhs); |
| 26 | +} |
| 27 | +
|
| 28 | +// the remaining traits have the same signature |
| 29 | +// (lang items have been omitted for brevity) |
| 30 | +trait BitAndAssign { .. } // `&=` |
| 31 | +trait BitOrAssign { .. } // `|=` |
| 32 | +trait BitXorAssign { .. } // `^=` |
| 33 | +trait DivAssign { .. } // `/=` |
| 34 | +trait MulAssign { .. } // `*=` |
| 35 | +trait RemAssign { .. } // `%=` |
| 36 | +trait ShlAssign { .. } // `<<=` |
| 37 | +trait ShrAssign { .. } // `>>=` |
| 38 | +trait SubAssign { .. } // `-=` |
| 39 | +``` |
| 40 | + |
| 41 | +Implement these traits for the primitive numeric types *without* overloading, |
| 42 | +i.e. only `impl AddAssign<i32> for i32 { .. }`. |
| 43 | + |
| 44 | +Add an `op_assign` feature gate. When the feature gate is enabled, the compiler |
| 45 | +will consider these traits when typecheking `a += b`. Without the feature gate |
| 46 | +the compiler will enforce that `a` and `b` must be primitives of the same |
| 47 | +type/category as it does today. |
| 48 | + |
| 49 | +Once we feel comfortable with the implementation we'll remove the feature gate |
| 50 | +and mark the traits as stable. This can be done after 1.0 as this change is |
| 51 | +backwards compatible. |
| 52 | + |
| 53 | +# Drawbacks |
| 54 | + |
| 55 | +None that I can think of. |
| 56 | + |
| 57 | +# Alternatives |
| 58 | + |
| 59 | +Alternatively, we could change the traits to take the RHS by value. This makes |
| 60 | +them more "flexible" as the user can pick by value or by reference, but makes |
| 61 | +the use slightly unergonomic in the by ref case as the borrow must be explicit |
| 62 | +e.g. `x += &big_float;` vs `x += big_float;`. |
| 63 | + |
| 64 | +# Unresolved questions |
| 65 | + |
| 66 | +Are there any use cases of assignment operations where the RHS has to be taken |
| 67 | +by value for the operation to be performant (e.g. to avoid internal cloning)? |
| 68 | + |
| 69 | +Should we overload `ShlAssign` and `ShrAssign`, e.g. |
| 70 | +`impl ShlAssign<u8> for i32`, since we have already overloaded the `Shl` and |
| 71 | +`Shr` traits? |
| 72 | + |
| 73 | +Should we overload all the traits for references, e.g. |
| 74 | +`impl<'a> AddAssign<&'a i32> for i32` to allow `x += &0;`? |
0 commit comments