From 31a044577a59617c79649de297f2468336d5e1aa Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 26 Feb 2024 12:36:57 +0100 Subject: [PATCH] experimentation: use dynamic type ids for signatures and add qol macro --- .../src/experimental/trait_based.rs | 1 + .../src/experimental/trait_based/element.rs | 18 +++++++++++------- .../src/experimental/trait_based/ops/num.rs | 18 +++++------------- .../src/experimental/trait_based/ops/str.rs | 17 ++++------------- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/crates/prowocessing/src/experimental/trait_based.rs b/crates/prowocessing/src/experimental/trait_based.rs index 7e742a1..251fb35 100644 --- a/crates/prowocessing/src/experimental/trait_based.rs +++ b/crates/prowocessing/src/experimental/trait_based.rs @@ -5,6 +5,7 @@ //! - [Builder pattern](https://rust-unofficial.github.io/patterns/patterns/creational/builder.html) pub mod data; +#[macro_use] pub mod element; pub mod ops; pub mod pipeline; diff --git a/crates/prowocessing/src/experimental/trait_based/element.rs b/crates/prowocessing/src/experimental/trait_based/element.rs index b816351..b07c739 100644 --- a/crates/prowocessing/src/experimental/trait_based/element.rs +++ b/crates/prowocessing/src/experimental/trait_based/element.rs @@ -1,5 +1,7 @@ //! The trait and type representations +use std::any::TypeId; + use crate::experimental::trait_based::data::io::Inputs; use super::data::io::Outputs; @@ -13,13 +15,15 @@ pub(crate) trait PipelineElement { /// Type signature for an element used for static checking pub(crate) struct ElementSignature { - pub inputs: Vec, - pub outputs: Vec, + pub inputs: Vec, + pub outputs: Vec, } -/// Data type enum -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum DataType { - String, - Int, +macro_rules! signature { + ($($inputs:ty),+ => $($outputs:ty),+) => ( + ElementSignature { + inputs: vec![$(std::any::TypeId::of::<$inputs>(), )+], + outputs: vec![$(std::any::TypeId::of::<$outputs>(), )+] + } + ) } diff --git a/crates/prowocessing/src/experimental/trait_based/ops/num.rs b/crates/prowocessing/src/experimental/trait_based/ops/num.rs index 1a0a701..ea189bf 100644 --- a/crates/prowocessing/src/experimental/trait_based/ops/num.rs +++ b/crates/prowocessing/src/experimental/trait_based/ops/num.rs @@ -1,12 +1,13 @@ //! Operations on numeric data use core::panic; +use std::any::TypeId; use crate::experimental::trait_based::{ data::{ io::{Inputs, Outputs}, raw::Data, }, - element::{DataType, ElementSignature, PipelineElement}, + element::{ElementSignature, PipelineElement}, }; /// Addition @@ -22,10 +23,7 @@ impl PipelineElement for Add { } fn signature(&self) -> ElementSignature { - ElementSignature { - inputs: vec![DataType::Int, DataType::Int], - outputs: vec![DataType::Int], - } + signature!(i32, i32 => i32) } } @@ -42,10 +40,7 @@ impl PipelineElement for Subtract { } fn signature(&self) -> ElementSignature { - ElementSignature { - inputs: vec![DataType::Int, DataType::Int], - outputs: vec![DataType::Int], - } + signature!(i32, i32 => i32) } } @@ -62,9 +57,6 @@ impl PipelineElement for Stringify { } fn signature(&self) -> ElementSignature { - ElementSignature { - inputs: vec![DataType::Int], - outputs: vec![DataType::String], - } + signature!(i32 => String) } } diff --git a/crates/prowocessing/src/experimental/trait_based/ops/str.rs b/crates/prowocessing/src/experimental/trait_based/ops/str.rs index 01fd25a..fb59ada 100644 --- a/crates/prowocessing/src/experimental/trait_based/ops/str.rs +++ b/crates/prowocessing/src/experimental/trait_based/ops/str.rs @@ -4,7 +4,7 @@ use crate::experimental::trait_based::{ io::{Inputs, Outputs}, raw::Data, }, - element::{DataType, ElementSignature, PipelineElement}, + element::{ElementSignature, PipelineElement}, }; /// Concatenate the inputs @@ -20,10 +20,7 @@ impl PipelineElement for Concatenate { } fn signature(&self) -> ElementSignature { - ElementSignature { - inputs: vec![DataType::String, DataType::String], - outputs: vec![DataType::String], - } + signature!(String, String => String) } } @@ -40,10 +37,7 @@ impl PipelineElement for Upper { } fn signature(&self) -> ElementSignature { - ElementSignature { - inputs: vec![DataType::String], - outputs: vec![DataType::String], - } + signature!(String => String) } } @@ -60,9 +54,6 @@ impl PipelineElement for Lower { } fn signature(&self) -> ElementSignature { - ElementSignature { - inputs: vec![DataType::String], - outputs: vec![DataType::String], - } + signature!(String => String) } }