From f7be9273e522fd766ebfa0edf015b4dbf132bca4 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Fri, 26 Jul 2024 13:37:03 +0100 Subject: [PATCH] Avoid allocating strings in Display implementation. --- src/device/sound.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/device/sound.rs b/src/device/sound.rs index f05dff17..31494b0a 100644 --- a/src/device/sound.rs +++ b/src/device/sound.rs @@ -1,7 +1,7 @@ //! Driver for VirtIO Sound devices. use alloc::vec; -use alloc::{boxed::Box, collections::BTreeMap, format, vec::Vec}; +use alloc::{boxed::Box, collections::BTreeMap, vec::Vec}; use bitflags::bitflags; use core::{ fmt::{self, Debug, Display, Formatter}, @@ -1655,16 +1655,19 @@ impl Display for VirtIOSndChmapInfo { } else { "OUTPUT" }; - let mut positions = vec![]; - for i in 0..self.channels as usize { - let position = format!("{:?}", ChannelPosition::from(self.positions[i])); - positions.push(position); - } write!( f, - "direction: {}, channels: {}, postions: {:?}", - direction, self.channels, positions - ) + "direction: {}, channels: {}, postions: [", + direction, self.channels + )?; + for i in 0..usize::from(self.channels) { + if i != 0 { + write!(f, ", ")?; + } + write!(f, "{:?}", ChannelPosition::from(self.positions[i]))?; + } + write!(f, "]")?; + Ok(()) } }