@@ -16,13 +16,13 @@ make mathematical libraries more palatable.
1616
1717# Detailed design
1818
19- Add the following ** unstable** traits to libcore and reexported them in stdlib :
19+ Add the following ** unstable** traits to libcore and reexported them in libstd :
2020
2121```
2222// `+=`
2323#[lang = "add_assign"]
2424trait AddAssign<Rhs=Self> {
25- fn add_assign(&mut self, & Rhs);
25+ fn add_assign(&mut self, Rhs);
2626}
2727
2828// the remaining traits have the same signature
@@ -50,36 +50,37 @@ Once we feel comfortable with the implementation we'll remove the feature gate
5050and mark the traits as stable. This can be done after 1.0 as this change is
5151backwards compatible.
5252
53- ## RHS: By ref vs by value
53+ ## RHS: By value vs by ref
5454
55- This RFC proposes that the assignment operations take the RHS always by ref;
56- instead of by value like the "normal" binary operations (e.g. ` Add ` ) do. The
57- rationale is that, as far as the author has seen in practice [ 1] , one never
58- wants to mutate the RHS or consume it, or in other words an immutable view into
59- the RHS is enough to perform the operation. Therefore, this RFC follows in the
60- footsteps of the ` Index ` traits, where the same situation arises with the
61- indexing value, and by ref was chosen over by value.
55+ Taking the RHS by value is more flexible. The implementations allowed with
56+ a by value RHS are a superset of the implementations allowed with a by ref RHS.
57+ An example where taking the RHS by value is necessary would be operator sugar
58+ for extending a collection with an iterator [ 1] : ` vec ++= iter ` where
59+ ` vec: Vec<T> ` and ` iter impls Iterator<T> ` . This can't be implemented with the
60+ by ref version as the iterator couldn't be advanced in that case.
6261
63- [ 1] It could be possible that the author is not aware of use cases where taking
64- RHS by value is necessary. Feedback on this matter would be appreciated. (See
65- the first unresolved question)
62+ [ 1] Where ` ++ ` is the "combine" operator that has been proposed [ elsewhere] .
63+ Note that this RFC doesn't propose adding that particular operator or adding
64+ similar overloaded operations (` vec += iter ` ) to stdlib's collections, but it
65+ leaves the door open to the possibility of adding them in the future (if
66+ desired).
67+
68+ [ elsewhere ] : https://github.com/rust-lang/rfcs/pull/203
6669
6770# Drawbacks
6871
6972None that I can think of.
7073
7174# Alternatives
7275
73- Alternatively, we could change the traits to take the RHS by value. This makes
74- them more "flexible" as the user can pick by value or by reference, but makes
75- the use slightly unergonomic in the by ref case as the borrow must be explicit
76- e.g. ` x += &big_float; ` vs ` x += big_float; ` .
76+ Take the RHS by ref. This is less flexible than taking the RHS by value but, in
77+ some instances, it can save writing ` &rhs ` when the RHS is owned and the
78+ implementation demands a reference. However, this last point will be moot if we
79+ implement auto-referencing for binary operators, as ` lhs += rhs ` would actually
80+ call ` add_assign(&mut lhs, &rhs) ` if ` Lhs impls AddAssign<&Rhs> ` .
7781
7882# Unresolved questions
7983
80- Are there any use cases of assignment operations where the RHS has to be taken
81- by value for the operation to be performant (e.g. to avoid internal cloning)?
82-
8384Should we overload ` ShlAssign ` and ` ShrAssign ` , e.g.
8485` impl ShlAssign<u8> for i32 ` , since we have already overloaded the ` Shl ` and
8586` Shr ` traits?
0 commit comments