Skip to content

Commit

Permalink
feat: is_ext utility for checking equality against extension ops
Browse files Browse the repository at this point in the history
Open to bikeshedding on name of `is_ext`
  • Loading branch information
ss2165 committed Sep 2, 2024
1 parent a38e58c commit 5e9d7e1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 1 addition & 2 deletions hugr-core/src/extension/prelude/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ mod tests {
};
let op = def.to_concrete(ty, size);
let optype: OpType = op.clone().into();
let new_op: ArrayOp = optype.cast().unwrap();
assert_eq!(new_op, op);
assert!(optype.is_ext(op));
}
}

Expand Down
4 changes: 1 addition & 3 deletions hugr-core/src/hugr/rewrite/insert_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ mod tests {

assert_eq!(h.node_count(), 7);

let noop: Noop = h.get_optype(noop_node).cast().unwrap();

assert_eq!(noop, Noop(QB_T));
assert_eq!(h.get_optype(noop_node).cast(), Some(Noop(QB_T)));

h.update_validate(&PRELUDE_REGISTRY).unwrap();
}
Expand Down
8 changes: 8 additions & 0 deletions hugr-core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ impl OpType {
self.as_extension_op()
.and_then(|o| T::from_extension_op(o).ok())
}

/// Check if the operation is an extension operation by casting to it.
pub fn is_ext<T>(&self, op: T) -> bool
where
T: MakeExtensionOp + PartialEq,
{
self.cast::<T>() == Some(op)
}
}

/// Macro used by operations that want their
Expand Down

0 comments on commit 5e9d7e1

Please sign in to comment.