Skip to content

Commit

Permalink
Derive DataflowOpTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Dec 1, 2023
1 parent 8ebfcc3 commit daa6671
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
51 changes: 32 additions & 19 deletions src/ops/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,6 @@ impl ExternalOp {
qualify_name(res_id, op_name)
}

/// A description of the external op.
pub fn description(&self) -> &str {
match self {
Self::Opaque(op) => op.description.as_str(),
Self::Extension(ext_op) => DataflowOpTrait::description(ext_op),
}
}

/// Note the case of an OpaqueOp without a signature should already
/// have been detected in [resolve_extension_ops]
pub fn dataflow_signature(&self) -> FunctionType {
match self {
Self::Opaque(op) => op.signature.clone(),
Self::Extension(ext_op) => ext_op.signature(),
}
}

/// Downgrades this ExternalOp into an OpaqueOp
pub fn as_opaque(self) -> OpaqueOp {
match self {
Expand Down Expand Up @@ -100,6 +83,24 @@ impl From<ExternalOp> for OpType {
}
}

impl DataflowOpTrait for ExternalOp {
const TAG: OpTag = OpTag::Leaf;

fn description(&self) -> &str {
match self {
Self::Opaque(op) => DataflowOpTrait::description(op),
Self::Extension(ext_op) => DataflowOpTrait::description(ext_op),
}
}

fn signature(&self) -> FunctionType {
match self {
Self::Opaque(op) => op.signature.clone(),
Self::Extension(ext_op) => ext_op.signature(),
}
}
}

/// An operation defined by an [OpDef] from a loaded [Extension].
/// Note *not* Serializable: container ([ExternalOp]) is serialized as an [OpaqueOp] instead.
///
Expand Down Expand Up @@ -251,6 +252,18 @@ impl From<OpaqueOp> for OpType {
}
}

impl DataflowOpTrait for OpaqueOp {
const TAG: OpTag = OpTag::Leaf;

fn description(&self) -> &str {
&self.description
}

fn signature(&self) -> FunctionType {
self.signature.clone()
}
}

/// Resolve serialized names of operations into concrete implementation (OpDefs) where possible
#[allow(dead_code)]
pub fn resolve_extension_ops(
Expand Down Expand Up @@ -352,8 +365,8 @@ mod test {
);
let op: ExternalOp = op.into();
assert_eq!(op.name(), "res.op");
assert_eq!(op.description(), "desc");
assert_eq!(DataflowOpTrait::description(&op), "desc");
assert_eq!(op.args(), &[TypeArg::Type { ty: USIZE_T }]);
assert_eq!(op.dataflow_signature(), sig);
assert_eq!(op.signature(), sig);
}
}
2 changes: 1 addition & 1 deletion src/ops/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl DataflowOpTrait for LeafOp {

match self {
LeafOp::Noop { ty: typ } => FunctionType::new(vec![typ.clone()], vec![typ.clone()]),
LeafOp::CustomOp(ext) => ext.dataflow_signature(),
LeafOp::CustomOp(ext) => ext.signature(),
LeafOp::MakeTuple { tys: types } => {
FunctionType::new(types.clone(), vec![Type::new_tuple(types.clone())])
}
Expand Down

0 comments on commit daa6671

Please sign in to comment.