Skip to content

Commit

Permalink
[refactor] resolve_extension_ops: take an ExtensionRegistry (#520)
Browse files Browse the repository at this point in the history
This turns out to be pretty trivial, as ExtensionRegistry looks exactly
the same as HashMap as far as this code is concerned. Most of the effort
here is in fixing up imports and doclinks.

This addresses the first (easy) part of #508 but the second remains as
an issue/question for discussion, not attempting to resolve that here.
  • Loading branch information
acl-cqc authored Sep 12, 2023
1 parent 7806540 commit 9ebbe75
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/ops/custom.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Extensible operations.

use smol_str::SmolStr;
use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;

use crate::extension::{ExtensionId, OpDef, SignatureError};
use crate::extension::{ExtensionId, ExtensionRegistry, OpDef, SignatureError};
use crate::hugr::hugrmut::sealed::HugrMutInternals;
use crate::hugr::{HugrView, NodeType};
use crate::types::{type_param::TypeArg, FunctionType, SignatureDescription};
use crate::{Extension, Hugr, Node};
use crate::{Hugr, Node};

use super::tag::OpTag;
use super::{LeafOp, OpName, OpTrait, OpType};
Expand All @@ -19,8 +18,12 @@ use super::{LeafOp, OpName, OpTrait, OpType};
#[serde(into = "OpaqueOp", from = "OpaqueOp")]
pub enum ExternalOp {
/// When we've found (loaded) the [Extension] definition and identified the [OpDef]
///
/// [Extension]: crate::Extension
Extension(ExtensionOp),
/// When we either haven't tried to identify the [Extension] or failed to find it.
///
/// [Extension]: crate::Extension
Opaque(OpaqueOp),
}

Expand Down Expand Up @@ -95,7 +98,9 @@ impl OpTrait for ExternalOp {
}

/// An operation defined by an [OpDef] from a loaded [Extension].
// Note *not* Serializable: container (ExternalOp) is serialized as an OpaqueOp instead.
/// Note *not* Serializable: container ([ExternalOp]) is serialized as an [OpaqueOp] instead.
///
/// [Extension]: crate::Extension
#[derive(Clone, Debug)]
pub struct ExtensionOp {
def: Arc<OpDef>,
Expand Down Expand Up @@ -216,13 +221,13 @@ impl OpaqueOp {
#[allow(dead_code)]
pub fn resolve_extension_ops(
h: &mut Hugr,
extension_registry: &HashMap<SmolStr, Extension>,
extension_registry: &ExtensionRegistry,
) -> Result<(), CustomOpError> {
let mut replacements = Vec::new();
for n in h.nodes() {
if let OpType::LeafOp(LeafOp::CustomOp(op)) = h.get_optype(n) {
if let ExternalOp::Opaque(opaque) = op.as_ref() {
if let Some(r) = extension_registry.get(&*opaque.extension) {
if let Some(r) = extension_registry.get(&opaque.extension) {
// Fail if the Extension was found but did not have the expected operation
let Some(def) = r.get_op(&opaque.op_name) else {
return Err(CustomOpError::OpNotFoundInExtension(
Expand Down

0 comments on commit 9ebbe75

Please sign in to comment.