This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 1179278 Add an example of SpecificSingleObjectWriter/Reader usage
(#159)
1179278 is described below
commit 1179278b76177d672f6e514be027e080edd2a736
Author: Martin Grigorov <[email protected]>
AuthorDate: Thu Mar 13 07:53:52 2025 +0200
Add an example of SpecificSingleObjectWriter/Reader usage (#159)
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(())
+}