@@ -27,7 +27,7 @@ enum_string!(pub Opcode {
27
27
} ) ;
28
28
29
29
impl Default for Opcode {
30
- fn default ( ) -> Opcode {
30
+ fn default ( ) -> Self {
31
31
Opcode :: Dat
32
32
}
33
33
}
@@ -49,13 +49,13 @@ enum_string!(pub Modifier {
49
49
} ) ;
50
50
51
51
impl Default for Modifier {
52
- fn default ( ) -> Modifier {
52
+ fn default ( ) -> Self {
53
53
Modifier :: F
54
54
}
55
55
}
56
56
57
57
impl Modifier {
58
- pub fn default_88_to_94 ( opcode : Opcode , a_mode : AddressMode , b_mode : AddressMode ) -> Modifier {
58
+ pub fn default_88_to_94 ( opcode : Opcode , a_mode : AddressMode , b_mode : AddressMode ) -> Self {
59
59
/// Implemented based on the ICWS '94 document,
60
60
/// section A.2.1.2: ICWS'88 to ICWS'94 Conversion
61
61
use Opcode :: * ;
@@ -151,18 +151,30 @@ impl ToString for Field {
151
151
}
152
152
}
153
153
154
- #[ derive( Clone , Debug , Default , PartialEq ) ]
154
+ #[ derive( Clone , Debug , PartialEq ) ]
155
155
pub struct Instruction {
156
156
pub opcode : Opcode ,
157
157
pub modifier : Modifier ,
158
158
pub field_a : Field ,
159
159
pub field_b : Field ,
160
160
}
161
161
162
+ impl Default for Instruction {
163
+ fn default ( ) -> Self {
164
+ Instruction {
165
+ opcode : Opcode :: default ( ) ,
166
+ modifier : Modifier :: default ( ) ,
167
+ field_a : Field :: direct ( 0 ) ,
168
+ field_b : Field :: direct ( 0 ) ,
169
+ }
170
+ }
171
+ }
172
+
162
173
impl Instruction {
163
- pub fn new ( opcode : Opcode , field_a : Field , field_b : Field ) -> Instruction {
174
+ pub fn new ( opcode : Opcode , field_a : Field , field_b : Field ) -> Self {
164
175
let modifier =
165
176
Modifier :: default_88_to_94 ( opcode, field_a. address_mode , field_b. address_mode ) ;
177
+
166
178
Instruction {
167
179
opcode,
168
180
modifier,
@@ -191,7 +203,7 @@ pub struct Core {
191
203
}
192
204
193
205
impl Core {
194
- pub fn new ( core_size : usize ) -> Core {
206
+ pub fn new ( core_size : usize ) -> Self {
195
207
Core {
196
208
instructions : vec ! [ Instruction :: default ( ) ; core_size] ,
197
209
labels : HashMap :: new ( ) ,
@@ -219,17 +231,18 @@ impl Core {
219
231
) ) ;
220
232
}
221
233
222
- // TODO: make this nice with some kind of try_insert
223
- labels
224
- . into_iter ( )
225
- . map ( |label| match self . labels . entry ( label . into ( ) ) {
226
- Entry :: Occupied ( entry ) => Err ( format ! ( "Duplicate label '{}'" , entry . key ( ) ) ) ,
234
+ for label in labels {
235
+ match self . labels . entry ( label . into ( ) ) {
236
+ Entry :: Occupied ( entry ) => {
237
+ return Err ( format ! ( "Label '{}' already exists" , entry. key ( ) ) ) ;
238
+ }
227
239
Entry :: Vacant ( entry) => {
228
240
entry. insert ( index) ;
229
- Ok ( ( ) )
230
241
}
231
- } )
232
- . collect ( )
242
+ }
243
+ }
244
+
245
+ Ok ( ( ) )
233
246
}
234
247
235
248
pub fn label_address ( & self , label : & str ) -> Option < usize > {
@@ -255,7 +268,7 @@ impl Core {
255
268
}
256
269
257
270
impl Default for Core {
258
- fn default ( ) -> Core {
271
+ fn default ( ) -> Self {
259
272
Core :: new ( DEFAULT_CORE_SIZE )
260
273
}
261
274
}
@@ -431,6 +444,7 @@ mod tests {
431
444
assert_eq ! ( core. label_address( "bar" ) . unwrap( ) , 0 ) ;
432
445
assert_eq ! ( core. label_address( "baz" ) . unwrap( ) , 123 ) ;
433
446
assert_eq ! ( core. label_address( "boo" ) . unwrap( ) , 123 ) ;
447
+
434
448
assert ! ( core. label_address( "goblin" ) . is_none( ) ) ;
435
449
assert ! ( core. label_address( "never_mentioned" ) . is_none( ) ) ;
436
450
}
0 commit comments