Skip to content

Commit a372346

Browse files
committed
Don't create an intermediate map for rustpython_stdlib::stdlib_inits
1 parent 8bfc904 commit a372346

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,7 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
304304
fn add_stdlib(vm: &mut VirtualMachine) {
305305
let _ = vm;
306306
#[cfg(feature = "stdlib")]
307-
{
308-
let stdlib = rustpython_stdlib::get_module_inits();
309-
for (name, init) in stdlib.into_iter() {
310-
vm.add_native_module(name, init);
311-
}
312-
}
307+
vm.add_native_modules(rustpython_stdlib::get_module_inits());
313308
}
314309

315310
/// Create settings by examining command line arguments and environment

stdlib/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,23 @@ mod termios;
5252
use rustpython_common as common;
5353
use rustpython_vm as vm;
5454

55-
use crate::vm::{
56-
builtins,
57-
stdlib::{StdlibInitFunc, StdlibMap},
58-
};
55+
use crate::vm::{builtins, stdlib::StdlibInitFunc};
5956
use std::borrow::Cow;
6057

61-
pub fn get_module_inits() -> StdlibMap {
58+
pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInitFunc)> {
6259
macro_rules! modules {
6360
{
6461
$(
6562
#[cfg($cfg:meta)]
6663
{ $( $key:expr => $val:expr),* $(,)? }
6764
)*
6865
} => {{
69-
let modules = [
66+
[
7067
$(
7168
$(#[cfg($cfg)] (Cow::<'static, str>::from($key), Box::new($val) as StdlibInitFunc),)*
7269
)*
73-
];
74-
modules.into_iter().collect()
70+
]
71+
.into_iter()
7572
}};
7673
}
7774
modules! {

vm/src/vm.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,24 @@ impl VirtualMachine {
386386
self.initialized = true;
387387
}
388388

389+
fn state_mut(&mut self) -> &mut PyGlobalState {
390+
PyRc::get_mut(&mut self.state)
391+
.expect("there should not be multiple threads while a user has a mut ref to a vm")
392+
}
393+
389394
/// Can only be used in the initialization closure passed to [`Interpreter::new_with_init`]
390395
pub fn add_native_module<S>(&mut self, name: S, module: stdlib::StdlibInitFunc)
391396
where
392397
S: Into<Cow<'static, str>>,
393398
{
394-
let state = PyRc::get_mut(&mut self.state)
395-
.expect("can't add_native_module when there are multiple threads");
396-
state.module_inits.insert(name.into(), module);
399+
self.state_mut().module_inits.insert(name.into(), module);
400+
}
401+
402+
pub fn add_native_modules<I>(&mut self, iter: I)
403+
where
404+
I: IntoIterator<Item = (Cow<'static, str>, stdlib::StdlibInitFunc)>,
405+
{
406+
self.state_mut().module_inits.extend(iter);
397407
}
398408

399409
/// Can only be used in the initialization closure passed to [`Interpreter::new_with_init`]
@@ -402,9 +412,7 @@ impl VirtualMachine {
402412
I: IntoIterator<Item = (String, bytecode::FrozenModule)>,
403413
{
404414
let frozen = frozen::map_frozen(self, frozen).collect::<Vec<_>>();
405-
let state = PyRc::get_mut(&mut self.state)
406-
.expect("can't add_frozen when there are multiple threads");
407-
state.frozen.extend(frozen);
415+
self.state_mut().frozen.extend(frozen);
408416
}
409417

410418
/// Start a new thread with access to the same interpreter.

0 commit comments

Comments
 (0)