Skip to content

Commit a262fc5

Browse files
committed
more cleanps
1 parent 3618668 commit a262fc5

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

c-bindings-gen/src/main.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn maybe_print_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics,
244244
}
245245
}
246246

247-
fn println_trait<'a, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, module_path: &str, types: &mut TypeResolver<'a>) {
247+
fn println_trait<'a, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, types: &mut TypeResolver<'a>) {
248248
let trait_name = format!("{}", t.ident);
249249
match export_status(&t.attrs) {
250250
ExportStatus::Export => {},
@@ -354,7 +354,7 @@ fn println_trait<'a, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, module
354354
write!(w, "\t\tunimplemented!()\n\t}}\n}}\n").unwrap();
355355
}
356356
);
357-
write!(w, "\nuse {}::{} as ln{};\n", module_path, t.ident, trait_name).unwrap();
357+
write!(w, "\nuse {}::{}::{} as ln{};\n", types.orig_crate, types.module_path, t.ident, trait_name).unwrap();
358358
write!(w, "impl ln{}", t.ident).unwrap();
359359
maybe_print_generics(w, &t.generics, types);
360360
write!(w, " for {} {{\n", trait_name).unwrap();
@@ -444,11 +444,11 @@ fn println_trait<'a, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, module
444444
types.trait_declared(&t.ident, t);
445445
}
446446

447-
fn println_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name: &str, generics: &syn::Generics, attrs: &[syn::Attribute], module_path: &str, types: &TypeResolver, extra_headers: &mut File) {
447+
fn println_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name: &str, generics: &syn::Generics, attrs: &[syn::Attribute], types: &TypeResolver, extra_headers: &mut File) {
448448
// If we directly read the original type by its original name, cbindgen hits
449449
// https://github.com/eqrion/cbindgen/issues/286 Thus, instead, we import it as a temporary
450450
// name and then reference it by that name, which works around the issue.
451-
write!(w, "\nuse {}::{} as ln{}Import;\ntype ln{} = ln{}Import", module_path, ident, ident, ident, ident).unwrap();
451+
write!(w, "\nuse {}::{}::{} as ln{}Import;\ntype ln{} = ln{}Import", types.orig_crate, types.module_path, ident, ident, ident, ident).unwrap();
452452
maybe_print_generics(w, &generics, &types);
453453
write!(w, ";\n\n").unwrap();
454454
write!(extra_headers, "struct ln{}Opaque;\ntypedef struct ln{}Opaque LDKln{};\n", ident, ident, ident).unwrap();
@@ -458,7 +458,7 @@ fn println_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
458458
write!(w, "\tlet _ = unsafe {{ Box::from_raw(this_ptr.inner as *mut ln{}) }};\n}}\n", struct_name).unwrap();
459459
}
460460

