experimentation: use dynamic type ids for signatures and add qol macro

This commit is contained in:
Schrottkatze 2024-02-26 12:36:57 +01:00
parent 911339fc2a
commit 31a044577a
No known key found for this signature in database
4 changed files with 21 additions and 33 deletions

View file

@ -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;

View file

@ -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<DataType>,
pub outputs: Vec<DataType>,
pub inputs: Vec<TypeId>,
pub outputs: Vec<TypeId>,
}
/// 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>(), )+]
}
)
}

View file

@ -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)
}
}

View file

@ -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)
}
}