use std::any::Any;
use std::fmt;
use crate::runtime::BlockId;
use crate::runtime::Error;
use crate::runtime::FlowgraphMessage;
use crate::runtime::PortIndex;
use crate::runtime::PortName;
use crate::runtime::Result;
use crate::runtime::block_inbox::BlockEndpoint;
use crate::runtime::block_inbox::BlockInboxReader;
use crate::runtime::block_inbox::LocalBlockInbox;
use crate::runtime::buffer::DynBufferReader;
use crate::runtime::buffer::DynBufferWriter;
use crate::runtime::channel::mpsc::Sender;
pub(crate) trait BlockObject: Any {
fn inbox(&self) -> BlockEndpoint;
fn id(&self) -> BlockId;
fn type_name(&self) -> &str;
fn instance_name(&self) -> Option<&str>;
fn is_blocking(&self) -> bool;
fn stream_input_at(&mut self, index: PortIndex)
-> Option<(PortName, &mut dyn DynBufferReader)>;
fn stream_output_at(
&mut self,
index: PortIndex,
) -> Option<(PortName, &mut dyn DynBufferWriter)>;
fn message_inputs(&self) -> &'static [&'static str];
fn message_outputs(&self) -> &'static [&'static str];
fn connect_message(
&mut self,
src_port: PortIndex,
dst: BlockEndpoint,
dst_port: PortIndex,
) -> Result<(), Error>;
}
#[async_trait::async_trait]
pub(crate) trait Block: BlockObject + Send {
async fn run(&mut self, main_inbox: Sender<FlowgraphMessage>);
}
impl fmt::Debug for dyn Block {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Block")
.field("type_name", &self.type_name().to_string())
.finish()
}
}
#[async_trait::async_trait(?Send)]
pub(crate) trait LocalBlock: BlockObject {
fn local_inbox(&self) -> LocalBlockInbox;
fn take_external_inbox_reader(&mut self) -> Option<BlockInboxReader>;
async fn run(&mut self, main_inbox: Sender<FlowgraphMessage>);
}
impl fmt::Debug for dyn LocalBlock {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LocalBlock")
.field("type_name", &self.type_name().to_string())
.finish()
}
}