From 69f4396301b7a67b0b01346720a58306eb3f3895 Mon Sep 17 00:00:00 2001 From: Ggiggle <47661277+Ggiggle@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:20:27 +0800 Subject: [PATCH] fix: add the default value len when turn on the encode default value feature (#276) --- Cargo.lock | 2 +- examples/src/lib.rs | 5 ++++- pilota/Cargo.toml | 2 +- pilota/src/prost/encoding.rs | 10 ++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b88bfb71..339475b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -821,7 +821,7 @@ dependencies = [ [[package]] name = "pilota" -version = "0.11.5" +version = "0.11.6" dependencies = [ "ahash", "anyhow", diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 3deba319..a3a59f90 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -29,5 +29,8 @@ fn test_pb_encode_zero_value() { a.encode(&mut buf).unwrap(); println!("{:?}", buf); - println!("{:?}", buf.freeze().as_ref()); + // println!("{:?}", buf.freeze().as_ref()); + + let parsed_a = zero_value::zero_value::A::decode(buf).unwrap(); + println!("{:?}", parsed_a); } diff --git a/pilota/Cargo.toml b/pilota/Cargo.toml index fc644d86..37430e6d 100644 --- a/pilota/Cargo.toml +++ b/pilota/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pilota" -version = "0.11.5" +version = "0.11.6" edition = "2021" description = "Pilota is a thrift and protobuf implementation in pure rust with high performance and extensibility." documentation = "https://docs.rs/pilota" diff --git a/pilota/src/prost/encoding.rs b/pilota/src/prost/encoding.rs index 88c6447c..393b509f 100644 --- a/pilota/src/prost/encoding.rs +++ b/pilota/src/prost/encoding.rs @@ -1658,15 +1658,21 @@ macro_rules! map { KL: Fn(u32, &K) -> usize, VL: Fn(u32, &V) -> usize, { + let mut skip_default_value = false; + #[cfg(feature = "pb-encode-default-value")] + { + skip_default_value = true; + } + key_len(tag) * values.len() + values .iter() .map(|(key, val)| { - let len = (if key == &K::default() { + let len = (if key == &K::default() && skip_default_value { 0 } else { key_encoded_len(1, key) - }) + (if val == val_default { + }) + (if val == val_default && skip_default_value { 0 } else { val_encoded_len(2, val)