diff --git a/src/types/simple.rs b/src/types/simple.rs index 43ed169d9..63d885c92 100644 --- a/src/types/simple.rs +++ b/src/types/simple.rs @@ -97,6 +97,10 @@ pub trait PrimType: TypeRowElem + std::fmt::Debug + sealed::Sealed { fn tag(&self) -> TypeTag; } +impl TypeRowElem for SimpleType {} +impl TypeRowElem for ClassicType {} +impl TypeRowElem for HashableType {} + // sealed trait pattern to prevent users extending PrimType mod sealed { use super::{ClassicType, HashableType, SimpleType}; diff --git a/src/types/simple/serialize.rs b/src/types/simple/serialize.rs index bb09fcd93..470388b43 100644 --- a/src/types/simple/serialize.rs +++ b/src/types/simple/serialize.rs @@ -6,6 +6,7 @@ use super::HashableType; use super::PrimType; use super::TypeTag; +use itertools::Itertools; use smol_str::SmolStr; use super::super::custom::CustomType; @@ -17,6 +18,7 @@ use super::SimpleType; use super::super::AbstractSignature; use crate::ops::constant::HugrIntWidthStore; +use crate::types::type_row::TypeRowElem; #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[serde(tag = "t")] @@ -30,11 +32,11 @@ pub(crate) enum SerSimpleType { signature: Box, }, Tuple { - row: Box>, + row: Vec, c: TypeTag, }, Sum { - row: Box>, + row: Vec, c: TypeTag, }, Array { @@ -79,15 +81,15 @@ where fn from(value: Container) -> Self { match value { Container::Sum(inner) => SerSimpleType::Sum { - row: Box::new(inner.map_into()), + row: inner.into_owned().into_iter().map_into().collect(), c: T::TAG, // We could inspect inner.containing_tag(), but this should have been done already }, Container::Tuple(inner) => SerSimpleType::Tuple { - row: Box::new(inner.map_into()), + row: inner.into_owned().into_iter().map_into().collect(), c: T::TAG, }, Container::Array(inner, len) => SerSimpleType::Array { - inner: box_convert(*inner), + inner: Box::new((*inner).into()), len, c: T::TAG, }, @@ -130,19 +132,14 @@ impl From for SerSimpleType { } } -pub(crate) fn box_convert_try(value: T) -> Box -where - T: TryInto, - >::Error: std::fmt::Debug, -{ - Box::new((value).try_into().unwrap()) -} - -pub(crate) fn box_convert(value: T) -> Box -where - T: Into, -{ - Box::new((value).into()) +fn try_convert_list, T2: TypeRowElem>( + values: Vec, +) -> Result, T::Error> { + let vals = values + .into_iter() + .map(T::try_into) + .collect::, T::Error>>()?; + Ok(TypeRow::from(vals)) } macro_rules! handle_container { @@ -163,13 +160,13 @@ impl From for SimpleType { SerSimpleType::S => HashableType::String.into(), SerSimpleType::G { signature } => ClassicType::Graph(Box::new(*signature)).into(), SerSimpleType::Tuple { row: inner, c } => { - handle_container!(c, Tuple(Box::new(inner.try_convert_elems().unwrap()))) + handle_container!(c, Tuple(Box::new(try_convert_list(inner).unwrap()))) } SerSimpleType::Sum { row: inner, c } => { - handle_container!(c, Sum(Box::new(inner.try_convert_elems().unwrap()))) + handle_container!(c, Sum(Box::new(try_convert_list(inner).unwrap()))) } SerSimpleType::Array { inner, len, c } => { - handle_container!(c, Array(box_convert_try(*inner), len)) + handle_container!(c, Array(Box::new((*inner).try_into().unwrap()), len)) } SerSimpleType::Alias { name: s, c } => handle_container!(c, Alias(s)), SerSimpleType::Opaque { custom, c } => { diff --git a/src/types/type_row.rs b/src/types/type_row.rs index eee7928bc..37af0c1dd 100644 --- a/src/types/type_row.rs +++ b/src/types/type_row.rs @@ -12,8 +12,6 @@ use crate::utils::display_list; /// Base trait for anything that can be put in a [TypeRow] pub trait TypeRowElem: Clone + 'static {} -impl TypeRowElem for T {} - /// List of types, used for function signatures. #[derive(Clone, PartialEq, Eq, Debug, serde::Serialize, serde::Deserialize)] //#[cfg_attr(feature = "pyo3", pyclass)] // TODO: expose unparameterized versions