Skip to content

Commit

Permalink
feat: support for custom (de)serializer (#156)
Browse files Browse the repository at this point in the history
* feat: support for custom (de)serializer

This commit introduces the following changes to Zenoh-Flow:
1. types used within a Zenoh-Flow application do not have to implement the
   (cumbersome) `ZFData` trait; instead,
2. the types used must be `Send + Sync + 'static`,
3. an Output must know how to serialize the provided type `T` and, respectively,
   an Input must know how to deserialize bytes into `T`.

With these changes, any SerDe compatible data serialization format is supported
as well as, for instance, ProtoBuf.

In terms of API, the major difference is how the Input and Output are obtained:

```rust
input: inputs
    .take("in", |bytes| todo!("Provide your deserializer here"))
    .expect("No input called 'in' found"),

output: outputs
    .take("out", |data| todo!("Provide your serializer here"))
    .expect("No output called 'out' found"),
```

* fix: move `DeserializerFn` in types/message.rs

* fix: remove explicit call to `Arc::clone`

* fix: add `as_mut_any` on `SendSyncAny` trait

* fix: implement/derive Debug for the different messages

* feat: InputBuilder and OutputBuilder

Instead of having the methods `take` and `take_raw` on the Inputs and Outputs,
that give, respectively, the Typed and Raw Input/Output, this commit removes
the `take_raw` variant and introduces builders.

The builders can be turned into the Typed or Raw variant using the corresponding
methods: `build_typed` and `build_raw`.

* doc: improve documentation of Input / Output

* feat: reuse buffer when serializing (#160)

* feat: reuse buffer when serializing

This commit tries to minimize the number of allocations performed when
serializing data. For this end, the connector and built-in Source now create
internal `Vec<u8>` buffers that are reused whenever a message is serialized.

This change should hopefully improve performance.

* doc: fix typos

* refacto: renamed `build_raw` and `build_typed` to `raw` and `typed`

---------

Signed-off-by: Julien Loudet <[email protected]>
  • Loading branch information
J-Loudet committed May 2, 2023
1 parent 16c7b35 commit 28d8984
Show file tree
Hide file tree
Showing 16 changed files with 1,274 additions and 1,406 deletions.
30 changes: 0 additions & 30 deletions zenoh-flow-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

/// The `ZFData` derive macro is provided to help the users
/// in implementing the `DowncastAny` trait.
///
/// ## Example
///
/// ```no_compile
/// use zenoh_flow_derive::ZFData;
///
/// #[derive(Debug, Clone, ZFData)]
/// pub struct ZFString(pub String);
/// ```
#[proc_macro_derive(ZFData)]
pub fn zf_data_derive(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let ident = &ast.ident;
let gen = quote! {

impl zenoh_flow::prelude::DowncastAny for #ident {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
}
};
gen.into()
}

/// The `export_source` attribute macro is provided to allow the users
/// in exporting their source.
///
Expand Down
1 change: 1 addition & 0 deletions zenoh-flow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ zrpc-macros = { version= "=0.6.1-alpha.1" }

[dev-dependencies]
tempdir = "0.3.7"
prost = "0.11"

[build-dependencies]
rustc_version = "0.4.0"
Expand Down
Loading

0 comments on commit 28d8984

Please sign in to comment.