Skip to content

Commit 59884ef

Browse files
committed
Change locals to Vec
1 parent 630fbd9 commit 59884ef

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

vm/src/scope.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::LinkedList;
21
use std::fmt;
32

43
use crate::obj::objdict::PyDictRef;
@@ -11,7 +10,7 @@ use crate::vm::VirtualMachine;
1110
*/
1211
#[derive(Clone)]
1312
pub struct Scope {
14-
locals: LinkedList<PyDictRef>,
13+
locals: Vec<PyDictRef>,
1514
pub globals: PyDictRef,
1615
}
1716

@@ -24,14 +23,11 @@ impl fmt::Debug for Scope {
2423

2524
impl Scope {
2625
pub fn new(locals: Option<PyDictRef>, globals: PyDictRef, vm: &VirtualMachine) -> Scope {
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,
26+
let locals = match locals {
27+
Some(dict) => vec![dict],
28+
None => vec![],
3429
};
30+
let scope = Scope { locals, globals };
3531
scope.store_name(vm, "__annotations__", vm.ctx.new_dict().into_object());
3632
scope
3733
}
@@ -51,19 +47,20 @@ impl Scope {
5147
}
5248

5349
pub fn get_locals(&self) -> PyDictRef {
54-
match self.locals.front() {
50+
match self.locals.first() {
5551
Some(dict) => dict.clone(),
5652
None => self.globals.clone(),
5753
}
5854
}
5955

6056
pub fn get_only_locals(&self) -> Option<PyDictRef> {
61-
self.locals.front().cloned()
57+
self.locals.first().cloned()
6258
}
6359

6460
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);
61+
let mut new_locals = Vec::with_capacity(self.locals.len() + 1);
62+
new_locals.push(locals);
63+
new_locals.append(&mut self.locals.clone());
6764
Scope {
6865
locals: new_locals,
6966
globals: self.globals.clone(),

0 commit comments

Comments
 (0)