Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bring back input_extensions serialized field in rust NodeSer #1275

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kept this from the other PR, can remove but is harmless

}
}

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
Loading