- fn write_template_constructor<W: std::io::Write>(&mut self, w: &mut W, container_type: &str, mangled_container: &str, args: &Vec<&syn::Type>, generics: Option<&GenericTypes>, is_ref: bool) {
- if container_type == "Result" {
- assert_eq!(args.len(), 2);
- macro_rules! write_fn {
- ($call: expr) => { {
- writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_{}() -> {} {{", mangled_container, $call, mangled_container).unwrap();
- writeln!(w, "\t{}::CResultTempl::{}(0)\n}}\n", Self::container_templ_path(), $call).unwrap();
- } }
- }
- macro_rules! write_alias {
- ($call: expr, $item: expr) => { {
- write!(w, "#[no_mangle]\npub static {}_{}: extern \"C\" fn (", mangled_container, $call).unwrap();
- if let syn::Type::Path(syn::TypePath { path, .. }) = $item {
- let resolved = self.resolve_path(path, generics);
- if self.is_known_container(&resolved, is_ref) || self.is_transparent_container(&resolved, is_ref) {
- self.write_c_mangled_container_path_intern(w, Self::path_to_generic_args(path), generics,
- &format!("{}", single_ident_generic_path_to_ident(path).unwrap()), is_ref, false, false, false);
- } else {
- self.write_template_generics(w, &mut [$item].iter().map(|t| *t), is_ref, true);
- }
- } else if let syn::Type::Tuple(syn::TypeTuple { elems, .. }) = $item {
- self.write_c_mangled_container_path_intern(w, elems.iter().collect(), generics,
- &format!("{}Tuple", elems.len()), is_ref, false, false, false);
- } else { unimplemented!(); }
- write!(w, ") -> {} =\n\t{}::CResultTempl::<", mangled_container, Self::container_templ_path()).unwrap();
- self.write_template_generics(w, &mut args.iter().map(|t| *t), is_ref, true);
- writeln!(w, ">::{};\n", $call).unwrap();
- } }
- }
- match args[0] {
- syn::Type::Tuple(t) if t.elems.is_empty() => write_fn!("ok"),
- _ => write_alias!("ok", args[0]),
- }
- match args[1] {
- syn::Type::Tuple(t) if t.elems.is_empty() => write_fn!("err"),
- _ => write_alias!("err", args[1]),
- }
- } else if container_type.ends_with("Tuple") {
- write!(w, "#[no_mangle]\npub extern \"C\" fn {}_new(", mangled_container).unwrap();
- for (idx, gen) in args.iter().enumerate() {
- write!(w, "{}{}: ", if idx != 0 { ", " } else { "" }, ('a' as u8 + idx as u8) as char).unwrap();
- assert!(self.write_c_type_intern(w, gen, None, false, false, false));
- }
- writeln!(w, ") -> {} {{", mangled_container).unwrap();
- writeln!(w, "\t{} {{", mangled_container).unwrap();
- for idx in 0..args.len() {
- writeln!(w, "\t\t{}: Box::into_raw(Box::new({})),", ('a' as u8 + idx as u8) as char, ('a' as u8 + idx as u8) as char).unwrap();
- }
- writeln!(w, "\t}}\n}}\n").unwrap();
- } else {
- writeln!(w, "").unwrap();
- }
- }
-
- fn write_template_generics<'b, W: std::io::Write>(&self, w: &mut W, args: &mut dyn Iterator<Item=&'b syn::Type>, is_ref: bool, in_crate: bool) {