This is an automated email from the ASF dual-hosted git repository. kriskras99 pushed a commit to branch reorganize/bytes in repository https://gitbox.apache.org/repos/asf/avro-rs.git
commit dff66a1d216dff3f93e0e5eea9f9612fe1bf5fbc Author: default <[email protected]> AuthorDate: Fri Jan 23 14:35:00 2026 +0000 fix: Add backwards compatible deprecated module `bytes` The deprecated module does not re-export `get_schema_in_ctxt` as it hasn't existed there in a release. --- avro/src/bytes.rs | 787 +++++++++--------------------------------------------- avro/src/lib.rs | 10 +- 2 files changed, 132 insertions(+), 665 deletions(-) diff --git a/avro/src/bytes.rs b/avro/src/bytes.rs index bac4e3f..534ce47 100644 --- a/avro/src/bytes.rs +++ b/avro/src/bytes.rs @@ -15,769 +15,228 @@ // specific language governing permissions and limitations // under the License. -use std::cell::Cell; +//! Deprecated. See [`apache_avro::serde::*`] instead. +//! +//! [`apache_avro::serde::*`](crate::serde) -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. Use -/// [`serde_avro_bytes_opt`](crate::serde_avro_bytes_opt) for optional bytes. -/// -/// See usage with below example: -/// ```rust -/// use apache_avro::{AvroSchema, serde_avro_bytes, serde_avro_fixed}; -/// use serde::{Deserialize, Serialize}; -/// -/// #[derive(AvroSchema, Serialize, Deserialize)] -/// struct StructWithBytes { -/// #[avro(with)] -/// #[serde(with = "serde_avro_bytes")] -/// vec_field: Vec<u8>, -/// -/// #[avro(with = serde_avro_fixed::get_schema_in_ctxt::<6>)] -/// #[serde(with = "serde_avro_fixed")] -/// fixed_field: [u8; 6], -/// } -/// ``` +// Deprecated. See [`apache_avro::serde::bytes`] instead. +// +// [`apache_avro::serde::bytes`](crate::serde::bytes) +#[deprecated(since = "0.22.0", note = "Use `apache_avro::serde::bytes` instead")] pub mod serde_avro_bytes { use serde::{Deserializer, Serializer}; - use crate::{ - Schema, - schema::{Names, Namespace}, - }; - - /// Returns [`Schema::Bytes`] - pub fn get_schema_in_ctxt(_names: &mut Names, _enclosing_namespace: &Namespace) -> Schema { - Schema::Bytes - } - + // Deprecated. See [`apache_avro::serde::bytes::serialize`] instead. + // + // [`apache_avro::serde::bytes::serialize`](crate::serde::bytes::serialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::bytes::serialize` instead" + )] pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { - serde_bytes::serialize(bytes, serializer) + crate::serde::bytes::serialize(bytes, serializer) } + // Deprecated. See [`apache_avro::serde::bytes::deserialize`] instead. + // + // [`apache_avro::serde::bytes::deserialize`](crate::serde::bytes::deserialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::bytes::deserialize` instead" + )] pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error> where D: Deserializer<'de>, { - serde_bytes::deserialize(deserializer) + crate::serde::bytes::deserialize(deserializer) } } -/// Efficient (de)serialization of optional Avro bytes values. -/// -/// This module is intended to be used through the Serde `with` attribute. Use -/// [`serde_avro_bytes`](crate::serde_avro_bytes) for non optional bytes. -/// -/// See usage with below example: -/// ```rust -/// use apache_avro::{AvroSchema, serde_avro_bytes_opt, serde_avro_fixed_opt}; -/// use serde::{Deserialize, Serialize}; -/// -/// #[derive(AvroSchema, Serialize, Deserialize)] -/// struct StructWithBytes { -/// #[avro(with)] -/// #[serde(with = "serde_avro_bytes_opt")] -/// vec_field: Option<Vec<u8>>, -/// -/// #[avro(with = serde_avro_fixed_opt::get_schema_in_ctxt::<6>)] -/// #[serde(with = "serde_avro_fixed_opt")] -/// fixed_field: Option<[u8; 6]>, -/// } -/// ``` +// Deprecated. See [`apache_avro::serde::bytes_opt`] instead. +// +// [`apache_avro::serde::bytes_opt`](crate::serde::bytes_opt) +#[deprecated(since = "0.22.0", note = "Use `apache_avro::serde::bytes_opt` instead")] pub mod serde_avro_bytes_opt { use serde::{Deserializer, Serializer}; use std::borrow::Borrow; - use crate::{ - Schema, - schema::{Names, Namespace, UnionSchema}, - }; - - /// Returns `Schema::Union(Schema::Null, Schema::Bytes)` - pub fn get_schema_in_ctxt(_names: &mut Names, _enclosing_namespace: &Namespace) -> Schema { - Schema::Union( - UnionSchema::new(vec![Schema::Null, Schema::Bytes]).expect("This is a valid union"), - ) - } - + // Deprecated. See [`apache_avro::serde::bytes_opt::serialize`] instead. + // + // [`apache_avro::serde::bytes_opt::serialize`](crate::serde::bytes_opt::serialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::bytes_opt::serialize` instead" + )] pub fn serialize<S, B>(bytes: &Option<B>, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, B: Borrow<[u8]> + serde_bytes::Serialize, { - serde_bytes::serialize(bytes, serializer) + crate::serde::bytes_opt::serialize(bytes, serializer) } + // Deprecated. See [`apache_avro::serde::bytes_opt::deserialize`] instead. + // + // [`apache_avro::serde::bytes_opt::deserialize`](crate::serde::bytes_opt::deserialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::bytes_opt::deserialize` instead" + )] pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error> where D: Deserializer<'de>, { - serde_bytes::deserialize(deserializer) + crate::serde::bytes_opt::deserialize(deserializer) } } -/// Efficient (de)serialization of Avro fixed values. -/// -/// This module is intended to be used through the Serde `with` attribute. Use -/// [`serde_avro_fixed_opt`](crate::serde_avro_fixed_opt) for optional fixed values. -/// -/// See usage with below example: -/// ```rust -/// use apache_avro::{AvroSchema, serde_avro_bytes, serde_avro_fixed}; -/// use serde::{Deserialize, Serialize}; -/// -/// #[derive(AvroSchema, Serialize, Deserialize)] -/// struct StructWithBytes { -/// #[avro(with)] -/// #[serde(with = "serde_avro_bytes")] -/// vec_field: Vec<u8>, -/// -/// #[avro(with = serde_avro_fixed::get_schema_in_ctxt::<6>)] -/// #[serde(with = "serde_avro_fixed")] -/// fixed_field: [u8; 6], -/// } -/// ``` +// Deprecated. See [`apache_avro::serde::fixed`] instead. +// +// [`apache_avro::serde::fixed`](crate::serde::fixed) +#[deprecated(since = "0.22.0", note = "Use `apache_avro::serde::fixed` instead")] pub mod serde_avro_fixed { - use super::{BytesType, SER_BYTES_TYPE}; use serde::{Deserializer, Serializer}; - use crate::{ - Schema, - schema::{FixedSchema, Name, Names, Namespace}, - }; - - /// Returns `Schema::Fixed(N)` named `serde_avro_fixed_{N}` - #[expect(clippy::map_entry, reason = "We don't use the value from the map")] - pub fn get_schema_in_ctxt<const N: usize>( - named_schemas: &mut Names, - enclosing_namespace: &Namespace, - ) -> Schema { - let name = Name::new(&format!("serde_avro_fixed_{N}")) - .expect("Name is valid") - .fully_qualified_name(enclosing_namespace); - if named_schemas.contains_key(&name) { - Schema::Ref { name } - } else { - let schema = Schema::Fixed(FixedSchema::builder().name(name.clone()).size(N).build()); - named_schemas.insert(name, schema.clone()); - schema - } - } - + // Deprecated. See [`apache_avro::serde::fixed::serialize`] instead. + // + // [`apache_avro::serde::fixed::serialize`](crate::serde::fixed::serialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::fixed::serialize` instead" + )] pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { - SER_BYTES_TYPE.set(BytesType::Fixed); - let res = serde_bytes::serialize(bytes, serializer); - SER_BYTES_TYPE.set(BytesType::Bytes); - res + crate::serde::fixed::serialize(bytes, serializer) } + // Deprecated. See [`apache_avro::serde::fixed::deserialize`] instead. + // + // [`apache_avro::serde::fixed::deserialize`](crate::serde::fixed::deserialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::fixed::deserialize` instead" + )] pub fn deserialize<'de, D, const N: usize>(deserializer: D) -> Result<[u8; N], D::Error> where D: Deserializer<'de>, { - serde_bytes::deserialize(deserializer) + crate::serde::fixed::deserialize(deserializer) } } -/// Efficient (de)serialization of optional Avro fixed values. -/// -/// This module is intended to be used through the Serde `with` attribute. Use -/// [`serde_avro_fixed`](crate::serde_avro_fixed) for non optional fixed values. -/// -/// See usage with below example: -/// ```rust -/// use apache_avro::{AvroSchema, serde_avro_bytes_opt, serde_avro_fixed_opt}; -/// use serde::{Deserialize, Serialize}; -/// -/// #[derive(AvroSchema, Serialize, Deserialize)] -/// struct StructWithBytes { -/// #[avro(with)] -/// #[serde(with = "serde_avro_bytes_opt")] -/// vec_field: Option<Vec<u8>>, -/// -/// #[avro(with = serde_avro_fixed_opt::get_schema_in_ctxt::<6>)] -/// #[serde(with = "serde_avro_fixed_opt")] -/// fixed_field: Option<[u8; 6]>, -/// } -/// ``` +// Deprecated. See [`apache_avro::serde::fixed_opt`] instead. +// +// [`apache_avro::serde::fixed_opt`](crate::serde::fixed_opt) +#[deprecated(since = "0.22.0", note = "Use `apache_avro::serde::fixed_opt` instead")] pub mod serde_avro_fixed_opt { - use super::{BytesType, SER_BYTES_TYPE}; use serde::{Deserializer, Serializer}; use std::borrow::Borrow; - use crate::{ - Schema, - schema::{Names, Namespace, UnionSchema}, - }; - - /// Returns `Schema::Union(Schema::Null, Schema::Fixed(N))` where the fixed schema is named `serde_avro_fixed_{N}` - pub fn get_schema_in_ctxt<const N: usize>( - named_schemas: &mut Names, - enclosing_namespace: &Namespace, - ) -> Schema { - Schema::Union( - UnionSchema::new(vec![ - Schema::Null, - super::serde_avro_fixed::get_schema_in_ctxt::<N>( - named_schemas, - enclosing_namespace, - ), - ]) - .expect("This is a valid union"), - ) - } - + // Deprecated. See [`apache_avro::serde::fixed_opt::serialize`] instead. + // + // [`apache_avro::serde::fixed_opt::serialize`](crate::serde::fixed_opt::serialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::fixed_opt::serialize` instead" + )] pub fn serialize<S, B>(bytes: &Option<B>, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, B: Borrow<[u8]> + serde_bytes::Serialize, { - SER_BYTES_TYPE.set(BytesType::Fixed); - let res = serde_bytes::serialize(bytes, serializer); - SER_BYTES_TYPE.set(BytesType::Bytes); - res + crate::serde::fixed_opt::serialize(bytes, serializer) } + // Deprecated. See [`apache_avro::serde::fixed_opt::deserialize`] instead. + // + // [`apache_avro::serde::fixed_opt::deserialize`](crate::serde::fixed_opt::deserialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::fixed_opt::deserialize` instead" + )] pub fn deserialize<'de, D, const N: usize>(deserializer: D) -> Result<Option<[u8; N]>, D::Error> where D: Deserializer<'de>, { - serde_bytes::deserialize(deserializer) + crate::serde::fixed_opt::deserialize(deserializer) } } -/// Efficient (de)serialization of Avro bytes/fixed borrowed values. -/// -/// This module is intended to be used through the Serde `with` attribute. Note that -/// `bytes: &[u8]` are always serialized as -/// [`Value::Bytes`](crate::types::Value::Bytes). However, both -/// [`Value::Bytes`](crate::types::Value::Bytes) and -/// [`Value::Fixed`](crate::types::Value::Fixed) can be deserialized as `bytes: -/// &[u8]`. Use [`serde_avro_slice_opt`](crate::serde_avro_slice_opt) for optional -/// bytes/fixed borrowed values. -/// -/// See usage with below example: -/// ```rust -/// use apache_avro::{AvroSchema, serde_avro_slice}; -/// use serde::{Deserialize, Serialize}; -/// -/// #[derive(AvroSchema, Serialize, Deserialize)] -/// struct StructWithBytes<'a> { -/// #[avro(with)] -/// #[serde(with = "serde_avro_slice")] -/// slice_field: &'a [u8], -/// } -/// ``` +// Deprecated. See [`apache_avro::serde::slice`] instead. +// +// [`apache_avro::serde::slice`](crate::serde::slice) +#[deprecated(since = "0.22.0", note = "Use `apache_avro::serde::slice` instead")] pub mod serde_avro_slice { - use super::DE_BYTES_BORROWED; use serde::{Deserializer, Serializer}; - use crate::{ - Schema, - schema::{Names, Namespace}, - }; - - /// Returns [`Schema::Bytes`] - pub fn get_schema_in_ctxt(_names: &mut Names, _enclosing_namespace: &Namespace) -> Schema { - Schema::Bytes - } - + // Deprecated. See [`apache_avro::serde::slice::serialize`] instead. + // + // [`apache_avro::serde::slice::serialize`](crate::serde::slice::serialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::slice::serialize` instead" + )] pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, { - serde_bytes::serialize(bytes, serializer) + crate::serde::slice::serialize(bytes, serializer) } + // Deprecated. See [`apache_avro::serde::slice::deserialize`] instead. + // + // [`apache_avro::serde::slice::deserialize`](crate::serde::slice::deserialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::slice::deserialize` instead" + )] pub fn deserialize<'de, D>(deserializer: D) -> Result<&'de [u8], D::Error> where D: Deserializer<'de>, { - DE_BYTES_BORROWED.set(true); - let res = serde_bytes::deserialize(deserializer); - DE_BYTES_BORROWED.set(false); - res + crate::serde::slice::deserialize(deserializer) } } -/// Efficient (de)serialization of optional Avro bytes/fixed borrowed values. -/// -/// This module is intended to be used through the Serde `with` attribute. Note that -/// `bytes: &[u8]` are always serialized as -/// [`Value::Bytes`](crate::types::Value::Bytes). However, both -/// [`Value::Bytes`](crate::types::Value::Bytes) and -/// [`Value::Fixed`](crate::types::Value::Fixed) can be deserialized as `bytes: -/// &[u8]`. Use [`serde_avro_slice`](crate::serde_avro_slice) for non optional -/// bytes/fixed borrowed values. -/// -/// See usage with below example: -/// ```rust -/// use apache_avro::{AvroSchema, serde_avro_slice_opt}; -/// use serde::{Deserialize, Serialize}; -/// -/// #[derive(AvroSchema, Serialize, Deserialize)] -/// struct StructWithBytes<'a> { -/// #[avro(with)] -/// #[serde(with = "serde_avro_slice_opt")] -/// slice_field: Option<&'a [u8]>, -/// } -/// ``` +// Deprecated. See [`apache_avro::serde::slice_opt`] instead. +// +// [`apache_avro::serde::slice_opt`](crate::serde::slice_opt) +#[deprecated(since = "0.22.0", note = "Use `apache_avro::serde::slice_opt` instead")] pub mod serde_avro_slice_opt { - use super::DE_BYTES_BORROWED; use serde::{Deserializer, Serializer}; use std::borrow::Borrow; - use crate::{ - Schema, - schema::{Names, Namespace, UnionSchema}, - }; - - /// Returns `Schema::Union(Schema::Null, Schema::Bytes)` - pub fn get_schema_in_ctxt(_names: &mut Names, _enclosing_namespace: &Namespace) -> Schema { - Schema::Union( - UnionSchema::new(vec![Schema::Null, Schema::Bytes]).expect("This is a valid union"), - ) - } - + // Deprecated. See [`apache_avro::serde::slice_opt::serialize`] instead. + // + // [`apache_avro::serde::slice_opt::serialize`](crate::serde::slice_opt::serialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::slice_opt::serialize` instead" + )] pub fn serialize<S, B>(bytes: &Option<B>, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer, B: Borrow<[u8]> + serde_bytes::Serialize, { - serde_bytes::serialize(&bytes, serializer) + crate::serde::slice_opt::serialize(bytes, serializer) } + // Deprecated. See [`apache_avro::serde::slice_opt::deserialize`] instead. + // + // [`apache_avro::serde::slice_opt::deserialize`](crate::serde::slice_opt::deserialize) + #[deprecated( + since = "0.22.0", + note = "Use `apache_avro::serde::slice_opt::deserialize` instead" + )] pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<&'de [u8]>, D::Error> where D: Deserializer<'de>, { - DE_BYTES_BORROWED.set(true); - let res = serde_bytes::deserialize(deserializer); - DE_BYTES_BORROWED.set(false); - res - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{Schema, from_value, to_value, types::Value}; - use serde::{Deserialize, Serialize}; - - #[test] - fn avro_3631_validate_schema_for_struct_with_byte_types() { - #[derive(Debug, Serialize)] - struct TestStructWithBytes<'a> { - #[serde(with = "serde_avro_bytes")] - vec_field: Vec<u8>, - #[serde(with = "serde_avro_bytes_opt")] - vec_field_opt: Option<Vec<u8>>, - - #[serde(with = "serde_avro_fixed")] - fixed_field: [u8; 6], - #[serde(with = "serde_avro_fixed_opt")] - fixed_field_opt: Option<[u8; 7]>, - - #[serde(with = "serde_avro_slice")] - slice_field: &'a [u8], - #[serde(with = "serde_avro_slice_opt")] - slice_field_opt: Option<&'a [u8]>, - } - - let test = TestStructWithBytes { - vec_field: vec![2, 3, 4], - vec_field_opt: Some(vec![2, 3, 4]), - fixed_field: [1; 6], - fixed_field_opt: Some([1; 7]), - slice_field: &[1, 2, 3], - slice_field_opt: Some(&[1, 2, 3]), - }; - let value: Value = to_value(test).unwrap(); - let schema = Schema::parse_str( - r#" - { - "type": "record", - "name": "TestStructWithBytes", - "fields": [ { - "name": "vec_field", - "type": "bytes" - }, { - "name": "vec_field_opt", - "type": ["null", "bytes"] - }, { - "name": "fixed_field", - "type": { - "name": "ByteData", - "type": "fixed", - "size": 6 - } - }, { - "name": "fixed_field_opt", - "type": ["null", { - "name": "ByteData2", - "type": "fixed", - "size": 7 - } ] - }, { - "name": "slice_field", - "type": "bytes" - }, { - "name": "slice_field_opt", - "type": ["null", "bytes"] - } ] - }"#, - ) - .unwrap(); - assert!(value.validate(&schema)); - } - - #[test] - fn avro_3631_deserialize_value_to_struct_with_byte_types() { - #[derive(Debug, Deserialize, PartialEq)] - struct TestStructWithBytes<'a> { - #[serde(with = "serde_avro_bytes")] - vec_field: Vec<u8>, - #[serde(with = "serde_avro_bytes_opt")] - vec_field_opt: Option<Vec<u8>>, - #[serde(with = "serde_avro_bytes_opt")] - vec_field_opt2: Option<Vec<u8>>, - - #[serde(with = "serde_avro_fixed")] - fixed_field: [u8; 6], - #[serde(with = "serde_avro_fixed_opt")] - fixed_field_opt: Option<[u8; 7]>, - #[serde(with = "serde_avro_fixed_opt")] - fixed_field_opt2: Option<[u8; 8]>, - - #[serde(with = "serde_avro_slice")] - slice_bytes_field: &'a [u8], - #[serde(with = "serde_avro_slice_opt")] - slice_bytes_field_opt: Option<&'a [u8]>, - #[serde(with = "serde_avro_slice_opt")] - slice_bytes_field_opt2: Option<&'a [u8]>, - - #[serde(with = "serde_avro_slice")] - slice_fixed_field: &'a [u8], - #[serde(with = "serde_avro_slice_opt")] - slice_fixed_field_opt: Option<&'a [u8]>, - #[serde(with = "serde_avro_slice_opt")] - slice_fixed_field_opt2: Option<&'a [u8]>, - } - - let expected = TestStructWithBytes { - vec_field: vec![3, 33], - vec_field_opt: Some(vec![4, 44]), - vec_field_opt2: None, - fixed_field: [1; 6], - fixed_field_opt: Some([7; 7]), - fixed_field_opt2: None, - slice_bytes_field: &[1, 11, 111], - slice_bytes_field_opt: Some(&[5, 5, 5, 5, 5]), - slice_bytes_field_opt2: None, - slice_fixed_field: &[2, 22, 222], - slice_fixed_field_opt: Some(&[3, 3, 3]), - slice_fixed_field_opt2: None, - }; - - let value = Value::Record(vec![ - ( - "vec_field".to_owned(), - Value::Bytes(expected.vec_field.clone()), - ), - ( - "vec_field_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Bytes( - expected.vec_field_opt.as_ref().unwrap().clone(), - )), - ), - ), - ( - "vec_field_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "fixed_field".to_owned(), - Value::Fixed(expected.fixed_field.len(), expected.fixed_field.to_vec()), - ), - ( - "fixed_field_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Fixed( - expected.fixed_field_opt.as_ref().unwrap().len(), - expected.fixed_field_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "fixed_field_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "slice_bytes_field".to_owned(), - Value::Bytes(expected.slice_bytes_field.to_vec()), - ), - ( - "slice_bytes_field_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Bytes( - expected.slice_bytes_field_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "slice_bytes_field_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "slice_fixed_field".to_owned(), - Value::Fixed( - expected.slice_fixed_field.len(), - expected.slice_fixed_field.to_vec(), - ), - ), - ( - "slice_fixed_field_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Fixed( - expected.slice_fixed_field_opt.as_ref().unwrap().len(), - expected.slice_fixed_field_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "slice_fixed_field_opt2".to_owned(), - Value::Union(1, Box::new(Value::Null)), - ), - ]); - assert_eq!(expected, from_value(&value).unwrap()); - } - - #[test] - fn avro_3631_serialize_struct_to_value_with_byte_types() { - #[derive(Debug, Serialize)] - struct TestStructWithBytes<'a> { - array_field: &'a [u8], - vec_field: Vec<u8>, - - #[serde(with = "serde_avro_fixed")] - vec_field2: Vec<u8>, - #[serde(with = "serde_avro_fixed_opt")] - vec_field2_opt: Option<Vec<u8>>, - #[serde(with = "serde_avro_fixed_opt")] - vec_field2_opt2: Option<Vec<u8>>, - - #[serde(with = "serde_avro_bytes")] - vec_field3: Vec<u8>, - #[serde(with = "serde_avro_bytes_opt")] - vec_field3_opt: Option<Vec<u8>>, - #[serde(with = "serde_avro_bytes_opt")] - vec_field3_opt2: Option<Vec<u8>>, - - #[serde(with = "serde_avro_fixed")] - fixed_field: [u8; 6], - #[serde(with = "serde_avro_fixed_opt")] - fixed_field_opt: Option<[u8; 5]>, - #[serde(with = "serde_avro_fixed_opt")] - fixed_field_opt2: Option<[u8; 4]>, - - #[serde(with = "serde_avro_fixed")] - fixed_field2: &'a [u8], - #[serde(with = "serde_avro_fixed_opt")] - fixed_field2_opt: Option<&'a [u8]>, - #[serde(with = "serde_avro_fixed_opt")] - fixed_field2_opt2: Option<&'a [u8]>, - - #[serde(with = "serde_avro_bytes")] - bytes_field: &'a [u8], - #[serde(with = "serde_avro_bytes_opt")] - bytes_field_opt: Option<&'a [u8]>, - #[serde(with = "serde_avro_bytes_opt")] - bytes_field_opt2: Option<&'a [u8]>, - - #[serde(with = "serde_avro_bytes")] - bytes_field2: [u8; 6], - #[serde(with = "serde_avro_bytes_opt")] - bytes_field2_opt: Option<[u8; 7]>, - #[serde(with = "serde_avro_bytes_opt")] - bytes_field2_opt2: Option<[u8; 8]>, - } - - let test = TestStructWithBytes { - array_field: &[1, 11, 111], - vec_field: vec![3, 33], - vec_field2: vec![4, 44], - vec_field2_opt: Some(vec![14, 144]), - vec_field2_opt2: None, - vec_field3: vec![5, 55], - vec_field3_opt: Some(vec![15, 155]), - vec_field3_opt2: None, - fixed_field: [1; 6], - fixed_field_opt: Some([6; 5]), - fixed_field_opt2: None, - fixed_field2: &[6, 66], - fixed_field2_opt: Some(&[7, 77]), - fixed_field2_opt2: None, - bytes_field: &[2, 22, 222], - bytes_field_opt: Some(&[3, 33, 233]), - bytes_field_opt2: None, - bytes_field2: [2; 6], - bytes_field2_opt: Some([2; 7]), - bytes_field2_opt2: None, - }; - let expected = Value::Record(vec![ - ( - "array_field".to_owned(), - Value::Array( - test.array_field - .iter() - .map(|&i| Value::Int(i as i32)) - .collect(), - ), - ), - ( - "vec_field".to_owned(), - Value::Array( - test.vec_field - .iter() - .map(|&i| Value::Int(i as i32)) - .collect(), - ), - ), - ( - "vec_field2".to_owned(), - Value::Fixed(test.vec_field2.len(), test.vec_field2.clone()), - ), - ( - "vec_field2_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Fixed( - test.vec_field2_opt.as_ref().unwrap().len(), - test.vec_field2_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "vec_field2_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "vec_field3".to_owned(), - Value::Bytes(test.vec_field3.clone()), - ), - ( - "vec_field3_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Bytes(test.vec_field3_opt.as_ref().unwrap().clone())), - ), - ), - ( - "vec_field3_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "fixed_field".to_owned(), - Value::Fixed(test.fixed_field.len(), test.fixed_field.to_vec()), - ), - ( - "fixed_field_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Fixed( - test.fixed_field_opt.as_ref().unwrap().len(), - test.fixed_field_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "fixed_field_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "fixed_field2".to_owned(), - Value::Fixed(test.fixed_field2.len(), test.fixed_field2.to_vec()), - ), - ( - "fixed_field2_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Fixed( - test.fixed_field2_opt.as_ref().unwrap().len(), - test.fixed_field2_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "fixed_field2_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "bytes_field".to_owned(), - Value::Bytes(test.bytes_field.to_vec()), - ), - ( - "bytes_field_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Bytes( - test.bytes_field_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "bytes_field_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ( - "bytes_field2".to_owned(), - Value::Bytes(test.bytes_field2.to_vec()), - ), - ( - "bytes_field2_opt".to_owned(), - Value::Union( - 1, - Box::new(Value::Bytes( - test.bytes_field2_opt.as_ref().unwrap().to_vec(), - )), - ), - ), - ( - "bytes_field2_opt2".to_owned(), - Value::Union(0, Box::new(Value::Null)), - ), - ]); - assert_eq!(expected, to_value(test).unwrap()); + crate::serde::slice_opt::deserialize(deserializer) } } diff --git a/avro/src/lib.rs b/avro/src/lib.rs index f370ff6..785d718 100644 --- a/avro/src/lib.rs +++ b/avro/src/lib.rs @@ -940,6 +940,7 @@ //! ``` mod bigdecimal; +mod bytes; mod codec; mod decimal; mod decode; @@ -959,7 +960,14 @@ pub mod types; pub mod util; pub mod validator; -pub use crate::bigdecimal::BigDecimal; +#[expect(deprecated)] +pub use crate::{ + bigdecimal::BigDecimal, + bytes::{ + serde_avro_bytes, serde_avro_bytes_opt, serde_avro_fixed, serde_avro_fixed_opt, + serde_avro_slice, serde_avro_slice_opt, + }, +}; #[cfg(feature = "bzip")] pub use codec::bzip::Bzip2Settings; #[cfg(feature = "xz")]
