pub trait CustomOperationBody:
'static
+ Debug
+ DynEqHash
+ Send
+ Sync
+ Serialize
+ Deserialize {
// Required methods
fn instantiate(
&self,
context: Context,
arguments_types: Vec<Type>,
) -> Result<Graph>;
fn get_name(&self) -> String;
}
Expand description
A trait that must be implemented by any custom operation struct.
Only structures satisfying this trait can be used to create CustomOperation.
Any structure implementing this trait must also implement the following traits:
§Example
This is the actual implementation of the custom operation Not.
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
pub struct Not {}
#[typetag::serde] // requires the typetag crate
impl CustomOperationBody for Not {
fn instantiate(&self, context: Context, arguments_types: Vec<Type>) -> Result<Graph> {
if arguments_types.len() != 1 {
return Err(runtime_error!("Invalid number of arguments for Not"));
}
let g = context.create_graph()?;
g.input(arguments_types[0].clone())?
.add(g.constant(scalar_type(BIT), Value::from_scalar(1, BIT)?)?)?
.set_as_output()?;
g.finalize()?;
Ok(g)
}
fn get_name(&self) -> String {
"Not".to_owned()
}
}
Required Methods§
Sourcefn instantiate(
&self,
context: Context,
arguments_types: Vec<Type>,
) -> Result<Graph>
fn instantiate( &self, context: Context, arguments_types: Vec<Type>, ) -> Result<Graph>
Defines the logic of a custom operation.
This function must create a graph in a given context computing a custom operation. Note that that the number of inputs and their types can vary. This function should describe the logic of the custom operation for all acceptable cases and return an error otherwise.
§Arguments
context
- context where a graph computing a custom operation should be createdarguments_types
- vector of input types of a custom operation
§Returns
New graph computing a custom operation