Skip to content

Commit 9e82ffe

Browse files
committed
Add augmented assignment (rust-lang#5992), sans wiring.
This is the first part of rust-lang#5992, covering the traits and their implementations and documentation of it all, but not including the wiring to make the actual operators (such as `+=`) desugar to the appropriate method call. This comes from my old augmented-assignment branch which had the wiring also but wasn't quite right. Things have changed enough that that wiring is utterly defunct and unworthy of any attempt at resurrection. The traits, however, were not, so I have restored that part of the work. All places in the present code base where any of the arithmetic traits (`Add`, `Sub`, `Mul`, `Div`, `Rem`, `BitAnd`, `BitOr`, `BitXor`, `Shl` and `Shr`) were implemented has an `*Assign` trait implementation added, with the exception (as before and as noted in the code) of `&str` and `&[T]` where the assignment operators cannot be implemented. Note that there is necessarily no default implementation of the `*Assign` traits, as that would preclude more efficient implementations of the augmented assignment operators and render the whole thing utterly pointless (c.f. rust-lang#7059 on function specialisation).
1 parent 5527c5d commit 9e82ffe

File tree

17 files changed

+358
-10
lines changed

17 files changed

+358
-10
lines changed

src/doc/rust.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,28 +2044,48 @@ These language items are traits:
20442044

20452045
* `add`
20462046
: Elements can be added (for example, integers and floats).
2047+
* `add_assign`
2048+
: Elements can be added with augmented assignment.
20472049
* `sub`
20482050
: Elements can be subtracted.
2051+
* `sub_assign`
2052+
: Elements can be subtracted with augmented assignment.
20492053
* `mul`
20502054
: Elements can be multiplied.
2055+
* `mul_assign`
2056+
: Elements can be multiplied with augmented assignment.
20512057
* `div`
20522058
: Elements have a division operation.
2059+
* `div_assign`
2060+
: Elements have a division operation with augmented assignment.
20532061
* `rem`
20542062
: Elements have a remainder operation.
2063+
* `rem_assign`
2064+
: Elements have a remainder operation with augmented assignment.
20552065
* `neg`
20562066
: Elements can be negated arithmetically.
20572067
* `not`
20582068
: Elements can be negated logically.
20592069
* `bitxor`
20602070
: Elements have an exclusive-or operation.
2071+
* `bitxor_assign`
2072+
: Elements have an exclusive-or operation with augmented assignment.
20612073
* `bitand`
20622074
: Elements have a bitwise `and` operation.
2075+
* `bitand_assign`
2076+
: Elements have a bitwise `and` operation with augmented assignment.
20632077
* `bitor`
20642078
: Elements have a bitwise `or` operation.
2079+
* `bitor_assign`
2080+
: Elements have a bitwise `or` operation with augmented assignment.
20652081
* `shl`
20662082
: Elements have a left shift operation.
2083+
* `shl_assign`
2084+
: Elements have a left shift operation with augmented assignment.
20672085
* `shr`
20682086
: Elements have a right shift operation.
2087+
* `shr_assign`
2088+
: Elements have a right shift operation with augmented assignment.
20692089
* `index`
20702090
: Elements can be indexed.
20712091
* `eq`

src/etc/kate/rust.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,26 @@
5858
<item> Ptr </item>
5959
<item> Drop </item>
6060
<item> Add </item>
61+
<item> AddAssign </item>
6162
<item> Sub </item>
63+
<item> SubAssign </item>
6264
<item> Mul </item>
63-
<item> Quot </item>
65+
<item> MulAssign </item>
66+
<item> Div </item>
67+
<item> DivAssign </item>
6468
<item> Rem </item>
69+
<item> RemAssign </item>
6570
<item> Neg </item>
6671
<item> BitAnd </item>
72+
<item> BitAndAssign </item>
6773
<item> BitOr </item>
74+
<item> BitOrAssign </item>
6875
<item> BitXor </item>
76+
<item> BitXorAssign </item>
6977
<item> Shl </item>
78+
<item> ShlAssign </item>
7079
<item> Shr </item>
80+
<item> ShrAssign </item>
7181
<item> Index </item>
7282
<item> Not </item>
7383
</list>

src/etc/vim/syntax/rust.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ syn keyword rustType f64 i8 i16 i32 i64 str Self
6464
" Core operators {{{3
6565
syn keyword rustTrait Copy Send Sized Share
6666
syn keyword rustTrait Add Sub Mul Div Rem Neg Not
67+
syn keyword rustTrait AddAssign SubAssign MulAssign DivAssign RemAssign
6768
syn keyword rustTrait BitAnd BitOr BitXor
69+
syn keyword rustTrait BitAndAssign BitOrAssign BitXorAssign
6870
syn keyword rustTrait Drop Deref DerefMut
6971
syn keyword rustTrait Shl Shr Index
72+
syn keyword rustTrait ShlAssign ShrAssign
7073
syn keyword rustEnum Option
7174
syn keyword rustEnumVariant Some None
7275
syn keyword rustEnum Result

src/libcollections/enum_set.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,36 @@ impl<E:CLike> Sub<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
8888
}
8989
}
9090

91+
impl<E:CLike> SubAssign<EnumSet<E>> for EnumSet<E> {
92+
fn sub_assign(&mut self, e: &EnumSet<E>) {
93+
self.bits &= !e.bits;
94+
}
95+
}
96+
9197
impl<E:CLike> BitOr<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
9298
fn bitor(&self, e: &EnumSet<E>) -> EnumSet<E> {
9399
EnumSet {bits: self.bits | e.bits}
94100
}
95101
}
96102

103+
impl<E:CLike> BitOrAssign<EnumSet<E>> for EnumSet<E> {
104+
fn bitor_assign(&mut self, e: &EnumSet<E>) {
105+
self.bits |= e.bits;
106+
}
107+
}
108+
97109
impl<E:CLike> BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
98110
fn bitand(&self, e: &EnumSet<E>) -> EnumSet<E> {
99111
EnumSet {bits: self.bits & e.bits}
100112
}
101113
}
102114

115+
impl<E:CLike> BitAndAssign<EnumSet<E>> for EnumSet<E> {
116+
fn bitand_assign(&mut self, e: &EnumSet<E>) {
117+
self.bits &= e.bits;
118+
}
119+
}
120+
103121
/// An iterator over an EnumSet
104122
pub struct Items<E> {
105123
index: uint,

0 commit comments

Comments
 (0)