From 29b37069e9deb8c13bfb0e7a51a7886073e822c5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 22 Sep 2021 02:43:51 +0000 Subject: [PATCH] Support mapping core::fmt::Debug as a supertrait --- c-bindings-gen/src/main.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index bfadca0..9cd32fe 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -329,6 +329,13 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty Some(format!("\t/**\n\t * {}\n\t * {}\n\t */\n", hash_docs_a, hash_docs_b)))); }, ("Send", _) => {}, ("Sync", _) => {}, + ("std::fmt::Debug", _)|("core::fmt::Debug", _) => { + let debug_docs = "Return a human-readable \"debug\" string describing this object"; + writeln!(w, "\t/// {}", debug_docs).unwrap(); + writeln!(w, "\tpub debug_str: extern \"C\" fn (this_arg: *const c_void) -> crate::c_types::Str,").unwrap(); + generated_fields.push(("debug_str".to_owned(), None, + Some(format!("\t/**\n\t * {}\n\t */\n", debug_docs)))); + }, (s, i) => { // TODO: Both of the below should expose supertrait methods in C++, but doing so is // nontrivial. @@ -494,6 +501,13 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty writeln!(w, "\t\t{}_clone(self)", trait_name).unwrap(); writeln!(w, "\t}}\n}}").unwrap(); }, + ("std::fmt::Debug", _)|("core::fmt::Debug", _) => { + writeln!(w, "impl core::fmt::Debug for {} {{", trait_name).unwrap(); + writeln!(w, "\tfn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {{").unwrap(); + writeln!(w, "\t\tf.write_str((self.debug_str)(self.this_arg).into_str())").unwrap(); + writeln!(w, "\t}}").unwrap(); + writeln!(w, "}}").unwrap(); + }, (s, i) => { if let Some(supertrait) = types.crate_types.traits.get(s) { let mut module_iter = s.rsplitn(2, "::"); @@ -827,6 +841,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ }, ("Sync", _) => {}, ("Send", _) => {}, ("std::marker::Sync", _) => {}, ("std::marker::Send", _) => {}, + ("core::fmt::Debug", _) => {}, (s, t) => { if let Some(supertrait_obj) = types.crate_types.traits.get(s) { writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap(); -- 2.30.2