Skip to content

Commit

Permalink
refactor: change &NodeType->&OpType conversion into op() accessor (#623)
Browse files Browse the repository at this point in the history
* Remove `impl From<&NodeType> for &OpType`
* Add new NodeType `fn op(&self) -> &OpType`
  • Loading branch information
acl-cqc committed Oct 27, 2023
1 parent 0beb165 commit 0c580eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ pub trait Dataflow: Container {
let const_node = cid.node();
let nodetype = self.hugr().get_nodetype(const_node);
let input_extensions = nodetype.input_extensions().cloned();
let op: &OpType = nodetype.into();
let op: ops::Const = op
let op: ops::Const = nodetype
.op()
.clone()
.try_into()
.expect("ConstID does not refer to Const op.");
Expand Down
5 changes: 3 additions & 2 deletions src/extension/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use super::{ExtensionId, ExtensionSet};
use crate::{
hugr::views::HugrView,
ops::{OpTag, OpTrait, OpType},
ops::{OpTag, OpTrait},
types::EdgeKind,
Direction, Node,
};
Expand Down Expand Up @@ -327,7 +327,7 @@ impl UnificationContext {
// Separate loop so that we can assume that a metavariable has been
// added for every (Node, Direction) in the graph already.
for tgt_node in hugr.nodes() {
let sig: &OpType = hugr.get_nodetype(tgt_node).into();
let sig = hugr.get_nodetype(tgt_node).op();
// Incoming ports with an edge that should mean equal extension reqs
for port in hugr.node_inputs(tgt_node).filter(|src_port| {
matches!(
Expand Down Expand Up @@ -694,6 +694,7 @@ mod test {
use crate::extension::{prelude::PRELUDE_REGISTRY, ExtensionSet};
use crate::hugr::{validate::ValidationError, Hugr, HugrMut, HugrView, NodeType};
use crate::macros::const_extension_ids;
use crate::ops::OpType;
use crate::ops::{self, dataflow::IOTrait, handle::NodeHandle, OpTrait};
use crate::type_row;
use crate::types::{FunctionType, Type, TypeRow};
Expand Down
13 changes: 7 additions & 6 deletions src/hugr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ impl NodeType {
}

impl NodeType {
/// Gets the underlying [OpType] i.e. without any [input_extensions]
///
/// [input_extensions]: NodeType::input_extensions
pub fn op(&self) -> &OpType {
&self.op
}

delegate! {
to self.op {
/// Tag identifying the operation.
Expand All @@ -134,12 +141,6 @@ impl NodeType {
}
}

impl<'a> From<&'a NodeType> for &'a OpType {
fn from(nt: &'a NodeType) -> Self {
&nt.op
}
}

impl OpType {
/// Convert an OpType to a NodeType by giving it some input extensions
pub fn with_extensions(self, rs: ExtensionSet) -> NodeType {
Expand Down

0 comments on commit 0c580eb

Please sign in to comment.