@@ -16,13 +16,13 @@ make mathematical libraries more palatable.
16
16
17
17
# Detailed design
18
18
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 :
20
20
21
21
```
22
22
// `+=`
23
23
#[lang = "add_assign"]
24
24
trait AddAssign<Rhs=Self> {
25
- fn add_assign(&mut self, & Rhs);
25
+ fn add_assign(&mut self, Rhs);
26
26
}
27
27
28
28
// the remaining traits have the same signature
@@ -50,36 +50,37 @@ Once we feel comfortable with the implementation we'll remove the feature gate
50
50
and mark the traits as stable. This can be done after 1.0 as this change is
51
51
backwards compatible.
52
52
53
- ## RHS: By ref vs by value
53
+ ## RHS: By value vs by ref
54
54
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.
62
61
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
66
69
67
70
# Drawbacks
68
71
69
72
None that I can think of.
70
73
71
74
# Alternatives
72
75
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> ` .
77
81
78
82
# Unresolved questions
79
83
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
-
83
84
Should we overload ` ShlAssign ` and ` ShrAssign ` , e.g.
84
85
` impl ShlAssign<u8> for i32 ` , since we have already overloaded the ` Shl ` and
85
86
` Shr ` traits?
0 commit comments