Skip to main content

define_data_types

Macro define_data_types 

Source
macro_rules! define_data_types {
    (
        $(#[$meta:meta])*
        $vis:vis $data_name:ident / $animated_name:ident / $discriminant_name:ident {
            $(
                $(#[$variant_meta:meta])*
                $variant:ident($inner_ty:ty)
            ),+ $(,)?
        }
    ) => { ... };
}
Expand description

Define a custom data type system with full interpolation support.

This macro generates three enums and implements all necessary traits for use with GenericValue and GenericTokenValueMap:

  1. A discriminant enum (like DataType) with unit variants.
  2. A data enum (like Data) holding actual values.
  3. An animated data enum (like AnimatedData) holding TimeDataMap<T> values.

§Example

use token_value_map::{define_data_types, TimeDataMap, Time, DataSystem};

define_data_types! {
    /// My custom data types.
    #[derive(Clone, Debug, PartialEq)]
    pub MyData / MyAnimatedData / MyDataType {
        /// A floating point value.
        Float(MyFloat),
        /// An integer value.
        Int(MyInt),
    }
}

// Now you can use GenericValue<MyData> and GenericTokenValueMap<MyData>.
use token_value_map::GenericValue;

let uniform = GenericValue::<MyData>::uniform(MyData::Float(MyFloat(42.0)));
let animated = GenericValue::<MyData>::animated(vec![
    (Time::default(), MyData::Float(MyFloat(0.0))),
    (Time::from(10.0), MyData::Float(MyFloat(100.0))),
]).unwrap();

§Generated Types

Given MyData / MyAnimatedData / MyDataType:

  • MyDataType: Discriminant enum with unit variants (Float, Int, Text).
  • MyData: Data enum holding values (Float(f32), Int(i32), Text(String)).
  • MyAnimatedData: Animated enum holding time maps (Float(TimeDataMap<f32>), etc.).

§Requirements

Each variant type must implement:

  • Clone + Debug + PartialEq + Eq + Hash + Send + Sync + 'static

For interpolation support, types should also implement:

  • Add<Output = Self> + Sub<Output = Self>
  • Mul<f32, Output = Self> + Mul<f64, Output = Self>
  • Div<f32, Output = Self> + Div<f64, Output = Self>

Types that don’t support interpolation will use sample-and-hold behavior.