summaryrefslogtreecommitdiff
path: root/src/codegen/impl_debug.rs
diff options
context:
space:
mode:
authorchrysn <chrysn@fsfe.org>2018-08-24 15:05:13 +0200
committerEmilio Cobos Álvarez <emilio@crisal.io>2018-08-28 12:47:44 +0200
commitb3fca4df2eda2f2bfe8861ab88b05a2573a7b502 (patch)
tree03feb5d01949a3cf616603019a530b0c0678da2c /src/codegen/impl_debug.rs
parent6084b394b3794b29b0751e90fe278f86bf133666 (diff)
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
Diffstat (limited to 'src/codegen/impl_debug.rs')
-rw-r--r--src/codegen/impl_debug.rs50
1 files changed, 33 insertions, 17 deletions
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::<String>()
+ }],
+ ))
+ }
+ }
+ }
+ 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::<String>()
- }],
+ #(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, _) |