From 911339fc2ae3c1e1e71c7ae1480b35f053f6a94f Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 23 Feb 2024 12:46:39 +0100 Subject: [PATCH] simplified by entirely removing `DataRef` --- crates/app/src/main.rs | 3 +- crates/eval/src/lib.rs | 2 +- .../src/experimental/trait_based/data/io.rs | 27 ++++--------- .../src/experimental/trait_based/data/raw.rs | 40 +------------------ .../src/experimental/trait_based/ops/num.rs | 8 ++-- .../src/experimental/trait_based/ops/str.rs | 8 ++-- 6 files changed, 19 insertions(+), 69 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index ffd7271..7780bbb 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -87,7 +87,8 @@ mod dev { let pipe = PipelineBuilder::new().add(1).stringify().build(); println!( "{:?}", - pipe.run(vec![num0.into(), num1.into()].into()).into_inner()[0] + pipe.run(vec![&num0.into(), &num1.into()].into()) + .into_inner()[0] ); } } diff --git a/crates/eval/src/lib.rs b/crates/eval/src/lib.rs index c3a6c38..8687d91 100644 --- a/crates/eval/src/lib.rs +++ b/crates/eval/src/lib.rs @@ -37,7 +37,7 @@ impl Available { #[must_use] pub fn pick(&self) -> Box { match self { - Self::Debug => Box::new(kind::debug::Evaluator::default()), + Self::Debug => Box::::default(), } } } diff --git a/crates/prowocessing/src/experimental/trait_based/data/io.rs b/crates/prowocessing/src/experimental/trait_based/data/io.rs index d091621..59e2f28 100644 --- a/crates/prowocessing/src/experimental/trait_based/data/io.rs +++ b/crates/prowocessing/src/experimental/trait_based/data/io.rs @@ -1,26 +1,19 @@ //! Types for element and pipeline IO -use std::convert::Into; +use std::{borrow::ToOwned, convert::Into}; -use super::raw::{Data, DataRef}; +use super::raw::Data; /// Newtype struct with borrowed types for pipeline/element inputs, so that doesn't force a move or clone -pub struct Inputs<'a>(Vec>); +pub struct Inputs<'a>(pub Vec<&'a Data>); -impl<'a> Inputs<'a> { - /// get inner value(s) - pub(crate) fn inner(&self) -> Vec> { - self.0.clone() - } -} - -impl<'a> From>> for Inputs<'a> { - fn from(value: Vec>) -> Self { +impl<'a> From> for Inputs<'a> { + fn from(value: Vec<&'a Data>) -> Self { Self(value) } } -impl<'a, T: Into>> From for Inputs<'a> { +impl<'a, T: Into<&'a Data>> From for Inputs<'a> { fn from(value: T) -> Self { Self(vec![value.into()]) } @@ -53,12 +46,6 @@ impl> From for Outputs { } impl From> for Outputs { fn from(value: Inputs) -> Self { - Self( - value - .0 - .into_iter() - .map(|i: DataRef<'_>| DataRef::to_owned_data(&i)) - .collect(), - ) + Self(value.0.into_iter().map(ToOwned::to_owned).collect()) } } diff --git a/crates/prowocessing/src/experimental/trait_based/data/raw.rs b/crates/prowocessing/src/experimental/trait_based/data/raw.rs index acc75c1..502bb91 100644 --- a/crates/prowocessing/src/experimental/trait_based/data/raw.rs +++ b/crates/prowocessing/src/experimental/trait_based/data/raw.rs @@ -1,6 +1,6 @@ //! Dynamic data storage and transfer types for use in [`io`] -/// Owned data type, for use mostly in outputs and storage +// Dynamic data type #[derive(Clone, Debug)] pub enum Data { String(String), @@ -18,41 +18,3 @@ impl From for Data { Self::Int(value) } } - -/// Unowned data type, for inputs into runner functions -#[derive(Clone, Copy, Debug)] -pub enum DataRef<'a> { - String(&'a str), - Int(i32), -} - -impl DataRef<'_> { - /// converts itself to `OwnedData` - pub fn to_owned_data(&self) -> Data { - match self { - DataRef::String(s) => (*s).to_owned().into(), - DataRef::Int(i) => (*i).into(), - } - } -} - -impl<'a> From<&'a str> for DataRef<'a> { - fn from(value: &'a str) -> Self { - Self::String(value) - } -} - -impl From for DataRef<'_> { - fn from(value: i32) -> Self { - Self::Int(value) - } -} - -impl<'a> From<&'a Data> for DataRef<'a> { - fn from(value: &'a Data) -> Self { - match value { - Data::String(s) => DataRef::String(s), - Data::Int(i) => DataRef::Int(*i), - } - } -} diff --git a/crates/prowocessing/src/experimental/trait_based/ops/num.rs b/crates/prowocessing/src/experimental/trait_based/ops/num.rs index a6ac18c..1a0a701 100644 --- a/crates/prowocessing/src/experimental/trait_based/ops/num.rs +++ b/crates/prowocessing/src/experimental/trait_based/ops/num.rs @@ -4,7 +4,7 @@ use core::panic; use crate::experimental::trait_based::{ data::{ io::{Inputs, Outputs}, - raw::DataRef, + raw::Data, }, element::{DataType, ElementSignature, PipelineElement}, }; @@ -14,7 +14,7 @@ pub struct Add(pub i32); impl PipelineElement for Add { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - let [DataRef::Int(i0), DataRef::Int(i1), ..] = input.inner()[..] else { + let [Data::Int(i0), Data::Int(i1), ..] = input.0[..] else { panic!("Invalid data passed") }; (i0 + i1).into() @@ -34,7 +34,7 @@ pub struct Subtract(pub i32); impl PipelineElement for Subtract { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - let [DataRef::Int(i0), DataRef::Int(i1), ..] = input.inner()[..] else { + let [Data::Int(i0), Data::Int(i1), ..] = input.0[..] else { panic!("Invalid data passed") }; (i0 + i1).into() @@ -54,7 +54,7 @@ pub struct Stringify; impl PipelineElement for Stringify { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - let [DataRef::Int(int), ..] = input.inner()[..] else { + let [Data::Int(int), ..] = input.0[..] else { panic!("Invalid data passed") }; int.to_string().into() diff --git a/crates/prowocessing/src/experimental/trait_based/ops/str.rs b/crates/prowocessing/src/experimental/trait_based/ops/str.rs index 49ce606..01fd25a 100644 --- a/crates/prowocessing/src/experimental/trait_based/ops/str.rs +++ b/crates/prowocessing/src/experimental/trait_based/ops/str.rs @@ -2,7 +2,7 @@ use crate::experimental::trait_based::{ data::{ io::{Inputs, Outputs}, - raw::DataRef, + raw::Data, }, element::{DataType, ElementSignature, PipelineElement}, }; @@ -12,7 +12,7 @@ pub struct Concatenate(pub String); impl PipelineElement for Concatenate { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - let [DataRef::String(s0), DataRef::String(s1), ..] = input.inner()[..] else { + let [Data::String(s0), Data::String(s1), ..] = input.0[..] else { panic!("Invalid data passed") }; format!("{s0}{s1}").into() @@ -32,7 +32,7 @@ pub struct Upper; impl PipelineElement for Upper { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - let [DataRef::String(s), ..] = input.inner()[..] else { + let [Data::String(s), ..] = input.0[..] else { panic!("Invalid data passed") }; s.to_uppercase().into() @@ -52,7 +52,7 @@ pub struct Lower; impl PipelineElement for Lower { fn runner(&self) -> fn(&Inputs) -> Outputs { |input| { - let [DataRef::String(s), ..] = input.inner()[..] else { + let [Data::String(s), ..] = input.0[..] else { panic!("Invalid data passed") }; s.to_lowercase().into()