Skip to content

Commit 8f4b7ad

Browse files
committed
Reuse indexmap crate instead of manually implementing OrderedMap.
This change seems desirable, because: * It allows dropping the `K: Copy` constraint. (This may be needed to unblock future, work-in-progress commits that may require dropping this impl/derive from `NamedImplKey`.) * It reduced the amount of code that has to be maintained within the `cxx` crates. I note that MSRV of `indexmap` is 1.63.0 [1] which is lower than MSRV of 1.73.0 advertised by the `cxx` crate [2]. [1] https://github.com/indexmap-rs/indexmap/blob/main/RELEASES.md#201-2023-09-27 [2] https://github.com/dtolnay/cxx/blob/b5b259f2517a003e4c05bb91cd2b346c6ee1c517/README.md?plain=1#L27-L28
1 parent bf9bf73 commit 8f4b7ad

16 files changed

+445
-56
lines changed

BUCK

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ rust_binary(
3333
deps = [
3434
"//third-party:clap",
3535
"//third-party:codespan-reporting",
36+
"//third-party:indexmap",
3637
"//third-party:proc-macro2",
3738
"//third-party:quote",
3839
"//third-party:syn",
@@ -57,6 +58,7 @@ rust_library(
5758
edition = "2021",
5859
proc_macro = True,
5960
deps = [
61+
"//third-party:indexmap",
6062
"//third-party:proc-macro2",
6163
"//third-party:quote",
6264
"//third-party:rustversion",
@@ -75,6 +77,7 @@ rust_library(
7577
deps = [
7678
"//third-party:cc",
7779
"//third-party:codespan-reporting",
80+
"//third-party:indexmap",
7881
"//third-party:proc-macro2",
7982
"//third-party:quote",
8083
"//third-party:scratch",
@@ -93,6 +96,7 @@ rust_library(
9396
deps = [
9497
"//third-party:cc",
9598
"//third-party:codespan-reporting",
99+
"//third-party:indexmap",
96100
"//third-party:proc-macro2",
97101
"//third-party:quote",
98102
"//third-party:syn",

BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ rust_binary(
3333
deps = [
3434
"@crates.io//:clap",
3535
"@crates.io//:codespan-reporting",
36+
"@crates.io//:indexmap",
3637
"@crates.io//:proc-macro2",
3738
"@crates.io//:quote",
3839
"@crates.io//:syn",
@@ -62,6 +63,7 @@ rust_proc_macro(
6263
"@crates.io//:rustversion",
6364
],
6465
deps = [
66+
"@crates.io//:indexmap",
6567
"@crates.io//:proc-macro2",
6668
"@crates.io//:quote",
6769
"@crates.io//:syn",
@@ -76,6 +78,7 @@ rust_library(
7678
deps = [
7779
"@crates.io//:cc",
7880
"@crates.io//:codespan-reporting",
81+
"@crates.io//:indexmap",
7982
"@crates.io//:proc-macro2",
8083
"@crates.io//:quote",
8184
"@crates.io//:scratch",
@@ -92,6 +95,7 @@ rust_library(
9295
deps = [
9396
"@crates.io//:cc",
9497
"@crates.io//:codespan-reporting",
98+
"@crates.io//:indexmap",
9599
"@crates.io//:proc-macro2",
96100
"@crates.io//:quote",
97101
"@crates.io//:syn",

MODULE.bazel.lock

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

gen/build/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ experimental-async-fn = []
2121
[dependencies]
2222
cc = "1.0.83"
2323
codespan-reporting = "0.12"
24+
indexmap = "2.9.0"
2425
proc-macro2 = { version = "1.0.74", default-features = false, features = ["span-locations"] }
2526
quote = { version = "1.0.35", default-features = false }
2627
scratch = "1.0.5"

gen/cmd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ experimental-async-fn = []
2323
[dependencies]
2424
clap = { version = "4.3.11", default-features = false, features = ["error-context", "help", "std", "suggestions", "usage"] }
2525
codespan-reporting = "0.12"
26+
indexmap = "2.9.0"
2627
proc-macro2 = { version = "1.0.74", default-features = false, features = ["span-locations"] }
2728
quote = { version = "1.0.35", default-features = false }
2829
syn = { version = "2.0.46", default-features = false, features = ["clone-impls", "full", "parsing", "printing"] }

gen/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rust-version = "1.73"
1414

1515
[dependencies]
1616
codespan-reporting = "0.12"
17+
indexmap = "2.9.0"
1718
proc-macro2 = { version = "1.0.74", default-features = false, features = ["span-locations"] }
1819
quote = { version = "1.0.35", default-features = false }
1920
syn = { version = "2.0.46", default-features = false, features = ["clone-impls", "full", "parsing", "printing"] }

macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ experimental-async-fn = []
2121
experimental-enum-variants-from-header = ["clang-ast", "flate2", "memmap", "serde", "serde_derive", "serde_json"]
2222

2323
[dependencies]
24+
indexmap = "2.9.0"
2425
proc-macro2 = "1.0.74"
2526
quote = "1.0.35"
2627
rustversion = "1"

syntax/map.rs

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,48 @@
11
use std::borrow::Borrow;
22
use std::hash::Hash;
33
use std::ops::Index;
4-
use std::slice;
54

65
pub(crate) use self::ordered::OrderedMap;
76
pub(crate) use self::unordered::UnorderedMap;
87
pub(crate) use std::collections::hash_map::Entry;
98

109
mod ordered {
11-
use super::{Entry, Iter, UnorderedMap};
12-
use std::borrow::Borrow;
1310
use std::hash::Hash;
14-
use std::mem;
1511

16-
pub(crate) struct OrderedMap<K, V> {
17-
map: UnorderedMap<K, usize>,
18-
vec: Vec<(K, V)>,
19-
}
12+
pub(crate) struct OrderedMap<K, V>(indexmap::IndexMap<K, V>);
2013

2114
impl<K, V> OrderedMap<K, V> {
2215
pub(crate) fn new() -> Self {
23-
OrderedMap {
24-
map: UnorderedMap::new(),
25-
vec: Vec::new(),
26-
}
27-
}
28-
29-
pub(crate) fn iter(&self) -> Iter<K, V> {
30-
Iter(self.vec.iter())
16+
OrderedMap(indexmap::IndexMap::new())
3117
}
3218

3319
#[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro
3420
pub(crate) fn keys(&self) -> impl Iterator<Item = &K> {
35-
self.vec.iter().map(|(k, _v)| k)
21+
self.0.keys()
3622
}
3723
}
3824

3925
impl<K, V> OrderedMap<K, V>
4026
where
41-
K: Copy + Hash + Eq,
27+
K: Hash + Eq,
4228
{
4329
pub(crate) fn insert(&mut self, key: K, value: V) -> Option<V> {
44-
match self.map.entry(key) {
45-
Entry::Occupied(entry) => {
46-
let i = &mut self.vec[*entry.get()];
47-
Some(mem::replace(&mut i.1, value))
48-
}
49-
Entry::Vacant(entry) => {
50-
entry.insert(self.vec.len());
51-
self.vec.push((key, value));
52-
None
53-
}
54-
}
30+
self.0.insert(key, value)
5531
}
5632

5733
pub(crate) fn contains_key<Q>(&self, key: &Q) -> bool
5834
where
59-
K: Borrow<Q>,
60-
Q: ?Sized + Hash + Eq,
35+
Q: ?Sized + Hash + indexmap::Equivalent<K>,
6136
{
62-
self.map.contains_key(key)
37+
self.0.contains_key(key)
6338
}
6439
}
6540

6641
impl<'a, K, V> IntoIterator for &'a OrderedMap<K, V> {
6742
type Item = (&'a K, &'a V);
68-
type IntoIter = Iter<'a, K, V>;
43+
type IntoIter = indexmap::map::Iter<'a, K, V>;
6944
fn into_iter(self) -> Self::IntoIter {
70-
self.iter()
45+
self.0.iter()
7146
}
7247
}
7348
}
@@ -138,21 +113,6 @@ mod unordered {
138113
}
139114
}
140115

141-
pub(crate) struct Iter<'a, K, V>(slice::Iter<'a, (K, V)>);
142-
143-
impl<'a, K, V> Iterator for Iter<'a, K, V> {
144-
type Item = (&'a K, &'a V);
145-
146-
fn next(&mut self) -> Option<Self::Item> {
147-
let (k, v) = self.0.next()?;
148-
Some((k, v))
149-
}
150-
151-
fn size_hint(&self) -> (usize, Option<usize>) {
152-
self.0.size_hint()
153-
}
154-
}
155-
156116
impl<K, V> Default for UnorderedMap<K, V> {
157117
fn default() -> Self {
158118
UnorderedMap::new()

third-party/BUCK

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

third-party/Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)