[bindings] Handle MessageSendEventsProvider impl blocks in a util fn
authorMatt Corallo <git@bluematt.me>
Wed, 25 Nov 2020 17:22:03 +0000 (12:22 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 2 Feb 2021 22:04:31 +0000 (17:04 -0500)
Instead of having manually-written lightning-specific code in a
supertrait walk in the middle of a large function, move it to a
utility function up next to the other manually-written-impl-block
functions.

c-bindings-gen/src/main.rs

index c0c5ecee1e9b0d4522d59c94658a52855b2051bf..b04d6b007828f9adc40eef56dae5bca551cc65b9 100644 (file)
@@ -80,6 +80,19 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
        }
 }
 
+/// Write out the impl block for a defined trait struct which has a supertrait
+fn do_write_impl_trait<W: std::io::Write>(w: &mut W, trait_path: &str, trait_name: &syn::Ident, for_obj: &str) {
+       match trait_path {
+               "util::events::MessageSendEventsProvider" => {
+                       writeln!(w, "impl lightning::{} for {} {{", trait_path, for_obj).unwrap();
+                       writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
+                       writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", trait_path, trait_path, trait_name).unwrap();
+                       writeln!(w, "\t}}\n}}").unwrap();
+               },
+               _ => panic!(),
+       }
+}
+
 // *******************************
 // *** Per-Type Printing Logic ***
 // *******************************
@@ -251,13 +264,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
                        writeln!(w, "\t}}\n}}").unwrap();
                },
                (s, i) => {
-                       if s != "util::events::MessageSendEventsProvider" { unimplemented!(); }
-                       // XXX: We straight-up cheat here - instead of bothering to get the trait object we
-                       // just print what we need since this is only used in one place.
-                       writeln!(w, "impl lightning::{} for {} {{", s, trait_name).unwrap();
-                       writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
-                       writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", s, s, i).unwrap();
-                       writeln!(w, "\t}}\n}}").unwrap();
+                       do_write_impl_trait(w, s, i, &trait_name);
                }
        ) );