Skip to content

Commit dfef21c

Browse files
committed
[bindings] Use common conv in _write impls, drop type restrictions
This expands the manual implementation logic for `*_write` and `*_read` methods to most types, converting the `*_write` path to the common type-conversion logic to ensure it works. Note that `*_write_void` is still only implemented for has-inner types, as its unclear what the `void*` would point to for others.
1 parent e36b514 commit dfef21c

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

c-bindings-gen/src/main.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,46 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
6363
if let Some(t) = types.maybe_resolve_path(&trait_path, None) {
6464
let for_obj;
6565
let full_obj_path;
66+
let mut has_inner = false;
6667
if let syn::Type::Path(ref p) = for_ty {
6768
if let Some(ident) = p.path.get_ident() {
68-
let s = types.maybe_resolve_ident(ident).unwrap();
69-
if !types.crate_types.opaques.get(&s).is_some() { return; }
70-
7169
for_obj = format!("{}", ident);
7270
full_obj_path = for_obj.clone();
71+
has_inner = types.c_type_has_inner_from_path(&types.resolve_path(&p.path, None));
7372
} else { return; }
7473
} else {
75-
return;
74+
// We assume that anything that isn't a Path is somehow a generic that ends up in our
75+
// derived-types module.
76+
let mut for_obj_vec = Vec::new();
77+
types.write_c_type(&mut for_obj_vec, for_ty, None, false);
78+
full_obj_path = String::from_utf8(for_obj_vec).unwrap();
79+
assert!(full_obj_path.starts_with(TypeResolver::generated_container_path()));
80+
for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into();
7681
}
7782

7883
match &t as &str {
7984
"util::ser::Writeable" => {
8085
writeln!(w, "#[no_mangle]").unwrap();
8186
writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap();
82-
writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap();
83-
writeln!(w, "}}").unwrap();
84-
writeln!(w, "#[no_mangle]").unwrap();
85-
writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
86-
writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap();
87+
88+
let ref_type = syn::Type::Reference(syn::TypeReference {
89+
and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None,
90+
elem: Box::new(for_ty.clone()) });
91+
assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, None));
92+
93+
write!(w, "\tcrate::c_types::serialize_obj(").unwrap();
94+
types.write_from_c_conversion_prefix(w, &ref_type, None);
95+
write!(w, "unsafe {{ &*obj }}").unwrap();
96+
types.write_from_c_conversion_suffix(w, &ref_type, None);
97+
writeln!(w, ")").unwrap();
98+
8799
writeln!(w, "}}").unwrap();
100+
if has_inner {
101+
writeln!(w, "#[no_mangle]").unwrap();
102+
writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
103+
writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap();
104+
writeln!(w, "}}").unwrap();
105+
}
88106
},
89107
"util::ser::Readable" => {
90108
// Create the Result<Object, DecodeError> syn::Type

c-bindings-gen/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
727727

728728
/// Returns the module path in the generated mapping crate to the containers which we generate
729729
/// when writing to CrateTypes::template_file.
730-
fn generated_container_path() -> &'static str {
730+
pub fn generated_container_path() -> &'static str {
731731
"crate::c_types::derived"
732732
}
733733
/// Returns the module path in the generated mapping crate to the container templates, which
@@ -914,7 +914,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
914914
self.declared.get(ident)
915915
}
916916
/// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }.
917-
fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{
917+
pub fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{
918918
self.crate_types.opaques.get(full_path).is_some()
919919
}
920920

0 commit comments

Comments
 (0)