From 844b938023ede56f99f113dea5ab705e30023bc2 Mon Sep 17 00:00:00 2001 From: peter-formlogic Date: Thu, 27 Apr 2023 09:56:29 +0930 Subject: [PATCH 1/8] Set `is_human_readable` hint to `false` for `Value` --- lang/rust/avro/src/ser.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs index c85c8c4d3ee..6e156da29d2 100644 --- a/lang/rust/avro/src/ser.rs +++ b/lang/rust/avro/src/ser.rs @@ -283,6 +283,10 @@ impl<'b> ser::Serializer for &'b mut Serializer { ) -> Result { Ok(StructVariantSerializer::new(index, variant, len)) } + + fn is_human_readable(&self) -> bool { + false + } } impl ser::SerializeSeq for SeqSerializer { From b16712809e722d40304a3a02fb628cd08e33ad49 Mon Sep 17 00:00:00 2001 From: peter-formlogic Date: Thu, 27 Apr 2023 10:05:47 +0930 Subject: [PATCH 2/8] Include for `de` as well --- lang/rust/avro/src/de.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs index a5bc14b4f00..315eb505a5e 100644 --- a/lang/rust/avro/src/de.rs +++ b/lang/rust/avro/src/de.rs @@ -537,6 +537,10 @@ impl<'a, 'de> de::Deserializer<'de> for &'a Deserializer<'de> { { self.deserialize_any(visitor) } + + fn is_human_readable(&self) -> bool { + false + } } impl<'de> de::SeqAccess<'de> for SeqDeserializer<'de> { From cf9153746537d1395a5cf3b1140d5d38dae10509 Mon Sep 17 00:00:00 2001 From: peter-formlogic Date: Thu, 27 Apr 2023 15:15:21 +0930 Subject: [PATCH 3/8] Fix windows path error --- .../_index.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/content/en/project/{Contributors onboarding guide => Contributors onboarding guide}/_index.md (100%) diff --git a/doc/content/en/project/Contributors onboarding guide /_index.md b/doc/content/en/project/Contributors onboarding guide/_index.md similarity index 100% rename from doc/content/en/project/Contributors onboarding guide /_index.md rename to doc/content/en/project/Contributors onboarding guide/_index.md From 1440172c7847948333bc3074a8b0e76ec5766055 Mon Sep 17 00:00:00 2001 From: peter-formlogic Date: Fri, 28 Apr 2023 10:32:05 +0930 Subject: [PATCH 4/8] Include test --- lang/rust/avro/src/de.rs | 12 ++++++++++++ lang/rust/avro/src/ser.rs | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs index 315eb505a5e..f766b9f2b23 100644 --- a/lang/rust/avro/src/de.rs +++ b/lang/rust/avro/src/de.rs @@ -1224,4 +1224,16 @@ mod tests { assert_eq!(deserialized, reference); Ok(()) } + + #[test] + fn test_human_readable() -> TestResult<()> { + // AVRO-3747: set is_human_readable to false + use serde::de::Deserializer as SerdeDeserializer; + + let deser = Deserializer::new(&Value::Null); + + assert_eq!((&deser).is_human_readable(), false); + + Ok(()) + } } diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs index 6e156da29d2..150ec7f83d0 100644 --- a/lang/rust/avro/src/ser.rs +++ b/lang/rust/avro/src/ser.rs @@ -1002,4 +1002,14 @@ mod tests { "error serializing tuple untagged enum" ); } + + #[test] + fn test_human_readable() { + // AVRO-3747: set is_human_readable to false + use serde::ser::Serializer as SerdeSerializer; + + let mut ser = Serializer {}; + + assert_eq!((&mut ser).is_human_readable(), false); + } } From 8286274c39486fbdeb35f4691e433579fe70a00e Mon Sep 17 00:00:00 2001 From: peter-formlogic Date: Wed, 3 May 2023 09:48:27 +0930 Subject: [PATCH 5/8] Fix clippy lint and add util for setting whether it's human readable --- lang/rust/avro/src/de.rs | 4 +++- lang/rust/avro/src/lib.rs | 2 +- lang/rust/avro/src/ser.rs | 6 +++++- lang/rust/avro/src/util.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs index f766b9f2b23..82afc62b0b5 100644 --- a/lang/rust/avro/src/de.rs +++ b/lang/rust/avro/src/de.rs @@ -539,7 +539,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a Deserializer<'de> { } fn is_human_readable(&self) -> bool { - false + crate::util::is_human_readable() } } @@ -1230,6 +1230,8 @@ mod tests { // AVRO-3747: set is_human_readable to false use serde::de::Deserializer as SerdeDeserializer; + crate::util::set_human_readable(false); + let deser = Deserializer::new(&Value::Null); assert_eq!((&deser).is_human_readable(), false); diff --git a/lang/rust/avro/src/lib.rs b/lang/rust/avro/src/lib.rs index 2c6f46c07a3..266b95596d2 100644 --- a/lang/rust/avro/src/lib.rs +++ b/lang/rust/avro/src/lib.rs @@ -748,7 +748,7 @@ pub use reader::{ }; pub use schema::{AvroSchema, Schema}; pub use ser::to_value; -pub use util::max_allocation_bytes; +pub use util::{max_allocation_bytes, set_human_readable}; pub use writer::{ to_avro_datum, to_avro_datum_schemata, GenericSingleObjectWriter, SpecificSingleObjectWriter, Writer, diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs index 150ec7f83d0..5df6d6176a2 100644 --- a/lang/rust/avro/src/ser.rs +++ b/lang/rust/avro/src/ser.rs @@ -285,7 +285,7 @@ impl<'b> ser::Serializer for &'b mut Serializer { } fn is_human_readable(&self) -> bool { - false + crate::util::is_human_readable() } } @@ -1004,10 +1004,14 @@ mod tests { } #[test] + // the serde `Serializer` trait is only implemented for `&mut Serializer` + #[allow(clippy::unnecessary_mut_passed)] fn test_human_readable() { // AVRO-3747: set is_human_readable to false use serde::ser::Serializer as SerdeSerializer; + crate::util::set_human_readable(false); + let mut ser = Serializer {}; assert_eq!((&mut ser).is_human_readable(), false); diff --git a/lang/rust/avro/src/util.rs b/lang/rust/avro/src/util.rs index e18b5641fce..7552cd71e02 100644 --- a/lang/rust/avro/src/util.rs +++ b/lang/rust/avro/src/util.rs @@ -27,6 +27,13 @@ pub const DEFAULT_MAX_ALLOCATION_BYTES: usize = 512 * 1024 * 1024; static mut MAX_ALLOCATION_BYTES: usize = DEFAULT_MAX_ALLOCATION_BYTES; static MAX_ALLOCATION_BYTES_ONCE: Once = Once::new(); +/// Whether to set serialization & deserialization traits +/// as `human_readable` or not. +/// See set_human_readable to change this value. +pub const DEFAULT_HUMAN_READABLE: bool = true; +static mut HUMAN_READABLE: bool = DEFAULT_HUMAN_READABLE; +static HUMAN_READABLE_ONCE: Once = Once::new(); + pub trait MapHelper { fn string(&self, key: &str) -> Option; @@ -153,6 +160,27 @@ pub fn safe_len(len: usize) -> AvroResult { } } +/// Set whether serializing/deserializing is marked as human readable in serde traits. +/// This will adjust the return value of `is_human_readable()` for both. +/// Once called, the value cannot be changed. +/// +/// **NOTE** This function must be called before serializing/deserializing **any** data. The +/// library leverages [`std::sync::Once`](https://doc.rust-lang.org/std/sync/struct.Once.html) +/// to set the limit either when calling this method, or when decoding for +/// the first time. +pub fn set_human_readable(human_readable: bool) -> bool { + unsafe { + HUMAN_READABLE_ONCE.call_once(|| { + HUMAN_READABLE = human_readable; + }); + HUMAN_READABLE + } +} + +pub fn is_human_readable() -> bool { + set_human_readable(DEFAULT_HUMAN_READABLE) +} + #[cfg(test)] mod tests { use super::*; From 17fc95b2744e7971c618aba30cec99d09622c1eb Mon Sep 17 00:00:00 2001 From: Martin Tzvetanov Grigorov Date: Wed, 10 May 2023 14:33:40 +0300 Subject: [PATCH 6/8] AVRO-3747: [Rust] Make serde `is_human_readable` configurable Signed-off-by: Martin Tzvetanov Grigorov --- lang/rust/avro/src/de.rs | 22 ++++++++++++++++++++-- lang/rust/avro/src/lib.rs | 2 +- lang/rust/avro/src/ser.rs | 32 ++++++++++++++++++++++++++------ lang/rust/avro/src/util.rs | 21 +++++++++++---------- 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs index 82afc62b0b5..cb6f7db8b26 100644 --- a/lang/rust/avro/src/de.rs +++ b/lang/rust/avro/src/de.rs @@ -1226,11 +1226,13 @@ mod tests { } #[test] - fn test_human_readable() -> TestResult<()> { + fn avro_3747_human_readable_false() -> TestResult<()> { // AVRO-3747: set is_human_readable to false use serde::de::Deserializer as SerdeDeserializer; - crate::util::set_human_readable(false); + unsafe { + crate::util::SERDE_HUMAN_READABLE = false; + } let deser = Deserializer::new(&Value::Null); @@ -1238,4 +1240,20 @@ mod tests { Ok(()) } + + #[test] + fn avro_3747_human_readable_true() -> TestResult<()> { + // AVRO-3747: set is_human_readable to false + use serde::de::Deserializer as SerdeDeserializer; + + unsafe { + crate::util::SERDE_HUMAN_READABLE = true; + } + + let deser = Deserializer::new(&Value::Null); + + assert!((&deser).is_human_readable()); + + Ok(()) + } } diff --git a/lang/rust/avro/src/lib.rs b/lang/rust/avro/src/lib.rs index 266b95596d2..af856e906f5 100644 --- a/lang/rust/avro/src/lib.rs +++ b/lang/rust/avro/src/lib.rs @@ -748,7 +748,7 @@ pub use reader::{ }; pub use schema::{AvroSchema, Schema}; pub use ser::to_value; -pub use util::{max_allocation_bytes, set_human_readable}; +pub use util::{max_allocation_bytes, set_serde_human_readable}; pub use writer::{ to_avro_datum, to_avro_datum_schemata, GenericSingleObjectWriter, SpecificSingleObjectWriter, Writer, diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs index 5df6d6176a2..e8757139ef0 100644 --- a/lang/rust/avro/src/ser.rs +++ b/lang/rust/avro/src/ser.rs @@ -1004,16 +1004,36 @@ mod tests { } #[test] - // the serde `Serializer` trait is only implemented for `&mut Serializer` - #[allow(clippy::unnecessary_mut_passed)] - fn test_human_readable() { - // AVRO-3747: set is_human_readable to false + fn avro_3747_human_readable_false() { use serde::ser::Serializer as SerdeSerializer; - crate::util::set_human_readable(false); + unsafe { + crate::util::SERDE_HUMAN_READABLE = false; + } + + let mut ser = Serializer {}; + + // the serde `Serializer` trait is only implemented for `&mut Serializer` + #[allow(clippy::unnecessary_mut_passed)] + { + assert_eq!((&mut ser).is_human_readable(), false); + } + } + + #[test] + fn avro_3747_human_readable_true() { + use serde::ser::Serializer as SerdeSerializer; + + unsafe { + crate::util::SERDE_HUMAN_READABLE = true; + } let mut ser = Serializer {}; - assert_eq!((&mut ser).is_human_readable(), false); + // the serde `Serializer` trait is only implemented for `&mut Serializer` + #[allow(clippy::unnecessary_mut_passed)] + { + assert!((&mut ser).is_human_readable()); + } } } diff --git a/lang/rust/avro/src/util.rs b/lang/rust/avro/src/util.rs index 7552cd71e02..695a1a6c7f0 100644 --- a/lang/rust/avro/src/util.rs +++ b/lang/rust/avro/src/util.rs @@ -29,10 +29,11 @@ static MAX_ALLOCATION_BYTES_ONCE: Once = Once::new(); /// Whether to set serialization & deserialization traits /// as `human_readable` or not. -/// See set_human_readable to change this value. -pub const DEFAULT_HUMAN_READABLE: bool = true; -static mut HUMAN_READABLE: bool = DEFAULT_HUMAN_READABLE; -static HUMAN_READABLE_ONCE: Once = Once::new(); +/// See [set_serde_human_readable] to change this value. +pub const DEFAULT_SERDE_HUMAN_READABLE: bool = true; +// crate visible for testing +pub(crate) static mut SERDE_HUMAN_READABLE: bool = DEFAULT_SERDE_HUMAN_READABLE; +static SERDE_HUMAN_READABLE_ONCE: Once = Once::new(); pub trait MapHelper { fn string(&self, key: &str) -> Option; @@ -168,17 +169,17 @@ pub fn safe_len(len: usize) -> AvroResult { /// library leverages [`std::sync::Once`](https://doc.rust-lang.org/std/sync/struct.Once.html) /// to set the limit either when calling this method, or when decoding for /// the first time. -pub fn set_human_readable(human_readable: bool) -> bool { +pub fn set_serde_human_readable(human_readable: bool) -> bool { unsafe { - HUMAN_READABLE_ONCE.call_once(|| { - HUMAN_READABLE = human_readable; + SERDE_HUMAN_READABLE_ONCE.call_once(|| { + SERDE_HUMAN_READABLE = human_readable; }); - HUMAN_READABLE + SERDE_HUMAN_READABLE } } -pub fn is_human_readable() -> bool { - set_human_readable(DEFAULT_HUMAN_READABLE) +pub(crate) fn is_human_readable() -> bool { + unsafe { SERDE_HUMAN_READABLE } } #[cfg(test)] From eb0989394e962be98f3e92746ab451f4739d5029 Mon Sep 17 00:00:00 2001 From: Martin Tzvetanov Grigorov Date: Wed, 10 May 2023 14:39:06 +0300 Subject: [PATCH 7/8] AVRO-3747: Get rid of clippy::unnecessary_mut_passed Signed-off-by: Martin Tzvetanov Grigorov --- lang/rust/avro/src/ser.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs index e8757139ef0..1c7718ba724 100644 --- a/lang/rust/avro/src/ser.rs +++ b/lang/rust/avro/src/ser.rs @@ -1011,13 +1011,9 @@ mod tests { crate::util::SERDE_HUMAN_READABLE = false; } - let mut ser = Serializer {}; + let ser = &mut Serializer {}; - // the serde `Serializer` trait is only implemented for `&mut Serializer` - #[allow(clippy::unnecessary_mut_passed)] - { - assert_eq!((&mut ser).is_human_readable(), false); - } + assert_eq!(ser.is_human_readable(), false); } #[test] @@ -1028,12 +1024,8 @@ mod tests { crate::util::SERDE_HUMAN_READABLE = true; } - let mut ser = Serializer {}; + let ser = &mut Serializer {}; - // the serde `Serializer` trait is only implemented for `&mut Serializer` - #[allow(clippy::unnecessary_mut_passed)] - { - assert!((&mut ser).is_human_readable()); - } + assert!(ser.is_human_readable()); } } From c4f8e9a1b73d613fc0e18565f68b50bd73180f5e Mon Sep 17 00:00:00 2001 From: Martin Tzvetanov Grigorov Date: Wed, 10 May 2023 15:29:01 +0300 Subject: [PATCH 8/8] AVRO-3747: Fix test comments Signed-off-by: Martin Tzvetanov Grigorov --- lang/rust/avro/src/de.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/rust/avro/src/de.rs b/lang/rust/avro/src/de.rs index cb6f7db8b26..cfac4d1563f 100644 --- a/lang/rust/avro/src/de.rs +++ b/lang/rust/avro/src/de.rs @@ -1227,7 +1227,7 @@ mod tests { #[test] fn avro_3747_human_readable_false() -> TestResult<()> { - // AVRO-3747: set is_human_readable to false + // AVRO-3747: set serde's is_human_readable to false use serde::de::Deserializer as SerdeDeserializer; unsafe { @@ -1243,7 +1243,7 @@ mod tests { #[test] fn avro_3747_human_readable_true() -> TestResult<()> { - // AVRO-3747: set is_human_readable to false + // AVRO-3747: set serde's is_human_readable to true use serde::de::Deserializer as SerdeDeserializer; unsafe {