Skip to content

Commit

Permalink
Add a blanket implementation for CustomSigFn (#259)
Browse files Browse the repository at this point in the history
* Add a blanket implementation for CustomSigFn

* Use a closure in the rotation resource def

This should appease the coverage checker

* Re-add name and misc parameters
  • Loading branch information
aborgna-q authored Jul 12, 2023
1 parent 9ebe269 commit f196546
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
46 changes: 15 additions & 31 deletions src/extensions/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use std::collections::HashMap;
use pyo3::prelude::*;

use crate::ops::constant::CustomConst;
use crate::resource::{CustomSignatureFunc, OpDef, ResourceSet, SignatureError, TypeDef};
use crate::types::{type_param::TypeArg, ClassicType, CustomType, SimpleType, TypeRow};
use crate::resource::{OpDef, ResourceSet, TypeDef};
use crate::types::type_param::TypeArg;
use crate::types::{ClassicType, CustomType, SimpleType, TypeRow};
use crate::Resource;

pub const fn resource_id() -> SmolStr {
Expand All @@ -27,15 +28,18 @@ pub fn resource() -> Resource {
resource.add_type(Type::Angle.type_def());
resource.add_type(Type::Quaternion.type_def());

resource
.add_op(OpDef::new_with_custom_sig(
"AngleAdd".into(),
"".into(),
vec![],
HashMap::default(),
AngleAdd,
))
.unwrap();
let op = OpDef::new_with_custom_sig(
"AngleAdd".into(),
"".into(),
vec![],
HashMap::default(),
|_arg_values: &[TypeArg]| {
let t: TypeRow = vec![SimpleType::Classic(Type::Angle.custom_type().into())].into();
Ok((t.clone(), t, ResourceSet::default()))
},
);

resource.add_op(op).unwrap();
resource
}

Expand Down Expand Up @@ -98,26 +102,6 @@ impl CustomConst for Constant {
}
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AngleAdd;

/// When we have a YAML type-scheme interpreter, we'll be able to use that;
/// there is no need for a binary compute_signature for a case this simple.
impl CustomSignatureFunc for AngleAdd {
fn compute_signature(
&self,
_name: &SmolStr,
_arg_values: &[TypeArg],
_misc: &HashMap<String, serde_yaml::Value>,
) -> Result<(TypeRow, TypeRow, ResourceSet), SignatureError> {
let t: TypeRow = vec![SimpleType::Classic(
Into::<CustomType>::into(Type::Angle).into(),
)]
.into();
Ok((t.clone(), t, ResourceSet::default()))
}
}

//
// TODO:
//
Expand Down
14 changes: 14 additions & 0 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ pub trait CustomSignatureFunc: Send + Sync {
}
}

impl<F> CustomSignatureFunc for F
where
F: Fn(&[TypeArg]) -> Result<(TypeRow, TypeRow, ResourceSet), SignatureError> + Send + Sync,
{
fn compute_signature(
&self,
_name: &SmolStr,
arg_values: &[TypeArg],
_misc: &HashMap<String, serde_yaml::Value>,
) -> Result<(TypeRow, TypeRow, ResourceSet), SignatureError> {
self(arg_values)
}
}

/// An error that can occur in computing the signature of a node.
/// TODO: decide on failure modes
#[derive(Debug, Clone, Error, PartialEq, Eq)]
Expand Down

0 comments on commit f196546

Please sign in to comment.