martin-g commented on code in PR #3027: URL: https://github.com/apache/avro/pull/3027#discussion_r1682746112
########## lang/rust/Cargo.toml: ########## @@ -34,7 +34,7 @@ authors = ["Apache Avro team <[email protected]>"] license = "Apache-2.0" repository = "https://github.com/apache/avro" edition = "2021" -rust-version = "1.70.0" +rust-version = "1.73.0" Review Comment: According to https://github.com/serde-rs/bytes/blob/0.11.15/Cargo.toml#L12 the MSRV for serde_bytes is 1.53. Why do we need to bump to 1.73.0 here ? ########## 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: Shall we leave only the field for `bytes` here ? Like the doc for `slice` below. ########## 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: Shall we leave only the field for `fixed` here ? Like the doc for `slice` below. ########## lang/rust/avro/src/lib.rs: ########## @@ -841,6 +841,7 @@ //! mod bigdecimal; +mod bytes; Review Comment: I recall you asked for some re-export related to bytes in rsgen-avro. Do you still need it ? -- 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]
