8000 Switch from HashMap/Set to {BTree,Index}Map/Set in derive (for where … · RustPython/RustPython@a710e65 · GitHub
[go: up one dir, main page]

Skip to content

Commit a710e65

Browse files
committed
Switch from HashMap/Set to {BTree,Index}Map/Set in derive (for where it affects output order)
1 parent 9bd7f18 commit a710e65

File tree

6 files changed

+18
-24
lines changed

6 files changed

+18
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

derive-impl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ rustpython-doc = { workspace = true }
1616
itertools = { workspace = true }
1717
syn = { workspace = true, features = ["full", "extra-traits"] }
1818

19-
maplit = "1.0.2"
2019
proc-macro2 = { workspace = true }
2120
quote = { workspace = true }
2221
syn-ext = { version = "0.5.0", features = ["full"] }
2322
textwrap = { version = "0.16.1", default-features = false }
23+
indexmap = { workspace = true }
2424

2525
[lints]
2626
workspace = true

derive-impl/src/compile_bytecode.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use quote::quote;
1919
use rustpython_compiler_core::{Mode, bytecode::CodeObject, frozen};
2020
use std::sync::LazyLock;
2121
use std::{
22-
collections::HashMap,
22+
collections::BTreeMap,
2323
env, fs,
2424
path::{Path, PathBuf},
2525
};
@@ -83,20 +83,21 @@ impl CompilationSource {
8383
mode: Mode,
8484
module_name: String,
8585
compiler: &dyn Compiler,
86-
) -> Result<HashMap<String, CompiledModule>, Diagnostic> {
86+
) -> Result<BTreeMap<String, CompiledModule>, Diagnostic> {
8787
match &self.kind {
8888
CompilationSourceKind::Dir(rel_path) => self.compile_dir(
8989
&CARGO_MANIFEST_DIR.join(rel_path),
9090
String::new(),
9191
mode,
9292
compiler,
9393
),
94-
_ => Ok(hashmap! {
95-
module_name.clone() => CompiledModule {
94+
_ => Ok(BTreeMap::from([(
95+
module_name.clone(),
96+
CompiledModule {
9697
code: self.compile_single(mode, module_name, compiler)?,
9798
package: false,
9899
},
99-
}),
100+
)])),
100101
}
101102
}
102103

@@ -136,8 +137,8 @@ impl CompilationSource {
136137
parent: String,
137138
mode: Mode,
138139
compiler: &dyn Compiler,
139-
) -> Result<HashMap<String, CompiledModule>, Diagnostic> {
140-
let mut code_map = HashMap::new();
140+
) -> Result<BTreeMap<String, CompiledModule>, Diagnostic> {
141+
let mut code_map = BTreeMap::new();
141142
let paths = fs::read_dir(path)
142143
.or_else(|e| {
143144
if cfg!(windows) {

derive-impl/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
extern crate proc_macro;
66

7-
#[macro_use]
8-
extern crate maplit;
9-
107
#[macro_use]
118
mod error;
129
#[macro_use]

derive-impl/src/pyclass.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::util::{
44
ItemMeta, ItemMetaInner, ItemNursery, SimpleItemMeta, format_doc, pyclass_ident_and_attrs,
55
pyexception_ident_and_attrs, text_signature,
66
};
7+
use indexmap::IndexMap;
78
use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
89
use quote::{ToTokens, quote, quote_spanned};
9-
use std::collections::{HashMap, HashSet};
10+
use std::collections::{BTreeMap, HashMap, HashSet};
1011
use std::str::FromStr;
1112
use syn::{Attribute, Ident, Item, Result, parse_quote, spanned::Spanned};
1213
use syn_ext::ext::*;
@@ -1028,7 +1029,7 @@ impl ToTokens for MethodNursery {
10281029
#[derive(Default)]
10291030
#[allow(clippy::type_complexity)]
10301031
struct GetSetNursery {
1031-
map: HashMap<(String, Vec<Attribute>), (Option<Ident>, Option<Ident>, Option<Ident>)>,
1032+
map: IndexMap<(String, Vec<Attribute>), (Option<Ident>, Option<Ident>, Option<Ident>)>,
10321033
validated: bool,
10331034
}
10341035

@@ -1121,7 +1122,7 @@ impl ToTokens for GetSetNursery {
11211122
#[derive(Default)]
11221123
#[allow(clippy::type_complexity)]
11231124
struct MemberNursery {
1124-
map: HashMap<(String, MemberKind), (Option<Ident>, Option<Ident>)>,
1125+
map: BTreeMap<(String, MemberKind), (Option<Ident>, Option<Ident>)>,
11251126
validated: bool,
11261127
}
11271128

@@ -1130,7 +1131,7 @@ enum MemberItemKind {
11301131
Set,
11311132
}
11321133

1133-
#[derive(Eq, PartialEq, Hash)]
1134+
#[derive(Eq, PartialEq, Ord, PartialOrd)]
11341135
enum MemberKind {
11351136
Bool,
11361137
ObjectEx,

derive-impl/src/pymodule.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::util::{
66
};
77
use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
88
use quote::{ToTokens, quote, quote_spanned};
9-
use std::{collections::HashSet, str::FromStr};
9+
use std::collections::{BTreeSet, HashSet};
10+
use std::str::FromStr;
1011
use syn::{Attribute, Ident, Item, Result, parse_quote, spanned::Spanned};
1112
use syn_ext::ext::*;
1213
use syn_ext::types::PunctuatedNestedMeta;
@@ -472,7 +473,7 @@ impl ModuleItem for FunctionItem {
472473
if self.py_attrs.is_empty() {
473474
vec![py_name]
474475
} else {
475-
let mut py_names = HashSet::new();
476+
let mut py_names = BTreeSet::new();
476477
py_names.insert(py_name);
477478
for attr_index in self.py_attrs.iter().rev() {
478479
let mut loop_unit = || {

0 commit comments

Comments
 (0)
0