From b3fca4df2eda2f2bfe8861ab88b05a2573a7b502 Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 24 Aug 2018 15:05:13 +0200 Subject: Debug implementation: Don't use format! or String when core is enabled As --use-core is typically given when the wrapped library is to be used in a no_std environment, format! and String can not be used. This is a quick fix that will cause regressions in the quality of the debug output to users that use core but are not no_std, but enables the use of bindgen with implemented Debug traits to no_std users. Closes: https://github.com/rust-lang-nursery/rust-bindgen/issues/1100 --- src/codegen/impl_debug.rs | 50 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'src/codegen/impl_debug.rs') diff --git a/src/codegen/impl_debug.rs b/src/codegen/impl_debug.rs index a604fc92..077d5737 100644 --- a/src/codegen/impl_debug.rs +++ b/src/codegen/impl_debug.rs @@ -186,28 +186,44 @@ impl<'a> ImplDebug<'a> for Item { // The simple case debug_print(name, quote! { #name_ident }) } else { - // Let's implement our own print function + if ctx.options().use_core { + // There is no String in core; reducing field visibility to avoid breaking + // no_std setups. + Some(( + format!("{}: [...]", name), vec![] + )) + } else { + // Let's implement our own print function + Some(( + format!("{}: [{{}}]", name), + vec![quote! { + self.#name_ident + .iter() + .enumerate() + .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) + .collect::() + }], + )) + } + } + } + TypeKind::Vector(_, len) => { + if ctx.options().use_core { + // There is no format! in core; reducing field visibility to avoid breaking + // no_std setups. Some(( - format!("{}: [{{}}]", name), + format!("{}(...)", name), vec![] + )) + } else { + let self_ids = 0..len; + Some(( + format!("{}({{}})", name), vec![quote! { - self.#name_ident - .iter() - .enumerate() - .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) - .collect::() - }], + #(format!("{:?}", self.#self_ids)),* + }] )) } } - TypeKind::Vector(_, len) => { - let self_ids = 0..len; - Some(( - format!("{}({{}})", name), - vec![quote! { - #(format!("{:?}", self.#self_ids)),* - }] - )) - } TypeKind::ResolvedTypeRef(t) | TypeKind::TemplateAlias(t, _) | -- cgit v1.2.3