From d643df35f408201da40426a82c0b08b0a76562c0 Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Sun, 14 Jan 2024 15:10:55 -0600 Subject: [PATCH] have DisplayFn take a Formatter instead of a dyn impl Write --- examples/gain/src/lib.rs | 4 +--- src/format/clap/instance.rs | 5 ++--- src/format/vst3/component.rs | 5 ++--- src/param.rs | 4 ++-- src/util.rs | 12 ++++++++++++ 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/examples/gain/src/lib.rs b/examples/gain/src/lib.rs index 666529e..c48a340 100644 --- a/examples/gain/src/lib.rs +++ b/examples/gain/src/lib.rs @@ -39,9 +39,7 @@ impl Plugin for Gain { default: 1.0, steps: None, parse: Box::new(|s| s.parse().ok()), - display: Box::new(|v, w| { - let _ = write!(w, "{:.2}", v); - }), + display: Box::new(|v, f| write!(f, "{:.2}", v)), }], } } diff --git a/src/format/clap/instance.rs b/src/format/clap/instance.rs index 700a505..acb799b 100644 --- a/src/format/clap/instance.rs +++ b/src/format/clap/instance.rs @@ -14,7 +14,7 @@ use crate::bus::{BusDir, Format}; use crate::events::{Data, Event, Events}; use crate::param::ParamInfo; use crate::sync::params::ParamValues; -use crate::util::{copy_cstring, slice_from_raw_parts_checked}; +use crate::util::{copy_cstring, slice_from_raw_parts_checked, DisplayParam}; use crate::{Config, Editor, Host, ParamId, ParamValue, Plugin, PluginInfo, Processor}; fn port_type_from_format(format: &Format) -> &'static CStr { @@ -572,8 +572,7 @@ impl Instance

{ if let Some(&index) = instance.param_map.get(¶m_id) { let param = &instance.info.params[index]; - let mut text = String::new(); - (param.display)(map_param_in(param, value), &mut text); + let text = format!("{}", DisplayParam(param, map_param_in(param, value))); let dst = slice::from_raw_parts_mut(display, size as usize); copy_cstring(&text, dst); diff --git a/src/format/vst3/component.rs b/src/format/vst3/component.rs index ec6b156..4da659f 100644 --- a/src/format/vst3/component.rs +++ b/src/format/vst3/component.rs @@ -12,7 +12,7 @@ use super::view::View; use crate::bus::{BusDir, Format, Layout}; use crate::events::{Data, Event, Events}; use crate::sync::params::ParamValues; -use crate::util::slice_from_raw_parts_checked; +use crate::util::{slice_from_raw_parts_checked, DisplayParam}; use crate::{Config, Editor, Host, ParamId, Plugin, PluginInfo, Processor}; fn format_to_speaker_arrangement(format: &Format) -> SpeakerArrangement { @@ -583,8 +583,7 @@ impl IEditControllerTrait for Component

{ if let Some(&index) = self.param_map.get(&id) { let param = &self.info.params[index]; - let mut display = String::new(); - (param.display)(valueNormalized, &mut display); + let display = format!("{}", DisplayParam(param, valueNormalized)); copy_wstring(&display, &mut *string); return kResultOk; diff --git a/src/param.rs b/src/param.rs index e01cb1d..22b4d8e 100644 --- a/src/param.rs +++ b/src/param.rs @@ -1,9 +1,9 @@ -use std::fmt::Write; +use std::fmt::{self, Formatter}; use crate::{ParamId, ParamValue}; pub type ParseFn = dyn Fn(&str) -> Option + Send + Sync; -pub type DisplayFn = dyn Fn(ParamValue, &mut dyn Write) + Send + Sync; +pub type DisplayFn = dyn Fn(ParamValue, &mut Formatter) -> Result<(), fmt::Error> + Send + Sync; pub struct ParamInfo { pub id: ParamId, diff --git a/src/util.rs b/src/util.rs index de7efec..434a8b8 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,11 @@ use std::ffi::CString; +use std::fmt::{self, Display, Formatter}; use std::os::raw::c_char; use std::slice; +use crate::param::ParamInfo; +use crate::ParamValue; + pub fn copy_cstring(src: &str, dst: &mut [c_char]) { let c_string = CString::new(src).unwrap_or_else(|_| CString::default()); let bytes = c_string.as_bytes_with_nul(); @@ -28,3 +32,11 @@ pub unsafe fn slice_from_raw_parts_checked<'a, T>(ptr: *const T, len: usize) -> &[] } } + +pub struct DisplayParam<'a>(pub &'a ParamInfo, pub ParamValue); + +impl<'a> Display for DisplayParam<'a> { + fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { + (self.0.display)(self.1, f) + } +}