Skip to content

Commit b3fca4d

Browse files
chrysnemilio
authored andcommitted
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: #1100
1 parent 6084b39 commit b3fca4d

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/codegen/impl_debug.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,28 +186,44 @@ impl<'a> ImplDebug<'a> for Item {
186186
// The simple case
187187
debug_print(name, quote! { #name_ident })
188188
} else {
189-
// Let's implement our own print function
189+
if ctx.options().use_core {
190+
// There is no String in core; reducing field visibility to avoid breaking
191+
// no_std setups.
192+
Some((
193+
format!("{}: [...]", name), vec![]
194+
))
195+
} else {
196+
// Let's implement our own print function
197+
Some((
198+
format!("{}: [{{}}]", name),
199+
vec![quote! {
200+
self.#name_ident
201+
.iter()
202+
.enumerate()
203+
.map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v))
204+
.collect::<String>()
205+
}],
206+
))
207+
}
208+
}
209+
}
210+
TypeKind::Vector(_, len) => {
211+
if ctx.options().use_core {
212+
// There is no format! in core; reducing field visibility to avoid breaking
213+
// no_std setups.
190214
Some((
191-
format!("{}: [{{}}]", name),
215+
format!("{}(...)", name), vec![]
216+
))
217+
} else {
218+
let self_ids = 0..len;
219+
Some((
220+
format!("{}({{}})", name),
192221
vec![quote! {
193-
self.#name_ident
194-
.iter()
195-
.enumerate()
196-
.map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v))
197-
.collect::<String>()
198-
}],
222+
#(format!("{:?}", self.#self_ids)),*
223+
}]
199224
))
200225
}
201226
}
202-
TypeKind::Vector(_, len) => {
203-
let self_ids = 0..len;
204-
Some((
205-
format!("{}({{}})", name),
206-
vec![quote! {
207-
#(format!("{:?}", self.#self_ids)),*
208-
}]
209-
))
210-
}
211227

212228
TypeKind::ResolvedTypeRef(t) |
213229
TypeKind::TemplateAlias(t, _) |

0 commit comments

Comments
 (0)