From 2cc956f1e9835bf7378653eef7cbe7101affa7b5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 4 Dec 2021 10:46:57 +0100 Subject: [PATCH] Add bincode serde support Signed-off-by: trivernis --- .github/workflows/bench.yml | 2 +- .github/workflows/build.yml | 8 +++++- .github/workflows/publish.yml | 2 +- Cargo.lock | 10 +++++++ Cargo.toml | 2 ++ src/events/payload_serializer/mod.rs | 7 +++++ .../payload_serializer/serialize_bincode.rs | 28 +++++++++++++++++++ src/lib.rs | 8 ++++++ 8 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/events/payload_serializer/serialize_bincode.rs diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 59e75b7a..5c280608 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -31,7 +31,7 @@ jobs: id: extract_branch - name: Run benchmark - run: cargo bench -- --save-baseline ${{steps.extract_branch.outputs.branch}} + run: cargo bench --feature serialize_rmp -- --save-baseline ${{steps.extract_branch.outputs.branch}} - name: upload artifact uses: actions/upload-artifact@v2 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8862f890..35a258c9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,4 +33,10 @@ jobs: run: cargo build --verbose - name: Run tests - run: cargo test --verbose \ No newline at end of file + run: cargo test --verbose --tests + + - name: Run rmp serialization tests + run: cargo test --verbose --all --features serialize_rmp + + - name: Run bincode serialization tests + run: cargo test --verbose --all --features serialize_bincode \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 5cfb5ebb..b91e1064 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -23,4 +23,4 @@ jobs: run: cargo login "$CRATES_IO_TOKEN" - name: Publish to crates.io - run: cargo publish \ No newline at end of file + run: cargo publish --all-features \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 60988940..ac3bb334 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -41,6 +50,7 @@ name = "bromine" version = "0.14.0" dependencies = [ "async-trait", + "bincode", "byteorder", "criterion", "crossbeam-utils", diff --git a/Cargo.toml b/Cargo.toml index 3635b0df..79c27e22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ byteorder = "1.4.3" async-trait = "0.1.51" futures = "0.3.17" rmp-serde = {version = "0.15.5", optional = true} +bincode = {version = "1.3.3", optional = true} [dependencies.serde] optional = true @@ -58,3 +59,4 @@ features = ["macros", "rt-multi-thread"] default = [] serialize = ["serde"] serialize_rmp = ["serialize", "rmp-serde"] +serialize_bincode = ["serialize", "bincode"] \ No newline at end of file diff --git a/src/events/payload_serializer/mod.rs b/src/events/payload_serializer/mod.rs index 7989f2f4..3ea45dd0 100644 --- a/src/events/payload_serializer/mod.rs +++ b/src/events/payload_serializer/mod.rs @@ -1,4 +1,11 @@ #[cfg(feature = "serialize_rmp")] pub mod serialize_rmp; + #[cfg(feature = "serialize_rmp")] pub use serialize_rmp::*; + +#[cfg(feature = "serialize_bincode")] +mod serialize_bincode; + +#[cfg(feature = "serialize_bincode")] +pub use serialize_bincode::*; \ No newline at end of file diff --git a/src/events/payload_serializer/serialize_bincode.rs b/src/events/payload_serializer/serialize_bincode.rs new file mode 100644 index 00000000..c6cc9c16 --- /dev/null +++ b/src/events/payload_serializer/serialize_bincode.rs @@ -0,0 +1,28 @@ +use crate::payload::{EventReceivePayload, EventSendPayload}; +use crate::prelude::IPCResult; +use serde::de::DeserializeOwned; +use serde::Serialize; +use std::io::Read; + +pub type SerializationError = bincode::Error; + +impl EventSendPayload for T +where + T: Serialize, +{ + fn to_payload_bytes(self) -> IPCResult> { + let bytes = bincode::serialize(&self)?; + + Ok(bytes) + } +} + +impl EventReceivePayload for T +where + T: DeserializeOwned, +{ + fn from_payload_bytes(reader: R) -> IPCResult { + let type_data = bincode::deserialize_from(reader)?; + Ok(type_data) + } +} diff --git a/src/lib.rs b/src/lib.rs index 1491b27a..3175ca4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,14 @@ //! # } //! ``` +#[cfg(all(feature = "serialize", not(any(feature = "serialize_bincode", feature = "serialize_rmp"))))] +compile_error!("Feature 'serialize' cannot be used by its own. Choose one of 'serialize_rmp', 'serialize_rmp' instead."); + +#[cfg(all(feature = "serialize_rmp", feature = "serialize_bincode"))] +compile_error!( + "Feature 'serialize_rmp' and 'serialize_bincode' cannot be enabled at the same time" +); + pub mod error; mod events; pub mod ipc;