zenoh_flow_derive/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
//
// Copyright (c) 2021 - 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
//! This crate exposes three procedural macros (one for each type of node) to facilitate exposing the symbols required
//! by Zenoh-Flow in order to dynamically load nodes.
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
/// Expose the symbols Zenoh-Flow needs to instantiate and start a Source.
///
/// In addition to exposing a specific symbol that will not be mangled by the compiler, this macro records the version
/// of the rust compiler used as well as the version of Zenoh-Flow. These additional information are here to (try) limit
/// possible surprises due to the lack of stable ABI in Rust.
///
/// ## Example
///
/// ```
/// # use async_trait::async_trait;
/// # use zenoh_flow_nodes::prelude::*;
/// #[export_source]
/// pub struct MySource {
/// // Your logic goes here.
/// }
/// # #[async_trait]
/// # impl Source for MySource{
/// # async fn new(
/// # context: Context,
/// # configuration: Configuration,
/// # outputs: Outputs,
/// # ) -> Result<Self> {
/// # todo!()
/// # }
/// # }
///
/// # #[async_trait]
/// # impl Node for MySource {
/// # async fn iteration(&self) -> Result<()> {
/// # todo!()
/// # }
/// # }
/// ```
#[proc_macro_attribute]
pub fn export_source(_: TokenStream, input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let ident = &ast.ident;
let gen = quote! {
#ast
#[doc(hidden)]
#[no_mangle]
pub static _zf_export_source: zenoh_flow_nodes::NodeDeclaration<
zenoh_flow_nodes::SourceFn,
> = zenoh_flow_nodes::NodeDeclaration::<
zenoh_flow_nodes::SourceFn,
> {
rustc_version: zenoh_flow_nodes::RUSTC_VERSION,
core_version: zenoh_flow_nodes::CORE_VERSION,
constructor: |context: zenoh_flow_nodes::prelude::Context,
configuration: zenoh_flow_nodes::prelude::Configuration,
outputs: zenoh_flow_nodes::prelude::Outputs| {
std::boxed::Box::pin(async {
let node = <#ident>::new(context, configuration, outputs).await?;
Ok(std::sync::Arc::new(node) as std::sync::Arc<dyn zenoh_flow_nodes::prelude::Node>)
})
},
};
};
gen.into()
}
/// Expose the symbols Zenoh-Flow needs to instantiate and start a Sink.
///
/// In addition to exposing a specific symbol that will not be mangled by the compiler, this macro records the version
/// of the rust compiler used as well as the version of Zenoh-Flow. These additional information are here to (try) limit
/// possible surprises due to the lack of stable ABI in Rust.
///
/// ## Example
///
/// ```
/// # use async_trait::async_trait;
/// # use zenoh_flow_nodes::prelude::*;
/// #[export_sink]
/// pub struct MySink {
/// // Your logic goes here.
/// }
/// # #[async_trait]
/// # impl Sink for MySink {
/// # async fn new(
/// # context: Context,
/// # configuration: Configuration,
/// # inputs: Inputs,
/// # ) -> Result<Self> {
/// # todo!()
/// # }
/// # }
///
/// # #[async_trait]
/// # impl Node for MySink {
/// # async fn iteration(&self) -> Result<()> {
/// # todo!()
/// # }
/// # }
/// ```
#[proc_macro_attribute]
pub fn export_sink(_: TokenStream, input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let ident = &ast.ident;
let sink = quote! {#ast};
let constructor = quote! {
#[doc(hidden)]
#[no_mangle]
pub static _zf_export_sink: zenoh_flow_nodes::NodeDeclaration<
zenoh_flow_nodes::SinkFn,
> = zenoh_flow_nodes::NodeDeclaration::<
zenoh_flow_nodes::SinkFn,
> {
rustc_version: zenoh_flow_nodes::RUSTC_VERSION,
core_version: zenoh_flow_nodes::CORE_VERSION,
constructor: |context: zenoh_flow_nodes::prelude::Context,
configuration: zenoh_flow_nodes::prelude::Configuration,
mut inputs: zenoh_flow_nodes::prelude::Inputs| {
std::boxed::Box::pin(async {
let node = <#ident>::new(context, configuration, inputs).await?;
Ok(std::sync::Arc::new(node) as std::sync::Arc<dyn zenoh_flow_nodes::prelude::Node>)
})
},
};
};
let gen = quote! {
#sink
#constructor
};
gen.into()
}
/// Expose the symbols Zenoh-Flow needs to instantiate and start a Operator.
///
/// In addition to exposing a specific symbol that will not be mangled by the compiler, this macro records the version
/// of the rust compiler used as well as the version of Zenoh-Flow. These additional information are here to (try) limit
/// possible surprises due to the lack of stable ABI in Rust.
///
/// ## Example
///
/// ```
/// # use async_trait::async_trait;
/// # use zenoh_flow_nodes::prelude::*;
/// #[export_operator]
/// pub struct MyOperator {
/// // Your logic code goes here.
/// }
/// # #[async_trait]
/// # impl Operator for MyOperator {
/// # async fn new(
/// # context: Context,
/// # configuration: Configuration,
/// # inputs: Inputs,
/// # outputs: Outputs,
/// # ) -> Result<Self>
/// # where
/// # Self: Sized {
/// # todo!()
/// # }
/// # }
///
/// # #[async_trait]
/// # impl Node for MyOperator {
/// # async fn iteration(&self) -> Result<()> {
/// # todo!()
/// # }
/// # }
/// ```
#[proc_macro_attribute]
pub fn export_operator(_: TokenStream, input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let ident = &ast.ident;
let gen = quote! {
#ast
#[doc(hidden)]
#[no_mangle]
pub static _zf_export_operator: zenoh_flow_nodes::NodeDeclaration<
zenoh_flow_nodes::OperatorFn,
> = zenoh_flow_nodes::NodeDeclaration::<
zenoh_flow_nodes::OperatorFn,
> {
rustc_version: zenoh_flow_nodes::RUSTC_VERSION,
core_version: zenoh_flow_nodes::CORE_VERSION,
constructor: |context: zenoh_flow_nodes::prelude::Context,
configuration: zenoh_flow_nodes::prelude::Configuration,
mut inputs: zenoh_flow_nodes::prelude::Inputs,
mut outputs: zenoh_flow_nodes::prelude::Outputs| {
std::boxed::Box::pin(async {
let node = <#ident>::new(context, configuration, inputs, outputs).await?;
Ok(std::sync::Arc::new(node) as std::sync::Arc<dyn zenoh_flow_nodes::prelude::Node>)
})
},
};
};
gen.into()
}