Skip to content

Commit

Permalink
SerSimpleType: use Vec not TypeRow (#381)
Browse files Browse the repository at this point in the history
* Also inlining singly-used methods `box_convert` and `box_convert_try`
* And switch TypeRowElem from a blanket impl to "elective" - we elect to
allow only TypeRows of SimpleType/ClassicType/HashableType
  • Loading branch information
acl-cqc authored Aug 9, 2023
1 parent 1db3f07 commit 9720b2f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/types/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
39 changes: 18 additions & 21 deletions src/types/simple/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand All @@ -30,11 +32,11 @@ pub(crate) enum SerSimpleType {
signature: Box<AbstractSignature>,
},
Tuple {
row: Box<TypeRow<SerSimpleType>>,
row: Vec<SerSimpleType>,
c: TypeTag,
},
Sum {
row: Box<TypeRow<SerSimpleType>>,
row: Vec<SerSimpleType>,
c: TypeTag,
},
Array {
Expand Down Expand Up @@ -79,15 +81,15 @@ where
fn from(value: Container<T>) -> 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,
},
Expand Down Expand Up @@ -130,19 +132,14 @@ impl From<SimpleType> for SerSimpleType {
}
}

pub(crate) fn box_convert_try<T, F>(value: T) -> Box<F>
where
T: TryInto<F>,
<T as TryInto<F>>::Error: std::fmt::Debug,
{
Box::new((value).try_into().unwrap())
}

pub(crate) fn box_convert<T, F>(value: T) -> Box<F>
where
T: Into<F>,
{
Box::new((value).into())
fn try_convert_list<T: TryInto<T2>, T2: TypeRowElem>(
values: Vec<T>,
) -> Result<TypeRow<T2>, T::Error> {
let vals = values
.into_iter()
.map(T::try_into)
.collect::<Result<Vec<T2>, T::Error>>()?;
Ok(TypeRow::from(vals))
}

macro_rules! handle_container {
Expand All @@ -163,13 +160,13 @@ impl From<SerSimpleType> 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 } => {
Expand Down
2 changes: 0 additions & 2 deletions src/types/type_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Clone + 'static> 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
Expand Down

0 comments on commit 9720b2f

Please sign in to comment.