From dcfd95a700b3629c8cd2ac560c3ade3fc7ef72bd Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 25 Nov 2020 12:22:03 -0500 Subject: [PATCH] [bindings] Handle MessageSendEventsProvider impl blocks in a util fn 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 | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index c0c5ecee1..b04d6b007 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -80,6 +80,19 @@ fn maybe_convert_trait_impl(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: &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 {{").unwrap(); + writeln!(w, "\t\t::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 {{").unwrap(); - writeln!(w, "\t\t::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); } ) ); -- 2.39.5