Skip to content

Commit

Permalink
fix: bring back input_extensions serialized field in rust NodeSer (#1275
Browse files Browse the repository at this point in the history
)

(skip serializing, deserialize to None)

Closes #1270
  • Loading branch information
ss2165 authored Jul 8, 2024
1 parent 75192f7 commit bbff0c9
Show file tree
Hide file tree
Showing 7 changed files with 1,180 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hugr-core/src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct Hugr {

impl Default for Hugr {
fn default() -> Self {
Self::new(crate::ops::Module)
Self::new(crate::ops::Module::new())
}
}

Expand Down
5 changes: 5 additions & 0 deletions hugr-core/src/hugr/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct NodeSer {
parent: Node,
#[serde(flatten)]
op: OpType,

#[serde(skip_serializing, default)]
input_extensions: Option<crate::extension::ExtensionSet>,
}

/// Version 1 of the HUGR serialization format.
Expand Down Expand Up @@ -146,6 +149,7 @@ impl TryFrom<&Hugr> for SerHugrV1 {
nodes[new_node] = Some(NodeSer {
parent,
op: opt.clone(),
input_extensions: None,
});
metadata[new_node].clone_from(hugr.metadata.get(n.pg_index()));
}
Expand Down Expand Up @@ -203,6 +207,7 @@ impl TryFrom<SerHugrV1> for Hugr {
let NodeSer {
parent: root_parent,
op: root_type,
..
} = nodes.next().unwrap();
if root_parent.index() != 0 {
return Err(HUGRSerializationError::FirstNodeNotRoot(root_parent));
Expand Down
21 changes: 18 additions & 3 deletions hugr-core/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn gen_optype(g: &MultiPortGraph, node: portgraph::NodeIndex) -> OpType {
.into(),
(true, false) => Input::new(vec![NAT; outputs - 1]).into(),
(false, true) => Output::new(vec![NAT; inputs - 1]).into(),
(true, true) => Module.into(),
(true, true) => Module::new().into(),
}
}

Expand Down Expand Up @@ -451,7 +451,7 @@ fn roundtrip_polyfunctype(#[case] poly_func_type: PolyFuncType) {
}

#[rstest]
#[case(ops::Module)]
#[case(ops::Module::new())]
#[case(ops::FuncDefn { name: "polyfunc1".into(), signature: polyfunctype1()})]
#[case(ops::FuncDecl { name: "polyfunc2".into(), signature: polyfunctype1()})]
#[case(ops::AliasDefn { name: "aliasdefn".into(), definition: Type::new_unit_sum(4)})]
Expand All @@ -466,9 +466,20 @@ fn roundtrip_optype(#[case] optype: impl Into<OpType> + std::fmt::Debug) {
check_testing_roundtrip(NodeSer {
parent: portgraph::NodeIndex::new(0).into(),
op: optype.into(),
input_extensions: None,
});
}

#[test]
/// issue 1270
fn input_extensions_deser() {
// load a file serialised with `input_extensions` fields on all ops
let _: Hugr = serde_json::from_reader(std::io::BufReader::new(
std::fs::File::open(crate::test_file!("issue-1270.json")).unwrap(),
))
.unwrap();
}

mod proptest {
use super::check_testing_roundtrip;
use super::{NodeSer, SimpleOpDef};
Expand All @@ -484,7 +495,11 @@ mod proptest {
(0..i32::MAX as usize).prop_map(|x| portgraph::NodeIndex::new(x).into()),
any::<OpType>(),
)
.prop_map(|(parent, op)| NodeSer { parent, op })
.prop_map(|(parent, op)| NodeSer {
parent,
op,
input_extensions: None,
})
.boxed()
}
}
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/hugr/validate/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn invalid_root() {
assert_eq!(b.validate(&EMPTY_REG), Ok(()));

// Add another hierarchy root
let other = b.add_node(ops::Module.into());
let other = b.add_node(ops::Module::new().into());
assert_matches!(
b.validate(&EMPTY_REG),
Err(ValidationError::NoParent { node }) => assert_eq!(node, other)
Expand Down
2 changes: 1 addition & 1 deletion hugr-core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl_op_ref_try_into!(Conditional);
impl_op_ref_try_into!(Case);

/// The default OpType (as returned by [Default::default])
pub const DEFAULT_OPTYPE: OpType = OpType::Module(Module);
pub const DEFAULT_OPTYPE: OpType = OpType::Module(Module::new());

impl Default for OpType {
fn default() -> Self {
Expand Down
8 changes: 7 additions & 1 deletion hugr-core/src/ops/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ use super::StaticTag;
use super::{impl_op_name, OpTag, OpTrait};

/// The root of a module, parent of all other `OpType`s.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[cfg_attr(test, derive(Arbitrary))]
pub struct Module;
impl Module {
/// Construct a new Module.
pub const fn new() -> Self {
Self
}
}

impl_op_name!(Module);

Expand Down
Loading

0 comments on commit bbff0c9

Please sign in to comment.