Skip to content

Commit

Permalink
refacto(BREAKING): move nodes traits/declaration in their own crate
Browse files Browse the repository at this point in the history
**BREAKING**: the macros defined in the crate `zenoh-flow-derive` were updated
to take into account this refactoring. Hence, current code that uses
`zenoh-flow` as a dependency will have their macro `export_xxx` fail at
compilation time.

The docstring withing the `zenoh-flow` crate have been ignored as a result.

-----

All the logic that is related to creating nodes will sit in this crate.

This separate crate will allow users to not have to compile Zenoh-Flow (and even
Zenoh) when compiling their nodes. This should lead to greatly decreased
compilation times.

Several aspects are still work-in-progress:
- the `Context` structure probably needs to be refined,
- documentation is still sparse.

Signed-off-by: Julien Loudet <julien.loudet@zettascale.tech>
  • Loading branch information
J-Loudet committed Oct 23, 2023
1 parent 3e5d3a9 commit 191835f
Show file tree
Hide file tree
Showing 17 changed files with 2,390 additions and 46 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"zenoh-flow-daemon",
"zenoh-flow-derive",
"zenoh-flow-descriptors",
"zenoh-flow-nodes",
"zenoh-flow-plugin",
"zenoh-flow-records",
"zfctl",
Expand Down Expand Up @@ -51,6 +52,7 @@ serde_cbor = "0.11"
serde_derive = "1.0"
serde_json = "1.0"
serde_yaml = "0.9"
tracing = { version = "0.1", features = ["log"] }
uhlc = "0.6"
uuid = { version = "1.1", features = ["serde", "v4"] }
zenoh = { version = "0.7.2-rc" }
Expand Down
11 changes: 0 additions & 11 deletions zenoh-flow-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,9 @@ version.workspace = true
# To build with debug on macros: RUSTFLAGS="-Z macro-backtrace"

[dependencies]
Inflector = "0.11.4"
async-std = { workspace = true, features = ["attributes"] }
darling = "0.20"
futures = { workspace = true }
proc-macro-error = "1.0.4"
proc-macro2 = "1.0"
quote = "1.0"
serde = { workspace = true, features = ["derive"] }
serde_derive = { workspace = true }
syn = { version = "2", features = ["full"] }
syn-serde = { version = "0.3", features = ["json"] }

[dev-dependencies]
env_logger = { workspace = true }

[lib]
proc-macro = true
62 changes: 31 additions & 31 deletions zenoh-flow-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ pub fn export_source(_: TokenStream, input: TokenStream) -> TokenStream {

#[doc(hidden)]
#[no_mangle]
pub static _zf_export_source: zenoh_flow::runtime::dataflow::loader::NodeDeclaration<
zenoh_flow::runtime::dataflow::node::SourceFn,
> = zenoh_flow::runtime::dataflow::loader::NodeDeclaration::<
zenoh_flow::runtime::dataflow::node::SourceFn,
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::runtime::dataflow::loader::RUSTC_VERSION,
core_version: zenoh_flow::runtime::dataflow::loader::CORE_VERSION,
constructor: |context: zenoh_flow::types::Context,
configuration: Option<zenoh_flow::types::Configuration>,
outputs: zenoh_flow::io::Outputs| {
rustc_version: zenoh_flow_nodes::RUSTC_VERSION,
core_version: zenoh_flow_nodes::CORE_VERSION,
constructor: |context: zenoh_flow_nodes::prelude::Context,
configuration: Option<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::traits::Node>)
Ok(std::sync::Arc::new(node) as std::sync::Arc<dyn zenoh_flow_nodes::prelude::Node>)
})
},
};
Expand Down Expand Up @@ -122,19 +122,19 @@ pub fn export_sink(_: TokenStream, input: TokenStream) -> TokenStream {

#[doc(hidden)]
#[no_mangle]
pub static _zf_export_sink: zenoh_flow::runtime::dataflow::loader::NodeDeclaration<
zenoh_flow::runtime::dataflow::node::SinkFn,
> = zenoh_flow::runtime::dataflow::loader::NodeDeclaration::<
zenoh_flow::runtime::dataflow::node::SinkFn,
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::runtime::dataflow::loader::RUSTC_VERSION,
core_version: zenoh_flow::runtime::dataflow::loader::CORE_VERSION,
constructor: |context: zenoh_flow::types::Context,
configuration: Option<zenoh_flow::types::Configuration>,
mut inputs: zenoh_flow::io::Inputs| {
rustc_version: zenoh_flow_nodes::RUSTC_VERSION,
core_version: zenoh_flow_nodes::CORE_VERSION,
constructor: |context: zenoh_flow_nodes::prelude::Context,
configuration: Option<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::traits::Node>)
Ok(std::sync::Arc::new(node) as std::sync::Arc<dyn zenoh_flow_nodes::prelude::Node>)
})
},
};
Expand Down Expand Up @@ -194,20 +194,20 @@ pub fn export_operator(_: TokenStream, input: TokenStream) -> TokenStream {

#[doc(hidden)]
#[no_mangle]
pub static _zf_export_operator: zenoh_flow::runtime::dataflow::loader::NodeDeclaration<
zenoh_flow::runtime::dataflow::node::OperatorFn,
> = zenoh_flow::runtime::dataflow::loader::NodeDeclaration::<
zenoh_flow::runtime::dataflow::node::OperatorFn,
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::runtime::dataflow::loader::RUSTC_VERSION,
core_version: zenoh_flow::runtime::dataflow::loader::CORE_VERSION,
constructor: |context: zenoh_flow::types::Context,
configuration: Option<zenoh_flow::types::Configuration>,
mut inputs: zenoh_flow::io::Inputs,
mut outputs: zenoh_flow::io::Outputs| {
rustc_version: zenoh_flow_nodes::RUSTC_VERSION,
core_version: zenoh_flow_nodes::CORE_VERSION,
constructor: |context: zenoh_flow_nodes::prelude::Context,
configuration: Option<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::traits::Node>)
Ok(std::sync::Arc::new(node) as std::sync::Arc<dyn zenoh_flow_nodes::prelude::Node>)
})
},
};
Expand Down
47 changes: 47 additions & 0 deletions zenoh-flow-nodes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Copyright (c) 2021 - 2023 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>
#

