Trait NodeSerializer

Source
pub trait NodeSerializer {
    // Provided methods
    fn make_self_from_string<'a, T>(json_string: &'a str) -> T
       where T: Sized + Deserialize<'a> { ... }
    fn serialize_node(&self) -> String
       where Self: Sized + Serialize { ... }
}
Expand description

Serializer trait for FBP Nodes

This trait will allow for serializing an FBP node into a JSON string and subsequently take the JSON string from a serialized FBP node and reconstitute the node.

§Example

use serde::{Deserialize, Serialize};
use async_trait::async_trait;
use std::any::Any;
use std::ops::{Deref, DerefMut};

use fbp::fbp_node_context::*;
use fbp::fbp_node_error::*;
use fbp::fbp_iidmessage::*;
use fbp::fbp_node_trait::*;
use fbp::fbp_threadsafe_wrapper::*;


#[derive(Clone, Serialize, Deserialize)]
pub struct ExampleFBPNode {
    data: Box<FBPNodeContext>,
}

#[async_trait]
impl FBPNodeTrait for ExampleFBPNode {

   fn node_data_clone(&self) -> FBPNodeContext {
       self.data.deref().clone()
   }

   fn node_data(&self) -> &FBPNodeContext { &self.data }

   fn node_data_mut(&mut self) -> &mut FBPNodeContext { &mut self.data }

   fn process_message(&mut self,
        msg: IIDMessage) -> Result<IIDMessage, NodeError> { Ok(msg.clone()) }

   fn node_is_configured(&self) -> bool { self.node_data().node_is_configured() }
}

impl NodeSerializer for ExampleFBPNode {}

impl ExampleFBPNode {
    pub fn new() -> Self {
       let result =  ExampleFBPNode {
            data: Box::new(FBPNodeContext::new("ExampleFBPNode")),
        };
     
        result.data.set_node_is_configured(true);
        result.clone().start();
        result
    }
}

let ex_node = ExampleFBPNode::new();
let serialized_pt_node = ex_node.serialize_node();

Provided Methods§

Source

fn make_self_from_string<'a, T>(json_string: &'a str) -> T
where T: Sized + Deserialize<'a>,

This will deserialize a JSON string that is a serialized FBP node back into an FBP Node struct

Source

fn serialize_node(&self) -> String
where Self: Sized + Serialize,

This will take an FBP node and serialize that node into a JSON string

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§