]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/augmented-assignments.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / augmented-assignments.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(augmented_assignments)]
12 #![feature(op_assign_traits)]
13
14 use std::mem;
15 use std::ops::{
16     AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, Index, MulAssign, RemAssign,
17     ShlAssign, ShrAssign, SubAssign,
18 };
19
20 #[derive(Debug, PartialEq)]
21 struct Int(i32);
22
23 struct Slice([i32]);
24
25 impl Slice {
26     fn new(slice: &mut [i32]) -> &mut Slice {
27         unsafe {
28             mem::transmute(slice)
29         }
30     }
31 }
32
33 fn main() {
34     let mut x = Int(1);
35
36     x += Int(2);
37     assert_eq!(x, Int(0b11));
38
39     x &= Int(0b01);
40     assert_eq!(x, Int(0b01));
41
42     x |= Int(0b10);
43     assert_eq!(x, Int(0b11));
44
45     x ^= Int(0b01);
46     assert_eq!(x, Int(0b10));
47
48     x /= Int(2);
49     assert_eq!(x, Int(1));
50
51     x *= Int(3);
52     assert_eq!(x, Int(3));
53
54     x %= Int(2);
55     assert_eq!(x, Int(1));
56
57     // overloaded RHS
58     x <<= 1u8;
59     assert_eq!(x, Int(2));
60
61     x <<= 1u16;
62     assert_eq!(x, Int(4));
63
64     x >>= 1u8;
65     assert_eq!(x, Int(2));
66
67     x >>= 1u16;
68     assert_eq!(x, Int(1));
69
70     x -= Int(1);
71     assert_eq!(x, Int(0));
72
73     // indexed LHS
74     let mut v = vec![Int(1), Int(2)];
75     v[0] += Int(2);
76     assert_eq!(v[0], Int(3));
77
78     // unsized RHS
79     let mut array = [0, 1, 2];
80     *Slice::new(&mut array) += 1;
81     assert_eq!(array[0], 1);
82     assert_eq!(array[1], 2);
83     assert_eq!(array[2], 3);
84 }
85
86 impl AddAssign for Int {
87     fn add_assign(&mut self, rhs: Int) {
88         self.0 += rhs.0;
89     }
90 }
91
92 impl BitAndAssign for Int {
93     fn bitand_assign(&mut self, rhs: Int) {
94         self.0 &= rhs.0;
95     }
96 }
97
98 impl BitOrAssign for Int {
99     fn bitor_assign(&mut self, rhs: Int) {
100         self.0 |= rhs.0;
101     }
102 }
103
104 impl BitXorAssign for Int {
105     fn bitxor_assign(&mut self, rhs: Int) {
106         self.0 ^= rhs.0;
107     }
108 }
109
110 impl DivAssign for Int {
111     fn div_assign(&mut self, rhs: Int) {
112         self.0 /= rhs.0;
113     }
114 }
115
116 impl MulAssign for Int {
117     fn mul_assign(&mut self, rhs: Int) {
118         self.0 *= rhs.0;
119     }
120 }
121
122 impl RemAssign for Int {
123     fn rem_assign(&mut self, rhs: Int) {
124         self.0 %= rhs.0;
125     }
126 }
127
128 impl ShlAssign<u8> for Int {
129     fn shl_assign(&mut self, rhs: u8) {
130         self.0 <<= rhs;
131     }
132 }
133
134 impl ShlAssign<u16> for Int {
135     fn shl_assign(&mut self, rhs: u16) {
136         self.0 <<= rhs;
137     }
138 }
139
140 impl ShrAssign<u8> for Int {
141     fn shr_assign(&mut self, rhs: u8) {
142         self.0 >>= rhs;
143     }
144 }
145
146 impl ShrAssign<u16> for Int {
147     fn shr_assign(&mut self, rhs: u16) {
148         self.0 >>= rhs;
149     }
150 }
151
152 impl SubAssign for Int {
153     fn sub_assign(&mut self, rhs: Int) {
154         self.0 -= rhs.0;
155     }
156 }
157
158 impl AddAssign<i32> for Slice {
159     fn add_assign(&mut self, rhs: i32) {
160         for lhs in &mut self.0 {
161             *lhs += rhs;
162         }
163     }
164 }