diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a6e5847..bf5c3cf 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -121,6 +121,7 @@ jobs: - bincode - ciborium - message-pack + - postcard steps: - name: Checkout sources diff --git a/.vscode/settings.json b/.vscode/settings.json index 67f30f9..f1d4814 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" ] } \ No newline at end of file diff --git a/remoc/Cargo.toml b/remoc/Cargo.toml index 7c3a21a..2727167 100644 --- a/remoc/Cargo.toml +++ b/remoc/Cargo.toml @@ -31,11 +31,14 @@ 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", ] @@ -43,7 +46,7 @@ full-codecs = [ 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" @@ -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] diff --git a/remoc/README.md b/remoc/README.md index 0d66a34..bbd5531 100644 --- a/remoc/README.md +++ b/remoc/README.md @@ -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 diff --git a/remoc/src/codec/mod.rs b/remoc/src/codec/mod.rs index 8987170..4804c16 100644 --- a/remoc/src/codec/mod.rs +++ b/remoc/src/codec/mod.rs @@ -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. diff --git a/remoc/src/codec/postcard.rs b/remoc/src/codec/postcard.rs new file mode 100644 index 0000000..53a22f5 --- /dev/null +++ b/remoc/src/codec/postcard.rs @@ -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: 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(mut reader: Reader) -> Result + 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) + } +} diff --git a/remoc/tests/codec/mod.rs b/remoc/tests/codec/mod.rs index 0a36e54..94419cd 100644 --- a/remoc/tests/codec/mod.rs +++ b/remoc/tests/codec/mod.rs @@ -110,3 +110,9 @@ fn json_with_attr() { fn message_pack() { roundtrip::() } + +#[cfg(feature = "codec-postcard")] +#[test] +fn postcard() { + roundtrip::() +}