Skip to content

Commit 9bcc4df

Browse files
committed
Try clone'ing the field before skipping exporting a field getter
1 parent 9c6d1f1 commit 9bcc4df

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

c-bindings-gen/src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,25 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
735735
write!(w, "inner_val").unwrap();
736736
types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
737737
writeln!(w, "\n}}").unwrap();
738+
} else {
739+
// If the type isn't reference-able, but is clonable, export a getter that just clones
740+
if types.understood_c_type(&$field.ty, Some(&gen_types)) {
741+
let mut v = Vec::new();
742+
types.write_c_type(&mut v, &$field.ty, Some(&gen_types), true);
743+
let s = String::from_utf8(v).unwrap();
744+
if types.is_clonable(&s) {
745+
writeln_arg_docs(w, &$field.attrs, "", types, Some(&gen_types), vec![].drain(..), Some(&$field.ty));
746+
writeln!(w, "///\n/// Returns a copy of the field.").unwrap();
747+
write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> {}", struct_name, $new_name, struct_name, s).unwrap();
748+
write!(w, " {{\n\tlet mut inner_val = this_ptr.get_native_mut_ref().{}.clone();\n\t", $real_name).unwrap();
749+
let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &$field.ty, Some(&gen_types), true);
750+
if local_var { write!(w, "\n\t").unwrap(); }
751+
types.write_to_c_conversion_inline_prefix(w, &$field.ty, Some(&gen_types), true);
752+
write!(w, "inner_val").unwrap();
753+
types.write_to_c_conversion_inline_suffix(w, &$field.ty, Some(&gen_types), true);
754+
writeln!(w, "\n}}").unwrap();
755+
}
756+
}
738757
}
739758
}
740759

0 commit comments

Comments
 (0)