diff --git a/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs b/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs index c8f19bc5af..05e66a72bc 100644 --- a/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs +++ b/bindgen-tests/tests/expectations/tests/derive-bitfield-method-same-name.rs @@ -136,12 +136,18 @@ impl ::std::fmt::Debug for Foo { write!( f, "Foo {{ large: [{}], type_ : {:?}, }}", - self - .large - .iter() - .enumerate() - .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) - .collect::(), + { + use std::fmt::Write as _; + let mut output = String::new(); + let mut iter = self.large.iter(); + if let Some(value) = iter.next() { + let _ = write!(output, "{value:?}"); + for value in iter { + let _ = write!(output, ", {value:?}"); + } + } + output + }, self.type__bindgen_bitfield(), ) } diff --git a/bindgen-tests/tests/expectations/tests/derive-debug-bitfield.rs b/bindgen-tests/tests/expectations/tests/derive-debug-bitfield.rs index 20c7cf0c88..e266880509 100644 --- a/bindgen-tests/tests/expectations/tests/derive-debug-bitfield.rs +++ b/bindgen-tests/tests/expectations/tests/derive-debug-bitfield.rs @@ -122,12 +122,18 @@ impl ::std::fmt::Debug for C { "C {{ a : {:?}, b : {:?}, large_array: [{}] }}", self.a(), self.b(), - self - .large_array - .iter() - .enumerate() - .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) - .collect::(), + { + use std::fmt::Write as _; + let mut output = String::new(); + let mut iter = self.large_array.iter(); + if let Some(value) = iter.next() { + let _ = write!(output, "{value:?}"); + for value in iter { + let _ = write!(output, ", {value:?}"); + } + } + output + }, ) } } diff --git a/bindgen-tests/tests/expectations/tests/derive-debug-function-pointer.rs b/bindgen-tests/tests/expectations/tests/derive-debug-function-pointer.rs index 9077201a77..9fe1f5518c 100644 --- a/bindgen-tests/tests/expectations/tests/derive-debug-function-pointer.rs +++ b/bindgen-tests/tests/expectations/tests/derive-debug-function-pointer.rs @@ -40,12 +40,18 @@ impl ::std::fmt::Debug for Nice { f, "Nice {{ pointer: {:?}, large_array: [{}] }}", self.pointer, - self - .large_array - .iter() - .enumerate() - .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) - .collect::(), + { + use std::fmt::Write as _; + let mut output = String::new(); + let mut iter = self.large_array.iter(); + if let Some(value) = iter.next() { + let _ = write!(output, "{value:?}"); + for value in iter { + let _ = write!(output, ", {value:?}"); + } + } + output + }, ) } } diff --git a/bindgen/codegen/impl_debug.rs b/bindgen/codegen/impl_debug.rs index c4daddf260..319c89b58e 100644 --- a/bindgen/codegen/impl_debug.rs +++ b/bindgen/codegen/impl_debug.rs @@ -186,13 +186,18 @@ impl<'a> ImplDebug<'a> for Item { // 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::() - }], + vec![quote! {{ + use std::fmt::Write as _; + let mut output = String::new(); + let mut iter = self.#name_ident.iter(); + if let Some(value) = iter.next() { + let _ = write!(output, "{value:?}"); + for value in iter { + let _ = write!(output, ", {value:?}"); + } + } + output + }}], )) } }