461-
fn println_struct<W: std::io::Write>(w: &mut W, s: &syn::ItemStruct, module_path: &str, types: &mut TypeResolver, extra_headers: &mut File) {
461+
fn println_struct<W: std::io::Write>(w: &mut W, s: &syn::ItemStruct, types: &mut TypeResolver, extra_headers: &mut File) {
462462
let struct_name = &format!("{}", s.ident);
463463
let export = export_status(&s.attrs);
464464
match export {
@@ -472,7 +472,7 @@ fn println_struct<W: std::io::Write>(w: &mut W, s: &syn::ItemStruct, module_path
472472
return;
473473
}
474474

475-
println_opaque(w, &s.ident, struct_name, &s.generics, &s.attrs, module_path, types, extra_headers);
475+
println_opaque(w, &s.ident, struct_name, &s.generics, &s.attrs, types, extra_headers);
476476

477477
eprintln!("exporting fields for {}", struct_name);
478478
if let syn::Fields::Named(fields) = &s.fields {
@@ -616,7 +616,7 @@ eprintln!("WIP: IMPL {:?} FOR {}", trait_path.1, ident);
616616
}
617617
}
618618

619-
fn println_enum<W: std::io::Write>(w: &mut W, e: &syn::ItemEnum, module_path: &str, types: &mut TypeResolver, extra_headers: &mut File) {
619+
fn println_enum<W: std::io::Write>(w: &mut W, e: &syn::ItemEnum, types: &mut TypeResolver, extra_headers: &mut File) {
620620
match export_status(&e.attrs) {
621621
ExportStatus::Export => {},
622622
ExportStatus::NoExport|ExportStatus::TestOnly => return,
@@ -625,7 +625,7 @@ fn println_enum<W: std::io::Write>(w: &mut W, e: &syn::ItemEnum, module_path: &s
625625
for var in e.variants.iter() {
626626
if let syn::Fields::Unit = var.fields {} else {
627627
eprintln!("Skipping enum {} as it contains non-unit fields", e.ident);
628-
println_opaque(w, &e.ident, &format!("{}", e.ident), &e.generics, &e.attrs, module_path, types, extra_headers);
628+
println_opaque(w, &e.ident, &format!("{}", e.ident), &e.generics, &e.attrs, types, extra_headers);
629629
types.enum_ignored(&e.ident);
630630
return;
631631
}
@@ -644,7 +644,7 @@ fn println_enum<W: std::io::Write>(w: &mut W, e: &syn::ItemEnum, module_path: &s
644644
if var.discriminant.is_some() { unimplemented!(); }
645645
write!(w, "\t{},\n", var.ident).unwrap();
646646
}
647-
write!(w, "}}\nuse {}::{} as ln{};\nimpl {} {{\n", module_path, e.ident, e.ident, e.ident).unwrap();
647+
write!(w, "}}\nuse {}::{}::{} as ln{};\nimpl {} {{\n", types.orig_crate, types.module_path, e.ident, e.ident, e.ident).unwrap();
648648
write!(w, "\t#[allow(unused)]\n\tpub(crate) fn to_ln(&self) -> ln{} {{\n\t\tmatch self {{\n", e.ident).unwrap();
649649
for var in e.variants.iter() {
650650
write!(w, "\t\t\t{}::{} => ln{}::{},\n", e.ident, var.ident, e.ident, var.ident).unwrap();
@@ -695,8 +695,7 @@ fn convert_file(path: &str, out_path: &str, orig_crate: &str, module: &str, head
695695
assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
696696
println_docs(&mut out, &syntax.attrs, "");
697697

698-
let mut type_resolver = TypeResolver::new(module);
699-
let orig_module = orig_crate.to_string() + "::" + module;
698+
let mut type_resolver = TypeResolver::new(orig_crate, module, crate_types);
700699

701700
if path.ends_with("lib.rs") {
702701
write!(out, "#![allow(non_camel_case_types)]\n").unwrap();
@@ -715,20 +714,20 @@ fn convert_file(path: &str, out_path: &str, orig_crate: &str, module: &str, head
715714
syn::Item::Static(_) => {},
716715
syn::Item::Enum(e) => {
717716
if let syn::Visibility::Public(_) = e.vis {
718-
println_enum(&mut out, &e, &orig_module, &mut type_resolver, header_file);
717+
println_enum(&mut out, &e, &mut type_resolver, header_file);
719718
}
720719
},
721720
syn::Item::Impl(i) => {
722721
println_impl(&mut out, &i, &type_resolver);
723722
},
724723
syn::Item::Struct(s) => {
725724
if let syn::Visibility::Public(_) = s.vis {
726-
println_struct(&mut out, &s, &orig_module, &mut type_resolver, header_file);
725+
println_struct(&mut out, &s, &mut type_resolver, header_file);
727726
}
728727
},
729728
syn::Item::Trait(t) => {
730729
if let syn::Visibility::Public(_) = t.vis {
731-
println_trait(&mut out, &t, &orig_module, &mut type_resolver);
730+
println_trait(&mut out, &t, &mut type_resolver);
732731
}
733732
},
734733
syn::Item::Mod(m) => {
@@ -769,7 +768,7 @@ fn convert_file(path: &str, out_path: &str, orig_crate: &str, module: &str, head
769768
ExportStatus::NoExport|ExportStatus::TestOnly => continue,
770769
}
771770
if t.generics.lt_token.is_none() {
772-
println_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &orig_module, &type_resolver, header_file);
771+
println_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &type_resolver, header_file);
773772
}
774773
}
775774
},

c-bindings-gen/src/types.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,22 @@ pub enum DeclType<'a> {
168168
EnumIgnored,
169169
}
170170

171+
pub struct CrateTypes<'a> {
172+
pub traits: HashMap<String, &'a syn::ItemTrait>,
173+
pub trait_impls: HashMap<String, Vec<&'a syn::Ident>>,
174+
}
175+
171176
pub struct TypeResolver<'a> {
172-
module_path: &'a str,
177+
pub orig_crate: &'a str,
178+
pub module_path: &'a str,
173179
imports: HashMap<syn::Ident, String>,
174180
// ident -> is-mirrored-enum
175181
declared: HashMap<syn::Ident, DeclType<'a>>,
182+
pub crate_types: &'a CrateTypes<'a>,
176183
}
177184

178185
impl<'a> TypeResolver<'a> {
179-
pub fn new(module_path: &'a str) -> Self {
186+
pub fn new(orig_crate: &'a str, module_path: &'a str, crate_types: &'a CrateTypes<'a>) -> Self {
180187
let mut imports = HashMap::new();
181188
// Add primitives to the "imports" list:
182189
imports.insert(syn::Ident::new("bool", Span::call_site()), "bool".to_string());
@@ -190,7 +197,7 @@ impl<'a> TypeResolver<'a> {
190197
// have C mappings:
191198
imports.insert(syn::Ident::new("Result", Span::call_site()), "Result".to_string());
192199
imports.insert(syn::Ident::new("Option", Span::call_site()), "Option".to_string());
193-
Self { module_path, imports, declared: HashMap::new() }
200+
Self { orig_crate, module_path, imports, declared: HashMap::new(), crate_types }
194201
}
195202

196203
// *** Well know type definitions ***

0 commit comments

Comments
 (0)