1
+ use std:: collections:: LinkedList ;
1
2
use std:: fmt;
2
- use std:: rc:: Rc ;
3
3
4
4
use crate :: obj:: objdict:: PyDictRef ;
5
5
use crate :: pyobject:: { ItemProtocol , PyContext , PyObjectRef , PyResult } ;
@@ -9,58 +9,9 @@ use crate::vm::VirtualMachine;
9
9
* So a scope is a linked list of scopes.
10
10
* When a name is looked up, it is check in its scope.
11
11
*/
12
- #[ derive( Debug ) ]
13
- struct RcListNode < T > {
14
- elem : T ,
15
- next : Option < Rc < RcListNode < T > > > ,
16
- }
17
-
18
- #[ derive( Debug , Clone ) ]
19
- struct RcList < T > {
20
- head : Option < Rc < RcListNode < T > > > ,
21
- }
22
-
23
- struct Iter < ' a , T : ' a > {
24
- next : Option < & ' a RcListNode < T > > ,
25
- }
26
-
27
- impl < T > RcList < T > {
28
- pub fn new ( ) -> Self {
29
- RcList { head : None }
30
- }
31
-
32
- pub fn insert ( self , elem : T ) -> Self {
33
- RcList {
34
- head : Some ( Rc :: new ( RcListNode {
35
- elem,
36
- next : self . head ,
37
- } ) ) ,
38
- }
39
- }
40
-
41
- #[ cfg_attr( feature = "flame-it" , flame( "RcList" ) ) ]
42
- pub fn iter ( & self ) -> Iter < T > {
43
- Iter {
44
- next : self . head . as_ref ( ) . map ( |node| & * * node) ,
45
- }
46
- }
47
- }
48
-
49
- impl < ' a , T > Iterator for Iter < ' a , T > {
50
- type Item = & ' a T ;
51
-
52
- #[ cfg_attr( feature = "flame-it" , flame( "Iter" ) ) ]
53
- fn next ( & mut self ) -> Option < Self :: Item > {
54
- self . next . map ( |node| {
55
- self . next = node. next . as_ref ( ) . map ( |node| & * * node) ;
56
- & node. elem
57
- } )
58
- }
59
- }
60
-
61
12
#[ derive( Clone ) ]
62
13
pub struct Scope {
63
- locals : RcList < PyDictRef > ,
14
+ locals : LinkedList < PyDictRef > ,
64
15
pub globals : PyDictRef ,
65
16
}
66
17
@@ -73,11 +24,14 @@ impl fmt::Debug for Scope {
73
24
74
25
impl Scope {
75
26
pub fn new ( locals : Option < PyDictRef > , globals : PyDictRef , vm : & VirtualMachine ) -> Scope {
76
- let locals = match locals {
77
- Some ( dict) => RcList :: new ( ) . insert ( dict) ,
78
- None => RcList :: new ( ) ,
27
+ let mut locals_list = LinkedList :: new ( ) ;
28
+ if let Some ( dict) = locals {
29
+ locals_list. push_front ( dict)
30
+ }
31
+ let scope = Scope {
32
+ locals : locals_list,
33
+ globals,
79
34
} ;
80
- let scope = Scope { locals, globals } ;
81
35
scope. store_name ( vm, "__annotations__" , vm. ctx . new_dict ( ) . into_object ( ) ) ;
82
36
scope
83
37
}
@@ -97,19 +51,21 @@ impl Scope {
97
51
}
98
52
99
53
pub fn get_locals ( & self ) -> PyDictRef {
100
- match self . locals . iter ( ) . next ( ) {
54
+ match self . locals . front ( ) {
101
55
Some ( dict) => dict. clone ( ) ,
102
56
None => self . globals . clone ( ) ,
103
57
}
104
58
}
105
59
106
60
pub fn get_only_locals ( & self ) -> Option < PyDictRef > {
107
- self . locals . iter ( ) . next ( ) . cloned ( )
61
+ self . locals . front ( ) . cloned ( )
108
62
}
109
63
110
64
pub fn new_child_scope_with_locals ( & self , locals : PyDictRef ) -> Scope {
65
+ let mut new_locals = self . locals . clone ( ) ;
66
+ new_locals. push_front ( locals) ;
111
67
Scope {
112
- locals : self . locals . clone ( ) . insert ( locals ) ,
68
+ locals : new_locals ,
113
69
globals : self . globals . clone ( ) ,
114
70
}
115
71
}
0 commit comments