lerouxrgd commented on code in PR #3027:
URL: https://github.com/apache/avro/pull/3027#discussion_r1682805030


##########
lang/rust/avro/src/bytes.rs:
##########
@@ -0,0 +1,320 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::cell::Cell;
+
+thread_local! {
+    /// A thread local that is used to decide how to serialize Rust bytes into 
an Avro
+    /// `types::Value` of type bytes.
+    ///
+    /// Relies on the fact that serde's serialization process is 
single-threaded.
+    pub(crate) static SER_BYTES_TYPE: Cell<BytesType> = const { 
Cell::new(BytesType::Bytes) };
+
+    /// A thread local that is used to decide how to deserialize an Avro 
`types::Value`
+    /// of type bytes into Rust bytes.
+    ///
+    /// Relies on the fact that serde's deserialization process is 
single-threaded.
+    pub(crate) static DE_BYTES_BORROWED: Cell<bool> = const { Cell::new(false) 
};
+}
+
+#[derive(Debug, Clone, Copy)]
+pub(crate) enum BytesType {
+    Bytes,
+    Fixed,
+}
+
+/// Efficient (de)serialization of Avro bytes values.
+///
+/// This module is intended to be used through the Serde `with` attribute. See 
below
+/// example:
+///
+/// ```rust
+/// use apache_avro::{serde_avro_bytes, serde_avro_fixed};
+/// use serde::{Deserialize, Serialize};
+///
+/// #[derive(Serialize, Deserialize)]
+/// struct StructWithBytes {
+///     #[serde(with = "serde_avro_bytes")]
+///     vec_field: Vec<u8>,
+///
+///     #[serde(with = "serde_avro_fixed")]

Review Comment:
   I was hesitating, but I thought showcasing both in the same struct could 
help a user who would be discovering this feature.



##########
lang/rust/avro/src/bytes.rs:
##########
@@ -0,0 +1,320 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::cell::Cell;
+
+thread_local! {
+    /// A thread local that is used to decide how to serialize Rust bytes into 
an Avro
+    /// `types::Value` of type bytes.
+    ///
+    /// Relies on the fact that serde's serialization process is 
single-threaded.
+    pub(crate) static SER_BYTES_TYPE: Cell<BytesType> = const { 
Cell::new(BytesType::Bytes) };
+
+    /// A thread local that is used to decide how to deserialize an Avro 
`types::Value`
+    /// of type bytes into Rust bytes.
+    ///
+    /// Relies on the fact that serde's deserialization process is 
single-threaded.
+    pub(crate) static DE_BYTES_BORROWED: Cell<bool> = const { Cell::new(false) 
};
+}
+
+#[derive(Debug, Clone, Copy)]
+pub(crate) enum BytesType {
+    Bytes,
+    Fixed,
+}
+
+/// Efficient (de)serialization of Avro bytes values.
+///
+/// This module is intended to be used through the Serde `with` attribute. See 
below
+/// example:
+///
+/// ```rust
+/// use apache_avro::{serde_avro_bytes, serde_avro_fixed};
+/// use serde::{Deserialize, Serialize};
+///
+/// #[derive(Serialize, Deserialize)]
+/// struct StructWithBytes {
+///     #[serde(with = "serde_avro_bytes")]
+///     vec_field: Vec<u8>,
+///
+///     #[serde(with = "serde_avro_fixed")]
+///     fixed_field: [u8; 6],
+/// }
+/// ```
+pub mod serde_avro_bytes {
+    use serde::{Deserializer, Serializer};
+
+    pub fn serialize<S: Serializer>(bytes: &[u8], serializer: S) -> 
Result<S::Ok, S::Error> {
+        serde_bytes::serialize(bytes, serializer)
+    }
+
+    pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> 
Result<Vec<u8>, D::Error> {
+        serde_bytes::deserialize(deserializer)
+    }
+}
+
+/// Efficient (de)serialization of Avro fixed values.
+///
+/// This module is intended to be used through the Serde `with` attribute. See 
below
+/// example:
+///
+/// ```rust
+/// use apache_avro::{serde_avro_bytes, serde_avro_fixed};
+/// use serde::{Deserialize, Serialize};
+///
+/// #[derive(Serialize, Deserialize)]
+/// struct StructWithBytes {
+///     #[serde(with = "serde_avro_bytes")]

Review Comment:
   I was hesitating, but I thought showcasing both in the same struct could 
help a user who would be discovering this feature.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to