Skip to content

Commit

Permalink
Add postcard as a Serde format
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Apr 11, 2024
1 parent 5463316 commit c1f03cf
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
strategy:
fail-fast: false
matrix:
serde_format: [bincode, cbor, cbor4ii]
serde_format: [bincode, cbor, cbor4ii, postcard]
toolchain: [stable, nightly]
self_ty_in_mod_name: [false, true]

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ The `test-fuzz` package currently supports the following features:

- `serde_cbor4ii` - [CBOR 0x(4+4)9 0x49]

- `serde_postcard` - [Postcard]

## Auto-generated corpus files

`cargo-test-fuzz` can auto-generate values for types that implement certain traits. If all of a target's argument types implement such traits, `cargo-test-fuzz` can auto-generate corpus files for the target.
Expand Down Expand Up @@ -498,6 +500,7 @@ These options are incompatible in the following sense. If a fuzz target's argume
[Limitations]: #limitations
[Macros and Inline Functions Exception]: https://spdx.org/licenses/mif-exception.html
[Overview]: #overview
[Postcard]: https://github.com/jamesmunns/postcard
[Serde CBOR]: https://github.com/pyfisch/cbor
[Serde attributes]: https://serde.rs/attributes.html
[Substrate externalities]: https://substrate.dev/docs/en/knowledgebase/runtime/tests#mock-runtime-storage
Expand Down
2 changes: 2 additions & 0 deletions internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ serde = "1.0"

bincode = "1.3"
cbor4ii = { version = "0.3", features = ["serde1", "use_std"], optional = true }
postcard = { version = "1.0", features = ["use-std"], optional = true }
serde_cbor = { version = "0.11", optional = true }

[features]
__serde_bincode = []
__serde_cbor = ["serde_cbor"]
__serde_cbor4ii = ["cbor4ii"]
__serde_postcard = ["postcard"]

[lints]
workspace = true
Expand Down
3 changes: 2 additions & 1 deletion internal/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ fn main() {
#[cfg(not(any(
feature = "__serde_bincode",
feature = "__serde_cbor",
feature = "__serde_cbor4ii"
feature = "__serde_cbor4ii",
feature = "__postcard"
)))]
println!("cargo:rustc-cfg=serde_default");
}
12 changes: 12 additions & 0 deletions internal/src/serde_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub fn as_feature() -> &'static str {
#[cfg(feature = "__serde_cbor4ii")]
formats.push("serde_cbor4ii");

#[cfg(feature = "__serde_postcard")]
formats.push("serde_postcard");

assert!(
formats.len() <= 1,
"{}",
Expand All @@ -37,6 +40,9 @@ pub const fn serializes_variant_names() -> bool {

#[cfg(feature = "__serde_cbor4ii")]
return true;

#[cfg(feature = "__serde_postcard")]
return false;
}

pub fn serialize<T: Serialize>(args: &T) -> Vec<u8> {
Expand Down Expand Up @@ -64,6 +70,9 @@ pub fn serialize<T: Serialize>(args: &T) -> Vec<u8> {
cbor4ii::serde::to_writer(&mut data, args).unwrap();
data
};

#[cfg(feature = "__serde_postcard")]
return postcard::to_io(args);
}

pub fn deserialize<T: DeserializeOwned, R: Read>(reader: R) -> Option<T> {
Expand All @@ -84,4 +93,7 @@ pub fn deserialize<T: DeserializeOwned, R: Read>(reader: R) -> Option<T> {
let reader = std::io::BufReader::new(reader);
cbor4ii::serde::from_reader(reader).ok()
};

#[cfg(feature = "__serde_postcard")]
return postcard::from_io(reader).ok();
}
1 change: 1 addition & 0 deletions test-fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ self_ty_in_mod_name = ["test-fuzz-macro/__self_ty_in_mod_name"]
serde_bincode = ["internal/__serde_bincode"]
serde_cbor = ["internal/__serde_cbor"]
serde_cbor4ii = ["internal/__serde_cbor4ii"]
serde_postcard = ["internal/__serde_postcard"]
__persistent = ["afl", "test-fuzz-macro/__persistent"]

[lints]
Expand Down
3 changes: 2 additions & 1 deletion test-fuzz/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ fn main() {
#[cfg(not(any(
feature = "serde_bincode",
feature = "serde_cbor",
feature = "serde_cbor4ii"
feature = "serde_cbor4ii",
feature = "serde_postcard"
)))]
println!("cargo:rustc-cfg=serde_default");
}

0 comments on commit c1f03cf

Please sign in to comment.