Skip to content

Commit

Permalink
have DisplayFn take a Formatter instead of a dyn impl Write
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Jan 14, 2024
1 parent 34da36b commit d643df3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
4 changes: 1 addition & 3 deletions examples/gain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}],
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/format/clap/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -572,8 +572,7 @@ impl<P: Plugin> Instance<P> {
if let Some(&index) = instance.param_map.get(&param_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);
Expand Down
5 changes: 2 additions & 3 deletions src/format/vst3/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -583,8 +583,7 @@ impl<P: Plugin> IEditControllerTrait for Component<P> {
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;
Expand Down
4 changes: 2 additions & 2 deletions src/param.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fmt::Write;
use std::fmt::{self, Formatter};

use crate::{ParamId, ParamValue};

pub type ParseFn = dyn Fn(&str) -> Option<ParamValue> + 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,
Expand Down
12 changes: 12 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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)
}
}

0 comments on commit d643df3

Please sign in to comment.