pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, types: &TypeResolver, concrete_lifetimes: bool) {
let mut gen_types = GenericTypes::new(None);
assert!(gen_types.learn_generics(generics, types));
- if !generics.params.is_empty() {
- write!(w, "<").unwrap();
- for (idx, generic) in generics.params.iter().enumerate() {
- match generic {
- syn::GenericParam::Type(type_param) => {
- write!(w, "{}", if idx != 0 { ", " } else { "" }).unwrap();
- let type_ident = &type_param.ident;
- types.write_c_type_in_generic_param(w, &syn::parse_quote!(#type_ident), Some(&gen_types), false);
- },
- syn::GenericParam::Lifetime(lt) => {
- if concrete_lifetimes {
- write!(w, "'static").unwrap();
- } else {
- write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap();
+ if generics.params.is_empty() { return; }
+ for generic in generics.params.iter() {
+ match generic {
+ syn::GenericParam::Type(type_param) => {
+ for bound in type_param.bounds.iter() {
+ match bound {
+ syn::TypeParamBound::Trait(t) => {
+ if let Some(trait_bound) = types.maybe_resolve_path(&t.path, None) {
+ if types.skip_path(&trait_bound) {
+ // Just hope rust deduces generic params if some bounds are skipable.
+ return;
+ }
+ }
+ }
+ _ => {},
}
- },
- _ => unimplemented!(),
+ }
}
+ _ => {},
+ }
+ }
+
+ write!(w, "<").unwrap();
+ for (idx, generic) in generics.params.iter().enumerate() {
+ match generic {
+ syn::GenericParam::Type(type_param) => {
+ write!(w, "{}", if idx != 0 { ", " } else { "" }).unwrap();
+ let type_ident = &type_param.ident;
+ types.write_c_type_in_generic_param(w, &syn::parse_quote!(#type_ident), Some(&gen_types), false);
+ },
+ syn::GenericParam::Lifetime(lt) => {
+ if concrete_lifetimes {
+ write!(w, "'static").unwrap();
+ } else {
+ write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap();
+ }
+ },
+ _ => unimplemented!(),
}
- write!(w, ">").unwrap();
}
+ write!(w, ">").unwrap();
}
pub fn maybe_write_lifetime_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, types: &TypeResolver) {
writeln_fn_docs(w, &f.attrs, "", types, Some(&gen_types), f.sig.inputs.iter(), &f.sig.output);
write!(w, "#[no_mangle]\npub extern \"C\" fn {}(", f.sig.ident).unwrap();
+
+
write_method_params(w, &f.sig, "", types, Some(&gen_types), false, true);
write!(w, " {{\n\t").unwrap();
write_method_var_decl_body(w, &f.sig, "", types, Some(&gen_types), false);
- write!(w, "{}::{}(", types.module_path, f.sig.ident).unwrap();
+ write!(w, "{}::{}", types.module_path, f.sig.ident).unwrap();
+
+ let mut function_generic_args = Vec::new();
+ maybe_write_generics(&mut function_generic_args, &f.sig.generics, types, true);
+ if !function_generic_args.is_empty() {
+ write!(w, "::{}", String::from_utf8(function_generic_args).unwrap()).unwrap();
+ }
+ write!(w, "(").unwrap();
+
write_method_call_params(w, &f.sig, "", types, Some(&gen_types), "", false);
writeln!(w, "\n}}\n").unwrap();
}