diff --git a/execution-plan-traits/src/containers.rs b/execution-plan-traits/src/containers.rs new file mode 100644 index 00000000..7b8648c7 --- /dev/null +++ b/execution-plan-traits/src/containers.rs @@ -0,0 +1,44 @@ +//! Impl Value for various container types, if the inner type implements Value. + +use crate::{MemoryError, Primitive, Value}; +const NONE: &str = "None"; +const SOME: &str = "Some"; + +impl Value for Option +where + T: Value, +{ + fn into_parts(self) -> Vec { + match self { + Some(v) => { + let mut parts = Vec::new(); + parts.push(SOME.to_owned().into()); + parts.extend(v.into_parts()); + parts + } + None => vec![NONE.to_owned().into()], + } + } + + fn from_parts(values: &mut I) -> Result + where + I: Iterator>, + { + let variant: String = values + .next() + .flatten() + .ok_or(MemoryError::MemoryWrongSize)? + .try_into()?; + match variant.as_str() { + NONE => Ok(None), + SOME => { + let val = T::from_parts(values)?; + Ok(Some(val)) + } + other => Err(MemoryError::InvalidEnumVariant { + expected_type: "option".to_owned(), + actual: other.to_owned(), + }), + } + } +} diff --git a/execution-plan-traits/src/lib.rs b/execution-plan-traits/src/lib.rs index fe36d854..e25e69a2 100644 --- a/execution-plan-traits/src/lib.rs +++ b/execution-plan-traits/src/lib.rs @@ -5,6 +5,7 @@ pub use self::primitive::{NumericPrimitive, Primitive}; #[macro_use] mod primitive; +mod containers; /// Types that can be written to or read from KCEP program memory. /// If they require multiple memory addresses, they will be laid out @@ -18,48 +19,6 @@ pub trait Value: Sized { I: Iterator>; } -const NONE: &str = "None"; -const SOME: &str = "Some"; - -impl Value for Option -where - T: Value, -{ - fn into_parts(self) -> Vec { - match self { - Some(v) => { - let mut parts = Vec::new(); - parts.push(SOME.to_owned().into()); - parts.extend(v.into_parts()); - parts - } - None => vec![NONE.to_owned().into()], - } - } - - fn from_parts(values: &mut I) -> Result - where - I: Iterator>, - { - let variant: String = values - .next() - .flatten() - .ok_or(MemoryError::MemoryWrongSize)? - .try_into()?; - match variant.as_str() { - NONE => Ok(None), - SOME => { - let val = T::from_parts(values)?; - Ok(Some(val)) - } - other => Err(MemoryError::InvalidEnumVariant { - expected_type: "option".to_owned(), - actual: other.to_owned(), - }), - } - } -} - /// TODO #[derive(Debug, thiserror::Error, Default)] pub enum MemoryError {