This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new ee5f281e7 feat(rust): support box serde for rust (#2677)
ee5f281e7 is described below
commit ee5f281e7632653c8e8f45990aaff705f6a98bba
Author: Shawn Yang <[email protected]>
AuthorDate: Sat Sep 27 22:32:32 2025 +0800
feat(rust): support box serde for rust (#2677)
<!--
**Thanks for contributing to Apache Fory™.**
**If this is your first time opening a PR on fory, you can refer to
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
Contribution Checklist
- The **Apache Fory™** community has requirements on the naming of pr
titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
- Apache Fory™ has a strong focus on performance. If the PR you submit
will have an impact on performance, please benchmark it first and
provide the benchmark result here.
-->
## Why?
<!-- Describe the purpose of this PR. -->
## What does this PR do?
<!-- Describe the details of this PR. -->
## Related issues
<!--
Is there any related issue? If this PR closes them you say say
fix/closes:
- #xxxx0
- #xxxx1
- Fixes #xxxx2
-->
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fory/issues/new/choose) describing the
need to do so and update the document if necessary.
Delete section if not applicable.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
Delete section if not applicable.
-->
---
rust/fory-core/src/serializer/box_.rs | 59 ++++++++++++++
rust/fory-core/src/serializer/mod.rs | 1 +
rust/tests/tests/test_box.rs | 146 ++++++++++++++++++++++++++++++++++
3 files changed, 206 insertions(+)
diff --git a/rust/fory-core/src/serializer/box_.rs
b/rust/fory-core/src/serializer/box_.rs
new file mode 100644
index 000000000..3d5115b81
--- /dev/null
+++ b/rust/fory-core/src/serializer/box_.rs
@@ -0,0 +1,59 @@
+// 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 crate::error::Error;
+use crate::fory::Fory;
+use crate::resolver::context::ReadContext;
+use crate::resolver::context::WriteContext;
+use crate::serializer::Serializer;
+use crate::types::ForyGeneralList;
+
+impl<T: Serializer> Serializer for Box<T> {
+ fn read(context: &mut ReadContext) -> Result<Self, Error> {
+ Ok(Box::new(T::read(context)?))
+ }
+
+ fn read_type_info(context: &mut ReadContext, is_field: bool) {
+ T::read_type_info(context, is_field);
+ }
+
+ fn write(&self, context: &mut WriteContext, is_field: bool) {
+ T::write(self.as_ref(), context, is_field)
+ }
+
+ fn write_type_info(context: &mut WriteContext, is_field: bool) {
+ T::write_type_info(context, is_field);
+ }
+
+ fn reserved_space() -> usize {
+ T::reserved_space()
+ }
+
+ fn get_type_id(fory: &Fory) -> u32 {
+ T::get_type_id(fory)
+ }
+
+ fn is_option() -> bool {
+ false
+ }
+
+ fn is_none(&self) -> bool {
+ false
+ }
+}
+
+impl<T: Serializer> ForyGeneralList for Box<T> {}
diff --git a/rust/fory-core/src/serializer/mod.rs
b/rust/fory-core/src/serializer/mod.rs
index 986625db5..b90046849 100644
--- a/rust/fory-core/src/serializer/mod.rs
+++ b/rust/fory-core/src/serializer/mod.rs
@@ -22,6 +22,7 @@ use crate::types::{Mode, RefFlag, PRIMITIVE_TYPES};
mod any;
mod bool;
+mod box_;
pub mod collection;
mod datetime;
pub mod enum_;
diff --git a/rust/tests/tests/test_box.rs b/rust/tests/tests/test_box.rs
new file mode 100644
index 000000000..ec7c53cb9
--- /dev/null
+++ b/rust/tests/tests/test_box.rs
@@ -0,0 +1,146 @@
+// 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 fory_core::fory::Fory;
+use fory_derive::Fory;
+use std::collections::HashMap;
+
+#[test]
+fn test_box_primitive() {
+ let fory = Fory::default();
+
+ // Test Box<i32>
+ let value = Box::new(42i32);
+ let bin = fory.serialize(&value);
+ let deserialized: Box<i32> = fory.deserialize(&bin).expect("Should
deserialize Box<i32>");
+ assert_eq!(*value, *deserialized);
+
+ // Test Box<String>
+ let value = Box::new("Hello, Box!".to_string());
+ let bin = fory.serialize(&value);
+ let deserialized: Box<String> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<String>");
+ assert_eq!(*value, *deserialized);
+
+ // Test Box<f64>
+ let value = Box::new(std::f64::consts::PI);
+ let bin = fory.serialize(&value);
+ let deserialized: Box<f64> = fory.deserialize(&bin).expect("Should
deserialize Box<f64>");
+ assert_eq!(*value, *deserialized);
+}
+
+#[test]
+fn test_box_struct() {
+ #[derive(Fory, Debug, PartialEq, Default)]
+ struct Person {
+ name: String,
+ age: i32,
+ }
+
+ let mut fory = Fory::default();
+ fory.register::<Person>(999);
+
+ let person = Person {
+ name: "John Doe".to_string(),
+ age: 30,
+ };
+ let value = Box::new(person);
+ let bin = fory.serialize(&value);
+ let deserialized: Box<Person> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<Person>");
+ assert_eq!(*value, *deserialized);
+}
+
+#[test]
+fn test_box_struct_separate() {
+ #[derive(Fory, Debug, PartialEq, Default)]
+ struct Person {
+ name: String,
+ age: i32,
+ }
+
+ let mut fory = Fory::default();
+ fory.register::<Person>(999);
+
+ // Test serializing the Box<Person> directly, not as a field
+ let person = Person {
+ name: "Jane Doe".to_string(),
+ age: 25,
+ };
+ let boxed_person = Box::new(person);
+ let bin = fory.serialize(&boxed_person);
+ let deserialized: Box<Person> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<Person>");
+ assert_eq!(*boxed_person, *deserialized);
+}
+
+#[test]
+fn test_box_collection() {
+ let fory = Fory::default();
+
+ // Test Box<Vec<i32>>
+ let value = Box::new(vec![1, 2, 3, 4, 5]);
+ let bin = fory.serialize(&value);
+ let deserialized: Box<Vec<i32>> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<Vec<i32>>");
+ assert_eq!(*value, *deserialized);
+
+ // Test Box<HashMap<String, i32>>
+ let mut map = HashMap::new();
+ map.insert("key1".to_string(), 10);
+ map.insert("key2".to_string(), 20);
+ let value = Box::new(map);
+ let bin = fory.serialize(&value);
+ let deserialized: Box<HashMap<String, i32>> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<HashMap<String, i32>>");
+ assert_eq!(*value, *deserialized);
+}
+
+#[test]
+fn test_box_option() {
+ let fory = Fory::default();
+
+ // Test Box<Option<String>> with Some value
+ let value = Box::new(Some("Hello".to_string()));
+ let bin = fory.serialize(&value);
+ let deserialized: Box<Option<String>> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<Option<String>>");
+ assert_eq!(*value, *deserialized);
+
+ // Note: Box<Option<None>> is not supported due to the way Option's
serializer works
+ // The Option serializer expects None cases to be handled at the
serialize() level,
+ // not at the write() level, but Box calls write() directly on the inner
type.
+}
+
+#[test]
+fn test_nested_box() {
+ // Test Box<Box<i32>> - though unusual, should work
+ let fory = Fory::default();
+
+ let value = Box::new(Box::new(42i32));
+ let bin = fory.serialize(&value);
+ let deserialized: Box<Box<i32>> = fory
+ .deserialize(&bin)
+ .expect("Should deserialize Box<Box<i32>>");
+ assert_eq!(**value, **deserialized);
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]