simplified by entirely removing DataRef

This commit is contained in:
Schrottkatze 2024-02-23 12:46:39 +01:00
parent 619b7acf94
commit 911339fc2a
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
6 changed files with 19 additions and 69 deletions

View file

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

View file

@ -37,7 +37,7 @@ impl Available {
#[must_use]
pub fn pick(&self) -> Box<dyn Evaluator> {
match self {
Self::Debug => Box::new(kind::debug::Evaluator::default()),
Self::Debug => Box::<kind::debug::Evaluator>::default(),
}
}
}

View file

@ -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<DataRef<'a>>);
pub struct Inputs<'a>(pub Vec<&'a Data>);
impl<'a> Inputs<'a> {
/// get inner value(s)
pub(crate) fn inner(&self) -> Vec<DataRef<'a>> {
self.0.clone()
}
}
impl<'a> From<Vec<DataRef<'a>>> for Inputs<'a> {
fn from(value: Vec<DataRef<'a>>) -> Self {
impl<'a> From<Vec<&'a Data>> for Inputs<'a> {
fn from(value: Vec<&'a Data>) -> Self {
Self(value)
}
}
impl<'a, T: Into<DataRef<'a>>> From<T> for Inputs<'a> {
impl<'a, T: Into<&'a Data>> From<T> for Inputs<'a> {
fn from(value: T) -> Self {
Self(vec![value.into()])
}
@ -53,12 +46,6 @@ impl<T: Into<Data>> From<T> for Outputs {
}
impl From<Inputs<'_>> 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())
}
}

View file

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

View file

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

View file

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