Skip to content

Commit fb5a561

Browse files
authored
feat: Add ExtensionOp helpers (#2072)
- We had `OpType::cast` but were missing it in `ExtensionOp` itself. - Adds a `ExtensionOp::extension_id` call
1 parent 7626797 commit fb5a561

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

hugr-core/src/ops.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ impl OpType {
301301
}
302302

303303
/// Cast to an extension operation.
304+
///
305+
/// Returns `None` if the operation is not of the requested type.
304306
pub fn cast<T: MakeExtensionOp>(&self) -> Option<T> {
305-
self.as_extension_op()
306-
.and_then(|o| T::from_extension_op(o).ok())
307+
self.as_extension_op().and_then(ExtensionOp::cast)
307308
}
308309

309310
/// Returns the extension where the operation is defined, if any.

hugr-core/src/ops/custom.rs

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use {
1212
::proptest_derive::Arbitrary,
1313
};
1414

15+
use crate::extension::simple_op::MakeExtensionOp;
1516
use crate::extension::{ConstFoldResult, ExtensionId, OpDef, SignatureError};
1617
use crate::types::{type_param::TypeArg, Signature};
1718
use crate::{ops, IncomingPort, Node};
@@ -123,6 +124,18 @@ impl ExtensionOp {
123124
pub(crate) fn args_mut(&mut self) -> &mut [TypeArg] {
124125
self.args.as_mut_slice()
125126
}
127+
128+
/// Cast the operation to an specific extension op.
129+
///
130+
/// Returns `None` if the operation is not of the requested type.
131+
pub fn cast<T: MakeExtensionOp>(&self) -> Option<T> {
132+
T::from_extension_op(self).ok()
133+
}
134+
135+
/// Returns the extension id of the operation.
136+
pub fn extension_id(&self) -> &ExtensionId {
137+
self.def.extension_id()
138+
}
126139
}
127140

128141
impl From<ExtensionOp> for OpaqueOp {

0 commit comments

Comments
 (0)