[package]
authors = { workspace = true }
categories = { workspace = true }
description = "Internal crate for Zenoh-Flow."
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
name = "zenoh-flow-nodes"
repository = { workspace = true }
version = { workspace = true }

[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
bincode = { version = "1.3" }
flume = { workspace = true }
futures = { workspace = true }
proc-macro-error = "1.0.4"
proc-macro2 = "1.0"
quote = "1.0"
serde = { workspace = true }
tracing = { workspace = true }
uhlc = { workspace = true }
uuid = { workspace = true }
zenoh-flow-commons = { workspace = true }
zenoh-flow-derive = { path = "../zenoh-flow-derive" }

[dev-dependencies]
prost = "0.12"
serde_json = { workspace = true }

[build-dependencies]
rustc_version = "0.4.0"
18 changes: 18 additions & 0 deletions zenoh-flow-nodes/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright (c) 2021 - 2023 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>
//

fn main() {
let version = rustc_version::version().unwrap();
println!("cargo:rustc-env=RUSTC_VERSION={version}");
}
61 changes: 61 additions & 0 deletions zenoh-flow-nodes/src/declaration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Copyright (c) 2021 - 2023 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>
//

use std::{pin::Pin, sync::Arc};

use futures::Future;
use zenoh_flow_commons::{Configuration, Result};

use crate::{
prelude::{Inputs, Node, Outputs},
Context,
};

/// Constant used to check if a node is compatible with the currently running Zenoh Flow daemon.
/// As nodes are dynamically loaded, this is to prevent (possibly cryptic) runtime error due to
/// incompatible API.
pub static CORE_VERSION: &str = env!("CARGO_PKG_VERSION");
/// Constant used to check if a node was compiled with the same version of the Rust compiler than
/// the currently running Zenoh Flow daemon.
/// As Rust is not ABI stable, this is to prevent (possibly cryptic) runtime errors.
pub static RUSTC_VERSION: &str = env!("RUSTC_VERSION");

/// Declaration expected in the library that will be loaded.
pub struct NodeDeclaration<C> {
pub rustc_version: &'static str,
pub core_version: &'static str,
pub constructor: C,
}

/// `SourceFn` is the only signature we accept to construct a [`Source`](`crate::prelude::Source`).
pub type SourceFn = fn(
Context,
Option<Configuration>,
Outputs,
) -> Pin<Box<dyn Future<Output = Result<Arc<dyn Node>>> + Send>>;

/// `OperatorFn` is the only signature we accept to construct an [`Operator`](`crate::prelude::Operator`).
pub type OperatorFn = fn(
Context,
Option<Configuration>,
Inputs,
Outputs,
) -> Pin<Box<dyn Future<Output = Result<Arc<dyn Node>>> + Send>>;

/// `SinkFn` is the only signature we accept to construct a [`Sink`](`crate::prelude::Sink`).
pub type SinkFn = fn(
Context,
Option<Configuration>,
Inputs,
) -> Pin<Box<dyn Future<Output = Result<Arc<dyn Node>>> + Send>>;
Loading

0 comments on commit 191835f

Please sign in to comment.