This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch add-example-for-single-object in repository https://gitbox.apache.org/repos/asf/avro-rs.git
commit ddab4ba9c76a90b05e6168e6ff145bde527d8208 Author: Martin Tzvetanov Grigorov <[email protected]> AuthorDate: Wed Mar 12 14:46:44 2025 +0200 Add an example of SpecificSingleObjectWriter/Reader usage Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> --- avro/examples/specific_single_object.rs | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/avro/examples/specific_single_object.rs b/avro/examples/specific_single_object.rs new file mode 100644 index 0000000..03ce25a --- /dev/null +++ b/avro/examples/specific_single_object.rs @@ -0,0 +1,53 @@ +// 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 apache_avro::{AvroSchema, SpecificSingleObjectReader, SpecificSingleObjectWriter}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize, AvroSchema, PartialEq)] +struct Test { + a: i64, + b: String, +} + +fn main() -> anyhow::Result<()> { + let mut buffer: Vec<u8> = Vec::new(); + let test = Test { + a: 27, + b: "foo".to_string(), + }; + + let mut writer = SpecificSingleObjectWriter::<Test>::with_capacity(1024)?; + match writer.write(test.clone(), &mut buffer) { + Ok(bytes_written) => { + assert_eq!(bytes_written, 15); + assert_eq!( + buffer, + vec![195, 1, 166, 59, 243, 49, 82, 230, 8, 161, 54, 6, 102, 111, 111] + ); + } + Err(err) => { + panic!("Error during serialization: {:?}", err); + } + } + + let reader = SpecificSingleObjectReader::<Test>::new()?; + let read = reader.read(&mut buffer.as_slice())?; + assert_eq!(test, read); + + Ok(()) +}
