Skip to content

Commit

Permalink
Add Postcard Codec (#20)
Browse files Browse the repository at this point in the history
Add Postcard Codec gated by feature codec-postcard.
  • Loading branch information
Firaenix authored Jul 14, 2024
1 parent d39539c commit c0ef98d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ jobs:
- bincode
- ciborium
- message-pack
- postcard

steps:
- name: Checkout sources
Expand Down
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,14 @@
"structs",
"unbuffered",
"untrusted"
],
"rust-analyzer.cargo.features": [
"full",
"default-codec-json",
"codec-bincode",
"codec-ciborium",
"codec-json",
"codec-message-pack",
"codec-postcard"
]
}
6 changes: 5 additions & 1 deletion remoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ codec-json = ["serde_json"]
default-codec-json = ["codec-json", "default-codec-set"]
codec-message-pack = ["rmp-serde"]
default-codec-message-pack = ["codec-message-pack", "default-codec-set"]
codec-postcard = ["postcard"]
default-codec-postcard = ["codec-postcard", "default-codec-set"]
full-codecs = [
"codec-bincode",
"codec-ciborium",
"codec-json",
"codec-message-pack",
"codec-postcard",
]


[dependencies]
remoc_macro = { version = "=0.13.0", path = "../remoc_macro", optional = true }

futures = "0.3"
tokio = { version = "1.32", features = ["macros", "rt", "sync", "time"] }
tokio = { version = "1.34", features = ["macros", "rt", "sync", "time"] }
tokio-util = { version = "0.7", features = ["codec"] }
rand = "0.8"
tracing = "0.1.29"
Expand All @@ -58,6 +61,7 @@ serde_json = { version = "1.0", optional = true }
bincode = { version = "1.3", optional = true }
ciborium = { version = "0.2", optional = true }
rmp-serde = { version = "1.0", optional = true }
postcard = { version = "1.0", features = ["use-std"] , optional = true }


[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions remoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The following features enable data formats for transmission:
* `codec-ciborium` provides the CBOR format.
* `codec-json` provides the JSON format.
* `codec-message-pack` provides the MessagePack format.
* `codec-postcard` provides the Postcard format.

The feature `default-codec-*` selects the respective codec as default.
At most one of this must be selected and this should only be used by
Expand Down
8 changes: 8 additions & 0 deletions remoc/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ pub use message_pack::MessagePack;
#[doc(no_inline)]
pub use message_pack::MessagePack as Default;

#[cfg(feature = "codec-postcard")]
mod postcard;
#[cfg(feature = "codec-postcard")]
pub use postcard::Postcard;
#[cfg(feature = "default-codec-postcard")]
#[doc(no_inline)]
pub use postcard::Postcard as Default;

/// Default codec is not set and cannot be used.
///
/// Set one of the crate features `default-codec-*` to define the default codec.
Expand Down
36 changes: 36 additions & 0 deletions remoc/src/codec/postcard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::{Deserialize, Serialize};

use super::{Codec, DeserializationError, SerializationError};

/// Postcard codec.
///
/// See [postcard] for details.
/// This uses the default function configuration.
#[cfg_attr(docsrs, doc(cfg(feature = "codec-postcard")))]
#[derive(Clone, Serialize, Deserialize)]
pub struct Postcard;

impl Codec for Postcard {
#[inline]
fn serialize<Writer, Item>(writer: Writer, item: &Item) -> Result<(), super::SerializationError>
where
Writer: std::io::Write,
Item: serde::Serialize,
{
postcard::to_io(item, writer).map_err(SerializationError::new)?;
Ok(())
}

#[inline]
fn deserialize<Reader, Item>(mut reader: Reader) -> Result<Item, super::DeserializationError>
where
Reader: std::io::Read,
Item: serde::de::DeserializeOwned,
{
let mut bytes = vec![];
reader.read_to_end(&mut bytes).map_err(DeserializationError::new)?;

let item = postcard::from_bytes(&bytes).map_err(DeserializationError::new)?;
Ok(item)
}
}
6 changes: 6 additions & 0 deletions remoc/tests/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ fn json_with_attr() {
fn message_pack() {
roundtrip::<TestStruct, codec::MessagePack>()
}

#[cfg(feature = "codec-postcard")]
#[test]
fn postcard() {
roundtrip::<TestStruct, codec::Postcard>()
}

0 comments on commit c0ef98d

Please sign in to comment.