Trait zenoh_flow_nodes::prelude::Source
source · pub trait Source: Node + Send + Sync {
// Required method
fn new<'async_trait>(
context: Context,
configuration: Configuration,
outputs: Outputs
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where Self: Sized + 'async_trait;
}Expand description
A Source feeds data into a data flow.
A Source only possesses Output (either typed or raw) as
it does not receive any data from upstream nodes but from “outside” the data flow.
A structure implementing the Source trait typically needs to keep a reference to the Output.
§Example
use async_trait::async_trait;
use zenoh_flow_nodes::prelude::*;
// Use our provided macro to expose the symbol that Zenoh-Flow will look for when it will load
// the shared library.
#[export_source]
pub struct MySource {
output: Output<usize>,
// The state could go in such structure.
// state: Arc<Mutex<State>>,
}
#[async_trait::async_trait]
impl Source for MySource {
async fn new(
_context: Context,
_configuration: Configuration,
mut outputs: Outputs,
) -> Result<Self> {
let output = outputs
.take("out")
.expect("No output called 'out' found")
.typed(|buffer, data| todo!("Provide your serialiser here"));
Ok(Self { output })
}
}
#[async_trait::async_trait]
impl Node for MySource {
async fn iteration(&self) -> Result<()> {
// To mutate the state, first lock it.
//
// let state = self.state.lock().await;
//
// The state is a way for the Source to read information from the external world, i.e., interacting with
// I/O devices.
self.output.send(10usize, None).await
}
}Required Methods§
sourcefn new<'async_trait>(
context: Context,
configuration: Configuration,
outputs: Outputs
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
Self: Sized + 'async_trait,
fn new<'async_trait>(
context: Context,
configuration: Configuration,
outputs: Outputs
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
Self: Sized + 'async_trait,
For a Context, a Configuration and a set of Outputs, produce a new Source.
Sources only possess Outputs as their purpose is to fetch data from the external world.