Skip to content

Commit 4c6052d

Browse files
committed
Key impls by an enum rather than by Type
1 parent f4d9789 commit 4c6052d

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

gen/src/write.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -1336,11 +1336,15 @@ fn write_generic_instantiations(out: &mut OutFile) {
13361336
out.set_namespace(Default::default());
13371337
out.begin_block(Block::ExternC);
13381338
for ty in out.types {
1339+
let impl_key = match ty.impl_key() {
1340+
Some(impl_key) => impl_key,
1341+
None => continue,
1342+
};
13391343
if let Type::RustBox(ptr) = ty {
13401344
if let Type::Ident(inner) = &ptr.inner {
13411345
if Atom::from(&inner.rust).is_none()
13421346
&& (!out.types.aliases.contains_key(&inner.rust)
1343-
|| out.types.explicit_impls.contains(ty))
1347+
|| out.types.explicit_impls.contains_key(&impl_key))
13441348
{
13451349
out.next_section();
13461350
write_rust_box_extern(out, &out.types.resolve(&inner));
@@ -1350,7 +1354,7 @@ fn write_generic_instantiations(out: &mut OutFile) {
13501354
if let Type::Ident(inner) = &vec.inner {
13511355
if Atom::from(&inner.rust).is_none()
13521356
&& (!out.types.aliases.contains_key(&inner.rust)
1353-
|| out.types.explicit_impls.contains(ty))
1357+
|| out.types.explicit_impls.contains_key(&impl_key))
13541358
{
13551359
out.next_section();
13561360
write_rust_vec_extern(out, inner);
@@ -1360,7 +1364,7 @@ fn write_generic_instantiations(out: &mut OutFile) {
13601364
if let Type::Ident(inner) = &ptr.inner {
13611365
if Atom::from(&inner.rust).is_none()
13621366
&& (!out.types.aliases.contains_key(&inner.rust)
1363-
|| out.types.explicit_impls.contains(ty))
1367+
|| out.types.explicit_impls.contains_key(&impl_key))
13641368
{
13651369
out.next_section();
13661370
write_unique_ptr(out, inner);
@@ -1370,7 +1374,7 @@ fn write_generic_instantiations(out: &mut OutFile) {
13701374
if let Type::Ident(inner) = &ptr.inner {
13711375
if Atom::from(&inner.rust).is_none()
13721376
&& (!out.types.aliases.contains_key(&inner.rust)
1373-
|| out.types.explicit_impls.contains(ty))
1377+
|| out.types.explicit_impls.contains_key(&impl_key))
13741378
{
13751379
out.next_section();
13761380
write_shared_ptr(out, inner);
@@ -1380,7 +1384,7 @@ fn write_generic_instantiations(out: &mut OutFile) {
13801384
if let Type::Ident(inner) = &ptr.inner {
13811385
if Atom::from(&inner.rust).is_none()
13821386
&& (!out.types.aliases.contains_key(&inner.rust)
1383-
|| out.types.explicit_impls.contains(ty))
1387+
|| out.types.explicit_impls.contains_key(&impl_key))
13841388
{
13851389
out.next_section();
13861390
write_weak_ptr(out, inner);
@@ -1390,7 +1394,7 @@ fn write_generic_instantiations(out: &mut OutFile) {
13901394
if let Type::Ident(inner) = &vector.inner {
13911395
if Atom::from(&inner.rust).is_none()
13921396
&& (!out.types.aliases.contains_key(&inner.rust)
1393-
|| out.types.explicit_impls.contains(ty))
1397+
|| out.types.explicit_impls.contains_key(&impl_key))
13941398
{
13951399
out.next_section();
13961400
write_cxx_vector(out, inner);
@@ -1403,11 +1407,15 @@ fn write_generic_instantiations(out: &mut OutFile) {
14031407
out.begin_block(Block::Namespace("rust"));
14041408
out.begin_block(Block::InlineNamespace("cxxbridge1"));
14051409
for ty in out.types {
1410+
let impl_key = match ty.impl_key() {
1411+
Some(impl_key) => impl_key,
1412+
None => continue,
1413+
};
14061414
if let Type::RustBox(ptr) = ty {
14071415
if let Type::Ident(inner) = &ptr.inner {
14081416
if Atom::from(&inner.rust).is_none()
14091417
&& (!out.types.aliases.contains_key(&inner.rust)
1410-
|| out.types.explicit_impls.contains(ty))
1418+
|| out.types.explicit_impls.contains_key(&impl_key))
14111419
{
14121420
write_rust_box_impl(out, &out.types.resolve(&inner));
14131421
}
@@ -1416,7 +1424,7 @@ fn write_generic_instantiations(out: &mut OutFile) {
14161424
if let Type::Ident(inner) = &vec.inner {
14171425
if Atom::from(&inner.rust).is_none()
14181426
&& (!out.types.aliases.contains_key(&inner.rust)
1419-
|| out.types.explicit_impls.contains(ty))
1427+
|| out.types.explicit_impls.contains_key(&impl_key))
14201428
{
14211429
write_rust_vec_impl(out, inner);
14221430
}

macro/src/expand.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ fn expand(ffi: Module, doc: Doc, attrs: OtherAttrs, apis: &[Api], types: &Types)
8080
}
8181

8282
for ty in types {
83-
let explicit_impl = types.explicit_impls.get(ty);
83+
let impl_key = match ty.impl_key() {
84+
Some(impl_key) => impl_key,
85+
None => continue,
86+
};
87+
let explicit_impl = types.explicit_impls.get(&impl_key).copied();
8488
if let Type::RustBox(ty) = ty {
8589
if let Type::Ident(ident) = &ty.inner {
8690
if Atom::from(&ident.rust).is_none()
@@ -1366,7 +1370,6 @@ fn expand_weak_ptr(ident: &RustName, types: &Types, explicit_impl: Option<&Impl>
13661370
}
13671371

13681372
fn expand_cxx_vector(elem: &RustName, explicit_impl: Option<&Impl>, types: &Types) -> TokenStream {
1369-
let _ = explicit_impl;
13701373
let name = elem.rust.to_string();
13711374
let prefix = format!("cxxbridge1$std$vector${}$", elem.to_symbol(types));
13721375
let link_size = format!("{}size", prefix);

syntax/impls.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::syntax::{
22
Array, ExternFn, Impl, Include, Lifetimes, Receiver, Ref, Signature, SliceRef, Ty1, Type, Var,
33
};
4-
use std::borrow::Borrow;
54
use std::hash::{Hash, Hasher};
65
use std::mem;
76
use std::ops::{Deref, DerefMut};
@@ -447,9 +446,3 @@ impl PartialEq for Impl {
447446
negative == negative2 && ty == ty2
448447
}
449448
}
450-
451-
impl Borrow<Type> for &Impl {
452-
fn borrow(&self) -> &Type {
453-
&self.ty
454-
}
455-
}

syntax/instantiate.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::syntax::Type;
2+
use proc_macro2::Ident;
3+
4+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
5+
pub enum ImplKey<'a> {
6+
RustBox(&'a Ident),
7+
RustVec(&'a Ident),
8+
UniquePtr(&'a Ident),
9+
SharedPtr(&'a Ident),
10+
WeakPtr(&'a Ident),
11+
CxxVector(&'a Ident),
12+
}
13+
14+
impl Type {
15+
pub(crate) fn impl_key(&self) -> Option<ImplKey> {
16+
if let Type::RustBox(ty) = self {
17+
if let Type::Ident(ident) = &ty.inner {
18+
return Some(ImplKey::RustBox(&ident.rust));
19+
}
20+
} else if let Type::RustVec(ty) = self {
21+
if let Type::Ident(ident) = &ty.inner {
22+
return Some(ImplKey::RustVec(&ident.rust));
23+
}
24+
} else if let Type::UniquePtr(ty) = self {
25+
if let Type::Ident(ident) = &ty.inner {
26+
return Some(ImplKey::UniquePtr(&ident.rust));
27+
}
28+
} else if let Type::SharedPtr(ty) = self {
29+
if let Type::Ident(ident) = &ty.inner {
30+
return Some(ImplKey::SharedPtr(&ident.rust));
31+
}
32+
} else if let Type::WeakPtr(ty) = self {
33+
if let Type::Ident(ident) = &ty.inner {
34+
return Some(ImplKey::WeakPtr(&ident.rust));
35+
}
36+
} else if let Type::CxxVector(ty) = self {
37+
if let Type::Ident(ident) = &ty.inner {
38+
return Some(ImplKey::CxxVector(&ident.rust));
39+
}
40+
}
41+
None
42+
}
43+
}

syntax/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod file;
1111
pub mod ident;
1212
mod impls;
1313
mod improper;
14+
pub mod instantiate;
1415
pub mod mangle;
1516
mod names;
1617
pub mod namespace;

syntax/types.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::syntax::improper::ImproperCtype;
2+
use crate::syntax::instantiate::ImplKey;
23
use crate::syntax::report::Errors;
34
use crate::syntax::set::{OrderedSet as Set, UnorderedSet};
45
use crate::syntax::trivial::{self, TrivialReason};
@@ -18,7 +19,7 @@ pub struct Types<'a> {
1819
pub aliases: Map<&'a Ident, &'a TypeAlias>,
1920
pub untrusted: Map<&'a Ident, &'a ExternType>,
2021
pub required_trivial: Map<&'a Ident, Vec<TrivialReason<'a>>>,
21-
pub explicit_impls: Set<&'a Impl>,
22+
pub explicit_impls: Map<ImplKey<'a>, &'a Impl>,
2223
pub resolutions: Map<&'a Ident, &'a Pair>,
2324
pub struct_improper_ctypes: UnorderedSet<&'a Ident>,
2425
pub toposorted_structs: Vec<&'a Struct>,
@@ -33,7 +34,7 @@ impl<'a> Types<'a> {
3334
let mut rust = Set::new();
3435
let mut aliases = Map::new();
3536
let mut untrusted = Map::new();
36-
let mut explicit_impls = Set::new();
37+
let mut explicit_impls = Map::new();
3738
let mut resolutions = Map::new();
3839
let struct_improper_ctypes = UnorderedSet::new();
3940
let toposorted_structs = Vec::new();
@@ -160,7 +161,9 @@ impl<'a> Types<'a> {
160161
}
161162
Api::Impl(imp) => {
162163
visit(&mut all, &imp.ty);
163-
explicit_impls.insert(imp);
164+
if let Some(key) = imp.ty.impl_key() {
165+
explicit_impls.insert(key, imp);
166+
}
164167
}
165168
}
166169
}

0 commit comments

Comments
 (0)