Skip to content

Commit dcfd95a

Browse files
committed
[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.
1 parent 4edf514 commit dcfd95a

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

c-bindings-gen/src/main.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
8080
}
8181
}
8282

83+
/// Write out the impl block for a defined trait struct which has a supertrait
84+
fn do_write_impl_trait<W: std::io::Write>(w: &mut W, trait_path: &str, trait_name: &syn::Ident, for_obj: &str) {
85+
match trait_path {
86+
"util::events::MessageSendEventsProvider" => {
87+
writeln!(w, "impl lightning::{} for {} {{", trait_path, for_obj).unwrap();
88+
writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
89+
writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", trait_path, trait_path, trait_name).unwrap();
90+
writeln!(w, "\t}}\n}}").unwrap();
91+
},
92+
_ => panic!(),
93+
}
94+
}
95+
8396
// *******************************
8497
// *** Per-Type Printing Logic ***
8598
// *******************************
@@ -251,13 +264,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty
251264
writeln!(w, "\t}}\n}}").unwrap();
252265
},
253266
(s, i) => {
254-
if s != "util::events::MessageSendEventsProvider" { unimplemented!(); }
255-
// XXX: We straight-up cheat here - instead of bothering to get the trait object we
256-
// just print what we need since this is only used in one place.
257-
writeln!(w, "impl lightning::{} for {} {{", s, trait_name).unwrap();
258-
writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
259-
writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", s, s, i).unwrap();
260-
writeln!(w, "\t}}\n}}").unwrap();
267+
do_write_impl_trait(w, s, i, &trait_name);
261268
}
262269
) );
263270

0 commit comments

Comments
 (0)