1
- use std:: cell:: { Cell , RefCell } ;
1
+ use std:: cell:: Cell ;
2
2
use std:: fmt;
3
3
4
4
use super :: objiter;
@@ -9,7 +9,7 @@ use crate::exceptions::PyBaseExceptionRef;
9
9
use crate :: function:: { KwArgs , OptionalArg , PyFuncArgs } ;
10
10
use crate :: pyobject:: {
11
11
IdProtocol , IntoPyObject , ItemProtocol , PyAttributes , PyClassImpl , PyContext , PyIterable ,
12
- PyObjectRef , PyRef , PyResult , PyValue ,
12
+ PyObjectRef , PyRef , PyResult , PyValue , ThreadSafe ,
13
13
} ;
14
14
use crate :: vm:: { ReprGuard , VirtualMachine } ;
15
15
@@ -20,9 +20,10 @@ pub type DictContentType = dictdatatype::Dict;
20
20
#[ pyclass]
21
21
#[ derive( Default ) ]
22
22
pub struct PyDict {
23
- entries : RefCell < DictContentType > ,
23
+ entries : DictContentType ,
24
24
}
25
25
pub type PyDictRef = PyRef < PyDict > ;
26
+ impl ThreadSafe for PyDict { }
26
27
27
28
impl fmt:: Debug for PyDict {
28
29
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -43,7 +44,7 @@ impl PyDictRef {
43
44
#[ pyslot]
44
45
fn tp_new ( class : PyClassRef , _args : PyFuncArgs , vm : & VirtualMachine ) -> PyResult < PyDictRef > {
45
46
PyDict {
46
- entries : RefCell :: new ( DictContentType :: default ( ) ) ,
47
+ entries : DictContentType :: default ( ) ,
47
48
}
48
49
. into_ref_with_type ( vm, class)
49
50
}
@@ -59,7 +60,7 @@ impl PyDictRef {
59
60
}
60
61
61
62
fn merge (
62
- dict : & RefCell < DictContentType > ,
63
+ dict : & DictContentType ,
63
64
dict_obj : OptionalArg < PyObjectRef > ,
64
65
kwargs : KwArgs ,
65
66
vm : & VirtualMachine ,
@@ -68,13 +69,13 @@ impl PyDictRef {
68
69
let dicted: Result < PyDictRef , _ > = dict_obj. clone ( ) . downcast ( ) ;
69
70
if let Ok ( dict_obj) = dicted {
70
71
for ( key, value) in dict_obj {
71
- dict. borrow_mut ( ) . insert ( vm, & key, value) ?;
72
+ dict. insert ( vm, & key, value) ?;
72
73
}
73
74
} else if let Some ( keys) = vm. get_method ( dict_obj. clone ( ) , "keys" ) {
74
75
let keys = objiter:: get_iter ( vm, & vm. invoke ( & keys?, vec ! [ ] ) ?) ?;
75
76
while let Some ( key) = objiter:: get_next_object ( vm, & keys) ? {
76
77
let val = dict_obj. get_item ( & key, vm) ?;
77
- dict. borrow_mut ( ) . insert ( vm, & key, val) ?;
78
+ dict. insert ( vm, & key, val) ?;
78
79
}
79
80
} else {
80
81
let iter = objiter:: get_iter ( vm, & dict_obj) ?;
@@ -92,14 +93,13 @@ impl PyDictRef {
92
93
if objiter:: get_next_object ( vm, & elem_iter) ?. is_some ( ) {
93
94
return Err ( err ( vm) ) ;
94
95
}
95
- dict. borrow_mut ( ) . insert ( vm, & key, value) ?;
96
+ dict. insert ( vm, & key, value) ?;
96
97
}
97
98
}
98
99
}
99
100
100
- let mut dict_borrowed = dict. borrow_mut ( ) ;
101
101
for ( key, value) in kwargs. into_iter ( ) {
102
- dict_borrowed . insert ( vm, & vm. new_str ( key) , value) ?;
102
+ dict . insert ( vm, & vm. new_str ( key) , value) ?;
103
103
}
104
104
Ok ( ( ) )
105
105
}
@@ -111,27 +111,26 @@ impl PyDictRef {
111
111
value : OptionalArg < PyObjectRef > ,
112
112
vm : & VirtualMachine ,
113
113
) -> PyResult < PyDictRef > {
114
- let mut dict = DictContentType :: default ( ) ;
114
+ let dict = DictContentType :: default ( ) ;
115
115
let value = value. unwrap_or_else ( || vm. ctx . none ( ) ) ;
116
116
for elem in iterable. iter ( vm) ? {
117
117
let elem = elem?;
118
118
dict. insert ( vm, & elem, value. clone ( ) ) ?;
119
119
}
120
- let entries = RefCell :: new ( dict) ;
121
- PyDict { entries } . into_ref_with_type ( vm, class)
120
+ PyDict { entries : dict } . into_ref_with_type ( vm, class)
122
121
}
123
122
124
123
#[ pymethod( magic) ]
125
124
fn bool ( self ) -> bool {
126
- !self . entries . borrow ( ) . is_empty ( )
125
+ !self . entries . is_empty ( )
127
126
}
128
127
129
128
fn inner_eq ( self , other : & PyDict , vm : & VirtualMachine ) -> PyResult < bool > {
130
- if other. entries . borrow ( ) . len ( ) != self . entries . borrow ( ) . len ( ) {
129
+ if other. entries . len ( ) != self . entries . len ( ) {
131
130
return Ok ( false ) ;
132
131
}
133
132
for ( k, v1) in self {
134
- match other. entries . borrow ( ) . get ( vm, & k) ? {
133
+ match other. entries . get ( vm, & k) ? {
135
134
Some ( v2) => {
136
135
if v1. is ( & v2) {
137
136
continue ;
@@ -170,12 +169,12 @@ impl PyDictRef {
170
169
171
170
#[ pymethod( magic) ]
172
171
fn len ( self ) -> usize {
173
- self . entries . borrow ( ) . len ( )
172
+ self . entries . len ( )
174
173
}
175
174
176
175
#[ pymethod( magic) ]
177
176
fn sizeof ( self ) -> usize {
178
- size_of :: < Self > ( ) + self . entries . borrow ( ) . sizeof ( )
177
+ size_of :: < Self > ( ) + self . entries . sizeof ( )
179
178
}
180
179
181
180
#[ pymethod( magic) ]
@@ -197,17 +196,17 @@ impl PyDictRef {
197
196
198
197
#[ pymethod( magic) ]
199
198
fn contains ( self , key : PyObjectRef , vm : & VirtualMachine ) -> PyResult < bool > {
200
- self . entries . borrow ( ) . contains ( vm, & key)
199
+ self . entries . contains ( vm, & key)
201
200
}
202
201
203
202
#[ pymethod( magic) ]
204
203
fn delitem ( self , key : PyObjectRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
205
- self . entries . borrow_mut ( ) . delete ( vm, & key)
204
+ self . entries . delete ( vm, & key)
206
205
}
207
206
208
207
#[ pymethod]
209
208
fn clear ( self ) {
210
- self . entries . borrow_mut ( ) . clear ( )
209
+ self . entries . clear ( )
211
210
}
212
211
213
212
#[ pymethod( magic) ]
@@ -243,7 +242,7 @@ impl PyDictRef {
243
242
value : PyObjectRef ,
244
243
vm : & VirtualMachine ,
245
244
) -> PyResult < ( ) > {
246
- self . entries . borrow_mut ( ) . insert ( vm, key, value)
245
+ self . entries . insert ( vm, key, value)
247
246
}
248
247
249
248
#[ pymethod( magic) ]
@@ -262,7 +261,7 @@ impl PyDictRef {
262
261
key : K ,
263
262
vm : & VirtualMachine ,
264
263
) -> PyResult < Option < PyObjectRef > > {
265
- if let Some ( value) = self . entries . borrow ( ) . get ( vm, key) ? {
264
+ if let Some ( value) = self . entries . get ( vm, key) ? {
266
265
return Ok ( Some ( value) ) ;
267
266
}
268
267
@@ -281,7 +280,7 @@ impl PyDictRef {
281
280
default : OptionalArg < PyObjectRef > ,
282
281
vm : & VirtualMachine ,
283
282
) -> PyResult {
284
- match self . entries . borrow ( ) . get ( vm, & key) ? {
283
+ match self . entries . get ( vm, & key) ? {
285
284
Some ( value) => Ok ( value) ,
286
285
None => Ok ( default. unwrap_or_else ( || vm. ctx . none ( ) ) ) ,
287
286
}
@@ -294,12 +293,11 @@ impl PyDictRef {
294
293
default : OptionalArg < PyObjectRef > ,
295
294
vm : & VirtualMachine ,
296
295
) -> PyResult {
297
- let mut entries = self . entries . borrow_mut ( ) ;
298
- match entries. get ( vm, & key) ? {
296
+ match self . entries . get ( vm, & key) ? {
299
297
Some ( value) => Ok ( value) ,
300
298
None => {
301
299
let set_value = default. unwrap_or_else ( || vm. ctx . none ( ) ) ;
302
- entries. insert ( vm, & key, set_value. clone ( ) ) ?;
300
+ self . entries . insert ( vm, & key, set_value. clone ( ) ) ?;
303
301
Ok ( set_value)
304
302
}
305
303
}
@@ -329,7 +327,7 @@ impl PyDictRef {
329
327
default : OptionalArg < PyObjectRef > ,
330
328
vm : & VirtualMachine ,
331
329
) -> PyResult {
332
- match self . entries . borrow_mut ( ) . pop ( vm, & key) ? {
330
+ match self . entries . pop ( vm, & key) ? {
333
331
Some ( value) => Ok ( value) ,
334
332
None => match default {
335
333
OptionalArg :: Present ( default) => Ok ( default) ,
@@ -340,8 +338,7 @@ impl PyDictRef {
340
338
341
339
#[ pymethod]
342
340
fn popitem ( self , vm : & VirtualMachine ) -> PyResult {
343
- let mut entries = self . entries . borrow_mut ( ) ;
344
- if let Some ( ( key, value) ) = entries. pop_front ( ) {
341
+ if let Some ( ( key, value) ) = self . entries . pop_front ( ) {
345
342
Ok ( vm. ctx . new_tuple ( vec ! [ key, value] ) )
346
343
} else {
347
344
let err_msg = vm. new_str ( "popitem(): dictionary is empty" . to_owned ( ) ) ;
@@ -360,14 +357,13 @@ impl PyDictRef {
360
357
}
361
358
362
359
pub fn from_attributes ( attrs : PyAttributes , vm : & VirtualMachine ) -> PyResult < Self > {
363
- let mut dict = DictContentType :: default ( ) ;
360
+ let dict = DictContentType :: default ( ) ;
364
361
365
362
for ( key, value) in attrs {
366
363
dict. insert ( vm, & vm. ctx . new_str ( key) , value) ?;
367
364
}
368
365
369
- let entries = RefCell :: new ( dict) ;
370
- Ok ( PyDict { entries } . into_ref ( vm) )
366
+ Ok ( PyDict { entries : dict } . into_ref ( vm) )
371
367
}
372
368
373
369
#[ pymethod( magic) ]
@@ -377,11 +373,11 @@ impl PyDictRef {
377
373
378
374
pub fn contains_key < T : IntoPyObject > ( & self , key : T , vm : & VirtualMachine ) -> bool {
379
375
let key = key. into_pyobject ( vm) . unwrap ( ) ;
380
- self . entries . borrow ( ) . contains ( vm, & key) . unwrap ( )
376
+ self . entries . contains ( vm, & key) . unwrap ( )
381
377
}
382
378
383
379
pub fn size ( & self ) -> dictdatatype:: DictSize {
384
- self . entries . borrow ( ) . size ( )
380
+ self . entries . size ( )
385
381
}
386
382
387
383
/// This function can be used to get an item without raising the
@@ -487,7 +483,7 @@ impl Iterator for DictIter {
487
483
type Item = ( PyObjectRef , PyObjectRef ) ;
488
484
489
485
fn next ( & mut self ) -> Option < Self :: Item > {
490
- match self . dict . entries . borrow ( ) . next_entry ( & mut self . position ) {
486
+ match self . dict . entries . next_entry ( & mut self . position ) {
491
487
Some ( ( key, value) ) => Some ( ( key, value) ) ,
492
488
None => None ,
493
489
}
@@ -563,13 +559,12 @@ macro_rules! dict_iterator {
563
559
#[ allow( clippy:: redundant_closure_call) ]
564
560
fn next( & self , vm: & VirtualMachine ) -> PyResult {
565
561
let mut position = self . position. get( ) ;
566
- let dict = self . dict. entries. borrow( ) ;
567
- if dict. has_changed_size( & self . size) {
562
+ if self . dict. entries. has_changed_size( & self . size) {
568
563
return Err (
569
564
vm. new_runtime_error( "dictionary changed size during iteration" . to_owned( ) )
570
565
) ;
571
566
}
572
- match dict. next_entry( & mut position) {
567
+ match self . dict. entries . next_entry( & mut position) {
573
568
Some ( ( key, value) ) => {
574
569
self . position. set( position) ;
575
570
Ok ( $result_fn( vm, key, value) )
@@ -585,10 +580,7 @@ macro_rules! dict_iterator {
585
580
586
581
#[ pymethod( name = "__length_hint__" ) ]
587
582
fn length_hint( & self ) -> usize {
588
- self . dict
589
- . entries
590
- . borrow( )
591
- . len_from_entry_index( self . position. get( ) )
583
+ self . dict. entries. len_from_entry_index( self . position. get( ) )
592
584
}
593
585
}
594
586
0 commit comments