From 44172da94fa36c10caf9d6f7ccbb99c17ddd59bb Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 31 Jan 2022 20:44:10 +0100 Subject: [PATCH] Use Arcs for the callbacks So it becomes possible to reuse one callback for multiple parameters. This will be important for value change notifications. --- src/formatters.rs | 6 ++++-- src/params.rs | 9 +++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/formatters.rs b/src/formatters.rs index 8e3265e6..1fe1de93 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -16,7 +16,9 @@ //! Convenience functions for formatting and parsing parameter values in common formats. +use std::sync::Arc; + /// Round an `f32` value to always have a specific number of decimal digits. -pub fn f32_rounded(digits: usize) -> Option String + Send + Sync>> { - Some(Box::new(move |x| format!("{:.digits$}", x))) +pub fn f32_rounded(digits: usize) -> Option String + Send + Sync>> { + Some(Arc::new(move |x| format!("{:.digits$}", x))) } diff --git a/src/params.rs b/src/params.rs index c8d0c4e0..4a2dcd57 100644 --- a/src/params.rs +++ b/src/params.rs @@ -19,6 +19,7 @@ use std::collections::HashMap; use std::fmt::Display; use std::pin::Pin; +use std::sync::Arc; pub type FloatParam = PlainParam; pub type IntParam = PlainParam; @@ -62,11 +63,11 @@ pub struct PlainParam { /// The parameter value's unit, added after `value_to_string` if that is set. pub unit: &'static str, /// Optional custom conversion function from a plain **unnormalized** value to a string. - pub value_to_string: Option String + Send + Sync>>, + pub value_to_string: Option String + Send + Sync>>, /// Optional custom conversion function from a string to a plain **unnormalized** value. If the /// string cannot be parsed, then this should return a `None`. If this happens while the /// parameter is being updated then the update will be canceled. - pub string_to_value: Option Option + Send + Sync>>, + pub string_to_value: Option Option + Send + Sync>>, } /// A simple boolean parmaeter. @@ -77,11 +78,11 @@ pub struct BoolParam { /// The parameter's human readable display name. pub name: &'static str, /// Optional custom conversion function from a boolean value to a string. - pub value_to_string: Option String + Send + Sync>>, + pub value_to_string: Option String + Send + Sync>>, /// Optional custom conversion function from a string to a boolean value. If the string cannot /// be parsed, then this should return a `None`. If this happens while the parameter is being /// updated then the update will be canceled. - pub string_to_value: Option Option + Send + Sync>>, + pub string_to_value: Option Option + Send + Sync>>, } /// Describes a single parmaetre of any type.