#byte #prost #deserialize #serde #serialization #prpc #add

macro prpc-serde-bytes

A procedural macro to add custom serialization and deserialization to prost bytes

1 unstable release

0.1.0 Apr 18, 2024

#75 in #prost

Download history 19/week @ 2024-11-15 8/week @ 2024-11-22 15/week @ 2024-11-29 56/week @ 2024-12-06 62/week @ 2024-12-13 40/week @ 2024-12-20 9/week @ 2024-12-27 20/week @ 2025-01-03 54/week @ 2025-01-10 17/week @ 2025-01-17 4/week @ 2025-01-24 43/week @ 2025-01-31 20/week @ 2025-02-07 2/week @ 2025-02-14 45/week @ 2025-02-21 35/week @ 2025-02-28

112 downloads per month
Used in prpc

Apache-2.0

6KB
84 lines

prpc_serde_bytes

prpc_serde_bytes is a Rust procedural macro designed to work with structs that utilize both Serde and Prost crate attributes. Specifically, this macro targets struct fields marked with #[prost(bytes = "vec")] and automatically adds #[serde(with = "as_bytes")] to them, enabling custom serialization behavior as specified.

Usage

Add prpc_serde_bytes as a dependency in your Cargo.toml:

[dependencies]
prpc-serde-bytes = "0.1.0"

Example

use prpc_serde_bytes::prpc_serde_bytes;

#[prpc_serde_bytes("::hexed_bytes")]
#[derive(Serialize, Deserialize, prost::Message)]
pub struct Message {
    #[prost(bytes = "vec", tag = "1")]
    pub encoded: ::prost::alloc::vec::Vec<u8>,
    #[prost(uint64, tag = "2")]
    pub timestamp: u64,
}

Which will generate the following code:

#[derive(Serialize, Deserialize, prost::Message)]
pub struct Message {
    #[prost(bytes = "vec", tag = "1")]
    #[serde(with = "::hexed_bytes")]
    pub encoded: ::prost::alloc::vec::Vec<u8>,
    #[prost(uint64, tag = "2")]
    pub timestamp: u64,
}

Dependencies

~200–620KB
~15K SLoC