1
0
Fork 0

Use Arcs for the callbacks

So it becomes possible to reuse one callback for multiple parameters.
This will be important for value change notifications.
This commit is contained in:
Robbert van der Helm 2022-01-31 20:44:10 +01:00
parent c883e0000d
commit 44172da94f
2 changed files with 9 additions and 6 deletions

View file

@ -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<Box<dyn Fn(f32) -> String + Send + Sync>> {
Some(Box::new(move |x| format!("{:.digits$}", x)))
pub fn f32_rounded(digits: usize) -> Option<Arc<dyn Fn(f32) -> String + Send + Sync>> {
Some(Arc::new(move |x| format!("{:.digits$}", x)))
}

View file

@ -19,6 +19,7 @@
use std::collections::HashMap;
use std::fmt::Display;
use std::pin::Pin;
use std::sync::Arc;
pub type FloatParam = PlainParam<f32>;
pub type IntParam = PlainParam<i32>;
@ -62,11 +63,11 @@ pub struct PlainParam<T> {
/// 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<Box<dyn Fn(T) -> String + Send + Sync>>,
pub value_to_string: Option<Arc<dyn Fn(T) -> 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<Box<dyn Fn(&str) -> Option<T> + Send + Sync>>,
pub string_to_value: Option<Arc<dyn Fn(&str) -> Option<T> + 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<Box<dyn Fn(bool) -> String + Send + Sync>>,
pub value_to_string: Option<Arc<dyn Fn(bool) -> 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<Box<dyn Fn(&str) -> Option<bool> + Send + Sync>>,
pub string_to_value: Option<Arc<dyn Fn(&str) -> Option<bool> + Send + Sync>>,
}
/// Describes a single parmaetre of any type.