impl `DerefMut` for all traits
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index 0a1d22a119aa9ea9a66aeb0984ae67877b9feda3..8876583315246524ffb938df804234b9a2784d86 100644 (file)
@@ -36,6 +36,7 @@ use blocks::*;
 
 const DEFAULT_IMPORTS: &'static str = "
 use alloc::str::FromStr;
+use alloc::string::String;
 use core::ffi::c_void;
 use core::convert::Infallible;
 use bitcoin::hashes::Hash;
@@ -73,7 +74,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                        let mut for_obj_vec = Vec::new();
                        types.write_c_type(&mut for_obj_vec, for_ty, Some(generics), false);
                        full_obj_path = String::from_utf8(for_obj_vec).unwrap();
-                       assert!(full_obj_path.starts_with(TypeResolver::generated_container_path()));
+                       if !full_obj_path.starts_with(TypeResolver::generated_container_path()) { return; }
                        for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into();
                }
 
@@ -533,8 +534,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                                                                        writeln!(w, "\ttype {} = crate::{};", t.ident, $type_resolver.resolve_path(&tr.path, Some(&gen_types))).unwrap();
                                                                        for bound in bounds_iter {
                                                                                if let syn::TypeParamBound::Trait(t) = bound {
-                                                                                       // We only allow for `?Sized` here.
-                                                                                       if let syn::TraitBoundModifier::Maybe(_) = t.modifier {} else { panic!(); }
+                                                                                       // We only allow for `Sized` here.
                                                                                        assert_eq!(t.path.segments.len(), 1);
                                                                                        assert_eq!(format!("{}", t.path.segments[0].ident), "Sized");
                                                                                }
@@ -647,6 +647,8 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                writeln!(w, "// directly as a Deref trait in higher-level structs:").unwrap();
                writeln!(w, "impl core::ops::Deref for {} {{\n\ttype Target = Self;", trait_name).unwrap();
                writeln!(w, "\tfn deref(&self) -> &Self {{\n\t\tself\n\t}}\n}}").unwrap();
+               writeln!(w, "impl core::ops::DerefMut for {} {{", trait_name).unwrap();
+               writeln!(w, "\tfn deref_mut(&mut self) -> &mut Self {{\n\t\tself\n\t}}\n}}").unwrap();
        }
 
        writeln!(w, "/// Calls the free function if one is set").unwrap();
@@ -945,9 +947,11 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, w_uses: &mut HashSet<String, NonRa
                                if i.defaultness.is_some() || i.unsafety.is_some() { unimplemented!(); }
                                if let Some(trait_path) = i.trait_.as_ref() {
                                        if trait_path.0.is_some() { unimplemented!(); }
-                                       if types.understood_c_path(&trait_path.1) {
-                                               let full_trait_path = types.resolve_path(&trait_path.1, None);
-                                               let trait_obj = *types.crate_types.traits.get(&full_trait_path).unwrap();
+                                       let full_trait_path_opt = types.maybe_resolve_path(&trait_path.1, None);
+                                       let trait_obj_opt = full_trait_path_opt.as_ref().and_then(|path| types.crate_types.traits.get(path));
+                                       if types.understood_c_path(&trait_path.1) && trait_obj_opt.is_some() {
+                                               let full_trait_path = full_trait_path_opt.unwrap();
+                                               let trait_obj = *trait_obj_opt.unwrap();
 
                                                let supertrait_name;
                                                let supertrait_resolver;