xmpp-parsers: Optimize to_hex() a bit

Just one correctly-sized String allocation, instead of one String per
byte, a Vec<String>, and then a final String.

skip-changelog: Internal change.
This commit is contained in:
Link Mauve 2026-06-09 00:08:52 +02:00 committed by Jonas Schäfer
commit 47c0edc3f6

View file

@ -6,6 +6,7 @@
use alloc::borrow::Cow;
use core::{
fmt::Write,
num::ParseIntError,
ops::{Deref, DerefMut},
str::FromStr,
@ -181,11 +182,11 @@ impl Hash {
/// Formats this hash into hexadecimal.
pub fn to_hex(&self) -> String {
self.hash
.iter()
.map(|byte| format!("{:02x}", byte))
.collect::<Vec<_>>()
.join("")
let mut hex = String::with_capacity(self.hash.len() * 2);
for byte in &self.hash {
write!(&mut hex, "{:02x}", byte).unwrap();
}
hex
}
/// Formats this hash into colon-separated hexadecimal.