Trait CustomOperationBody

Source
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§

Source

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 created
  • arguments_types - vector of input types of a custom operation
§Returns

New graph computing a custom operation

Source

fn get_name(&self) -> String

Specifies and returns the name of this custom operation.

The name must be unique among all the implemented custom operations.

§Returns

Name of this custom operation

Trait Implementations§

Source§

impl<'typetag> Serialize for dyn CustomOperationBody + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn CustomOperationBody + Send + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn CustomOperationBody + Send + Sync + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn CustomOperationBody + Sync + 'typetag

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Implementors§