Skip to content

Commit

Permalink
make parent ResourceID option
Browse files Browse the repository at this point in the history
None flags "not attached to resource yet"
  • Loading branch information
ss2165 committed Jul 28, 2023
1 parent 4025fb3 commit 49b0c27
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/extensions/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::collections::HashMap;
use pyo3::prelude::*;

use crate::ops::constant::CustomConst;
use crate::resource::{OpDef, ResourceId, ResourceSet, TypeDef};
use crate::resource::{OpDef, ResourceSet, TypeDef};
use crate::types::type_param::TypeArg;
use crate::types::{CustomType, SimpleRow, SimpleType};
use crate::Resource;
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Type {
name: self.name(),
params: vec![],
description: self.description().to_string(),
resource: ResourceId::default(),
resource: None,
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/ops/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ impl From<ExternalOp> for LeafOp {
impl OpName for ExternalOp {
fn name(&self) -> SmolStr {
let (res_id, op_name) = match self {
Self::Opaque(op) => (&op.resource, &op.op_name),
Self::Resource(ResourceOp { def, .. }) => (&def.resource, &def.name),
Self::Opaque(op) => (op.resource.clone(), &op.op_name),
Self::Resource(ResourceOp { def, .. }) => {
(def.resource.clone().unwrap_or_default(), &def.name)
}
};
qualify_name(res_id, op_name)
}
Expand Down Expand Up @@ -136,7 +138,7 @@ impl From<ResourceOp> for OpaqueOp {
None
};
OpaqueOp {
resource: def.resource.clone(),
resource: def.resource.clone().unwrap_or_default(),
op_name: def.name.clone(),
description: def.description.clone(),
args,
Expand All @@ -163,7 +165,7 @@ pub struct OpaqueOp {
signature: Option<Signature>,
}

fn qualify_name(res_id: &ResourceId, op_name: &SmolStr) -> SmolStr {
fn qualify_name(res_id: ResourceId, op_name: &SmolStr) -> SmolStr {
format!("{}.{}", res_id, op_name).into()
}

Expand Down
27 changes: 16 additions & 11 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ impl Debug for LowerFunc {
/// TODO: Define a way to construct new OpDef's from a serialized definition.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct OpDef {
/// The unique Resource owning this OpDef (of which this OpDef is a member)
pub resource: ResourceId,
/// The unique Resource, if any, owning this OpDef (of which this OpDef is a member)
pub resource: Option<ResourceId>,
/// Unique identifier of the operation. Used to look up OpDefs in the registry
/// when deserializing nodes (which store only the name).
pub name: SmolStr,
Expand Down Expand Up @@ -232,7 +232,11 @@ impl OpDef {
}
SignatureFunc::CustomFunc(bf) => bf.compute_signature(&self.name, args, &self.misc)?,
};
assert!(res.contains(&self.resource));
let resource = self
.resource
.as_ref()
.expect("OpDef does not belong to a Resource.");
assert!(res.contains(resource));
let mut sig = Signature::new_df(ins, outs);
sig.input_resources = resources_in.clone();
sig.output_resources = res.union(resources_in); // Pass input requirements through
Expand Down Expand Up @@ -296,8 +300,8 @@ pub struct TypeDef {
///
/// [`TypeArg`]: crate::types::type_param::TypeArg
pub params: Vec<TypeParam>,
/// The unique Resource owning this TypeDef (of which this TypeDef is a member)
pub resource: ResourceId,
/// The unique Resource, if any, owning this TypeDef (of which this TypeDef is a member)
pub resource: Option<ResourceId>,
/// Human readable description of the type definition.
pub description: String,
}
Expand Down Expand Up @@ -348,16 +352,17 @@ impl Resource {
}

/// Add an exported type to the resource.
pub fn add_type(&mut self, ty: TypeDef) -> Result<(), String> {
if ty.resource != ResourceId::default() {
pub fn add_type(&mut self, mut ty: TypeDef) -> Result<(), String> {
if let Some(resource) = ty.resource {
return Err(format!(
"TypeDef {} owned by another resource {}",
ty.name, ty.resource
ty.name, resource
));
}
match self.types.entry(ty.name.clone()) {
Entry::Occupied(_) => panic!("Resource already has a type called {}", &ty.name),
Entry::Vacant(ve) => {
ty.resource = Some(self.name.clone());
ve.insert(ty);
}
}
Expand All @@ -366,16 +371,16 @@ impl Resource {

/// Add an operation definition to the resource.
pub fn add_op(&mut self, mut op: OpDef) -> Result<(), String> {
if op.resource != ResourceId::default() {
if let Some(resource) = op.resource {
return Err(format!(
"OpDef {} owned by another resource {}",
op.name, op.resource
op.name, resource
));
}
match self.operations.entry(op.name.clone()) {
Entry::Occupied(_) => panic!("Resource already has an op called {}", &op.name),
Entry::Vacant(ve) => {
op.resource = self.name.clone();
op.resource = Some(self.name.clone());
ve.insert(Arc::new(op));
}
}
Expand Down

0 comments on commit 49b0c27

Please sign in to comment.