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 ec6b8d26e perf(Rust): Enchance performance (#2810)
ec6b8d26e is described below
commit ec6b8d26e8760733c7b9bb4d67cc35d9d1abc419
Author: weipeng <[email protected]>
AuthorDate: Thu Oct 23 10:36:10 2025 +0800
perf(Rust): Enchance performance (#2810)
<!--
**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?
- Replacing `Mutex` by Spainlock which is implemented by CAS usually
more faster
- Replacing `dump` function by detach which could avoid memcpy
<!-- 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/buffer.rs | 11 +-
rust/fory-core/src/fory.rs | 16 +-
rust/fory-core/src/resolver/context.rs | 17 +-
rust/fory-core/src/util.rs | 69 ++-
rust/tests/tests/compatible/test_basic_type.rs | 684 ++++++++++++++++--------
rust/tests/tests/compatible/test_container.rs | 394 ++++++++------
rust/tests/tests/compatible/test_struct_enum.rs | 18 +-
rust/tests/tests/test_debug.rs | 3 +-
8 files changed, 791 insertions(+), 421 deletions(-)
diff --git a/rust/fory-core/src/buffer.rs b/rust/fory-core/src/buffer.rs
index 8b087c687..19be2241d 100644
--- a/rust/fory-core/src/buffer.rs
+++ b/rust/fory-core/src/buffer.rs
@@ -32,9 +32,14 @@ pub struct Writer {
impl Writer {
#[inline(always)]
- pub fn reset(&mut self) {
- // keep capacity and reset len to 0
- self.bf.clear();
+ pub fn attach_buffer(&mut self, bf: Vec<u8>) {
+ self.reserved = 0;
+ self.bf = bf;
+ }
+
+ #[inline(always)]
+ pub fn detach_buffer(&mut self) -> Vec<u8> {
+ std::mem::take(&mut self.bf)
}
#[inline(always)]
diff --git a/rust/fory-core/src/fory.rs b/rust/fory-core/src/fory.rs
index 1f664a18b..53c5f7b4f 100644
--- a/rust/fory-core/src/fory.rs
+++ b/rust/fory-core/src/fory.rs
@@ -82,8 +82,8 @@ pub struct Fory {
max_dyn_depth: u32,
check_struct_version: bool,
// Lazy-initialized pools (thread-safe, one-time initialization)
- write_context_pool: OnceLock<Pool<WriteContext>>,
- read_context_pool: OnceLock<Pool<ReadContext>>,
+ write_context_pool: OnceLock<Pool<Box<WriteContext>>>,
+ read_context_pool: OnceLock<Pool<Box<ReadContext>>>,
}
impl Default for Fory {
@@ -446,7 +446,7 @@ impl Fory {
let factory = move || {
let reader = Reader::new(&[]);
- ReadContext::new(
+ Box::new(ReadContext::new(
reader,
type_resolver.clone(),
compatible,
@@ -454,7 +454,7 @@ impl Fory {
xlang,
max_dyn_depth,
check_struct_version,
- )
+ ))
};
Pool::new(factory)
});
@@ -531,7 +531,7 @@ impl Fory {
let factory = move || {
let writer = Writer::default();
- WriteContext::new(
+ Box::new(WriteContext::new(
writer,
type_resolver.clone(),
compatible,
@@ -539,13 +539,12 @@ impl Fory {
compress_string,
xlang,
check_struct_version,
- )
+ ))
};
Pool::new(factory)
});
let mut context = pool.get();
let result = self.serialize_with_context(record, &mut context)?;
- context.writer.reset();
pool.put(context);
Ok(result)
}
@@ -555,6 +554,7 @@ impl Fory {
record: &T,
context: &mut WriteContext,
) -> Result<Vec<u8>, Error> {
+ context.writer.attach_buffer(vec![]);
let is_none = record.fory_is_none();
self.write_head::<T>(is_none, &mut context.writer);
let meta_start_offset = context.writer.len();
@@ -568,7 +568,7 @@ impl Fory {
}
}
context.reset();
- Ok(context.writer.dump())
+ Ok(context.writer.detach_buffer())
}
/// Registers a struct type with a numeric type ID for serialization.
diff --git a/rust/fory-core/src/resolver/context.rs
b/rust/fory-core/src/resolver/context.rs
index b85d46a0f..8c6646fcd 100644
--- a/rust/fory-core/src/resolver/context.rs
+++ b/rust/fory-core/src/resolver/context.rs
@@ -25,8 +25,8 @@ use
crate::resolver::metastring_resolver::{MetaStringReaderResolver, MetaStringW
use crate::resolver::ref_resolver::{RefReader, RefWriter};
use crate::resolver::type_resolver::{TypeInfo, TypeResolver};
use crate::types;
+use crate::util::Spinlock;
use std::rc::Rc;
-use std::sync::Mutex;
/// Serialization state container used on a single thread at a time.
/// Sharing the same instance across threads simultaneously causes undefined
behavior.
@@ -405,7 +405,7 @@ impl ReadContext {
}
pub struct Pool<T> {
- items: Mutex<Vec<T>>,
+ items: Spinlock<Vec<T>>,
factory: Box<dyn Fn() -> T + Send + Sync>,
}
@@ -415,26 +415,19 @@ impl<T> Pool<T> {
F: Fn() -> T + Send + Sync + 'static,
{
Pool {
- items: Mutex::new(vec![]),
+ items: Spinlock::new(vec![]),
factory: Box::new(factory),
}
}
#[inline(always)]
pub fn get(&self) -> T {
- let item = self
- .items
- .lock()
- .unwrap()
- .pop()
- .unwrap_or_else(|| (self.factory)());
- // println!("Object address: {:p}", &item);
- item
+ self.items.lock().pop().unwrap_or_else(|| (self.factory)())
}
// put back manually
#[inline(always)]
pub fn put(&self, item: T) {
- self.items.lock().unwrap().push(item);
+ self.items.lock().push(item);
}
}
diff --git a/rust/fory-core/src/util.rs b/rust/fory-core/src/util.rs
index a09f47f54..cca6c4772 100644
--- a/rust/fory-core/src/util.rs
+++ b/rust/fory-core/src/util.rs
@@ -17,7 +17,10 @@
use crate::types::TypeId;
use chrono::NaiveDate;
-use std::ptr;
+use std::cell::UnsafeCell;
+use std::ops::{Deref, DerefMut};
+use std::sync::atomic::{AtomicBool, Ordering};
+use std::{ptr, thread};
pub const EPOCH: NaiveDate = match NaiveDate::from_ymd_opt(1970, 1, 1) {
None => {
@@ -129,3 +132,67 @@ pub fn get_ext_actual_type_id(type_id: u32,
register_by_name: bool) -> u32 {
TypeId::EXT as u32
}
}
+
+pub struct Spinlock<T> {
+ data: UnsafeCell<T>,
+ flag: AtomicBool,
+}
+
+unsafe impl<T: Send> Send for Spinlock<T> {}
+unsafe impl<T: Sync> Sync for Spinlock<T> {}
+
+impl<T> Spinlock<T> {
+ pub fn new(data: T) -> Self {
+ Spinlock {
+ data: UnsafeCell::new(data),
+ flag: AtomicBool::new(false),
+ }
+ }
+
+ pub fn lock(&self) -> SpinlockGuard<'_, T> {
+ let mut spins = 0;
+ while self
+ .flag
+ .compare_exchange(false, true, Ordering::Acquire,
Ordering::Relaxed)
+ .is_err()
+ {
+ // Spin for a few iterations
+ if spins < 10 {
+ std::hint::spin_loop();
+ spins += 1;
+ } else {
+ // Then yield to the scheduler
+ thread::yield_now();
+ spins = 0; // reset spin counter
+ }
+ }
+ SpinlockGuard { lock: self }
+ }
+
+ fn unlock(&self) {
+ self.flag.store(false, Ordering::Release);
+ }
+}
+
+pub struct SpinlockGuard<'a, T> {
+ lock: &'a Spinlock<T>,
+}
+
+impl<'a, T> Drop for SpinlockGuard<'a, T> {
+ fn drop(&mut self) {
+ self.lock.unlock();
+ }
+}
+
+impl<'a, T> Deref for SpinlockGuard<'a, T> {
+ type Target = T;
+ fn deref(&self) -> &Self::Target {
+ unsafe { &*self.lock.data.get() }
+ }
+}
+
+impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ unsafe { &mut *self.lock.data.get() }
+ }
+}
diff --git a/rust/tests/tests/compatible/test_basic_type.rs
b/rust/tests/tests/compatible/test_basic_type.rs
index 9c1d008c6..a8f103168 100644
--- a/rust/tests/tests/compatible/test_basic_type.rs
+++ b/rust/tests/tests/compatible/test_basic_type.rs
@@ -44,436 +44,667 @@ const INT64_ARRAY: [i64; 1] = [51];
const FLOAT32_ARRAY: [f32; 1] = [52.0];
const FLOAT64_ARRAY: [f64; 1] = [53.0];
-fn serialize_non_null(fory: &Fory, context: &mut WriteContext) {
- fory.serialize_with_context(&BOOL_VAL, context).unwrap();
- fory.serialize_with_context(&I8_VAL, context).unwrap();
- fory.serialize_with_context(&I16_VAL, context).unwrap();
- fory.serialize_with_context(&I32_VAL, context).unwrap();
- fory.serialize_with_context(&I64_VAL, context).unwrap();
- fory.serialize_with_context(&F32_VAL, context).unwrap();
- fory.serialize_with_context(&F64_VAL, context).unwrap();
- fory.serialize_with_context(&STR_LATIN1_VAL.to_string(), context)
- .unwrap();
- fory.serialize_with_context(&LOCAL_DATE_VAL, context)
- .unwrap();
- fory.serialize_with_context(&TIMESTAMP_VAL, context)
- .unwrap();
-
- fory.serialize_with_context(&BOOL_ARRAY.to_vec(), context)
- .unwrap();
- fory.serialize_with_context(&INT8_ARRAY.to_vec(), context)
- .unwrap();
- fory.serialize_with_context(&INT16_ARRAY.to_vec(), context)
- .unwrap();
- fory.serialize_with_context(&INT32_ARRAY.to_vec(), context)
- .unwrap();
- fory.serialize_with_context(&INT64_ARRAY.to_vec(), context)
- .unwrap();
- fory.serialize_with_context(&FLOAT32_ARRAY.to_vec(), context)
- .unwrap();
- fory.serialize_with_context(&FLOAT64_ARRAY.to_vec(), context)
- .unwrap();
+fn serialize_non_null(fory: &Fory, context: &mut WriteContext) -> Vec<Vec<u8>>
{
+ vec![
+ fory.serialize_with_context(&BOOL_VAL, context).unwrap(),
+ fory.serialize_with_context(&I8_VAL, context).unwrap(),
+ fory.serialize_with_context(&I16_VAL, context).unwrap(),
+ fory.serialize_with_context(&I32_VAL, context).unwrap(),
+ fory.serialize_with_context(&I64_VAL, context).unwrap(),
+ fory.serialize_with_context(&F32_VAL, context).unwrap(),
+ fory.serialize_with_context(&F64_VAL, context).unwrap(),
+ fory.serialize_with_context(&STR_LATIN1_VAL.to_string(), context)
+ .unwrap(),
+ fory.serialize_with_context(&LOCAL_DATE_VAL, context)
+ .unwrap(),
+ fory.serialize_with_context(&TIMESTAMP_VAL, context)
+ .unwrap(),
+ fory.serialize_with_context(&BOOL_ARRAY.to_vec(), context)
+ .unwrap(),
+ fory.serialize_with_context(&INT8_ARRAY.to_vec(), context)
+ .unwrap(),
+ fory.serialize_with_context(&INT16_ARRAY.to_vec(), context)
+ .unwrap(),
+ fory.serialize_with_context(&INT32_ARRAY.to_vec(), context)
+ .unwrap(),
+ fory.serialize_with_context(&INT64_ARRAY.to_vec(), context)
+ .unwrap(),
+ fory.serialize_with_context(&FLOAT32_ARRAY.to_vec(), context)
+ .unwrap(),
+ fory.serialize_with_context(&FLOAT64_ARRAY.to_vec(), context)
+ .unwrap(),
+ ]
}
-fn serialize_nullable(fory: &Fory, context: &mut WriteContext) {
- fory.serialize_with_context(&Some(BOOL_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(I8_VAL), context).unwrap();
- fory.serialize_with_context(&Some(I16_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(I32_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(I64_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(F32_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(F64_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(STR_LATIN1_VAL.to_string()), context)
- .unwrap();
- fory.serialize_with_context(&Some(LOCAL_DATE_VAL), context)
- .unwrap();
- fory.serialize_with_context(&Some(TIMESTAMP_VAL), context)
- .unwrap();
-
- fory.serialize_with_context(&Some(BOOL_ARRAY.to_vec()), context)
- .unwrap();
- fory.serialize_with_context(&Some(INT8_ARRAY.to_vec()), context)
- .unwrap();
- fory.serialize_with_context(&Some(INT16_ARRAY.to_vec()), context)
- .unwrap();
- fory.serialize_with_context(&Some(INT32_ARRAY.to_vec()), context)
- .unwrap();
- fory.serialize_with_context(&Some(INT64_ARRAY.to_vec()), context)
- .unwrap();
- fory.serialize_with_context(&Some(FLOAT32_ARRAY.to_vec()), context)
- .unwrap();
- fory.serialize_with_context(&Some(FLOAT64_ARRAY.to_vec()), context)
- .unwrap();
-
- fory.serialize_with_context(&Option::<bool>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<i8>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<i16>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<i32>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<i64>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<f32>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<f64>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<String>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<NaiveDate>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<NaiveDateTime>::None, context)
- .unwrap();
-
- fory.serialize_with_context(&Option::<Vec<bool>>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<i8>>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<i16>>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<i32>>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<i64>>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<f32>>::None, context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<f64>>::None, context)
- .unwrap();
+fn serialize_nullable(fory: &Fory, context: &mut WriteContext) -> Vec<Vec<u8>>
{
+ vec![
+ fory.serialize_with_context(&Some(BOOL_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(I8_VAL), context).unwrap(),
+ fory.serialize_with_context(&Some(I16_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(I32_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(I64_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(F32_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(F64_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(STR_LATIN1_VAL.to_string()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(LOCAL_DATE_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(TIMESTAMP_VAL), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(BOOL_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(INT8_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(INT16_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(INT32_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(INT64_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(FLOAT32_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(FLOAT64_ARRAY.to_vec()), context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<bool>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<i8>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<i16>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<i32>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<i64>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<f32>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<f64>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<String>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<NaiveDate>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<NaiveDateTime>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<bool>>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<i8>>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<i16>>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<i32>>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<i64>>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<f32>>::None, context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<f64>>::None, context)
+ .unwrap(),
+ ]
}
-fn deserialize_non_null(fory: &Fory, context: &mut ReadContext, auto_conv:
bool, to_end: bool) {
+fn deserialize_non_null(fory: &Fory, mut bins: Vec<Vec<u8>>, auto_conv: bool) {
+ bins.reverse();
assert_eq!(
BOOL_VAL,
- fory.deserialize_with_context::<bool>(context).unwrap()
+ fory.deserialize_with_context::<bool>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
I8_VAL,
- fory.deserialize_with_context::<i8>(context).unwrap()
+ fory.deserialize_with_context::<i8>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
I16_VAL,
- fory.deserialize_with_context::<i16>(context).unwrap()
+ fory.deserialize_with_context::<i16>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
I32_VAL,
- fory.deserialize_with_context::<i32>(context).unwrap()
+ fory.deserialize_with_context::<i32>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
I64_VAL,
- fory.deserialize_with_context::<i64>(context).unwrap()
+ fory.deserialize_with_context::<i64>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
F32_VAL,
- fory.deserialize_with_context::<f32>(context).unwrap()
+ fory.deserialize_with_context::<f32>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
F64_VAL,
- fory.deserialize_with_context::<f64>(context).unwrap()
+ fory.deserialize_with_context::<f64>(&mut ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
STR_LATIN1_VAL.to_string(),
- fory.deserialize_with_context::<String>(context).unwrap()
+ fory.deserialize_with_context::<String>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
LOCAL_DATE_VAL,
- fory.deserialize_with_context::<NaiveDate>(context).unwrap()
+ fory.deserialize_with_context::<NaiveDate>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
TIMESTAMP_VAL,
- fory.deserialize_with_context::<NaiveDateTime>(context)
- .unwrap()
+ fory.deserialize_with_context::<NaiveDateTime>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
BOOL_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<bool>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<bool>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
INT8_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<i8>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i8>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
INT16_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<i16>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i16>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
INT32_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<i32>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
INT64_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<i64>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
FLOAT32_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<f32>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<f32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
FLOAT64_ARRAY.to_vec(),
- fory.deserialize_with_context::<Vec<f64>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<f64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
if auto_conv {
assert_eq!(
bool::default(),
- fory.deserialize_with_context::<bool>(context).unwrap()
+ fory.deserialize_with_context::<bool>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
i8::default(),
- fory.deserialize_with_context::<i8>(context).unwrap()
+ fory.deserialize_with_context::<i8>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
i16::default(),
- fory.deserialize_with_context::<i16>(context).unwrap()
+ fory.deserialize_with_context::<i16>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
i32::default(),
- fory.deserialize_with_context::<i32>(context).unwrap()
+ fory.deserialize_with_context::<i32>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
i64::default(),
- fory.deserialize_with_context::<i64>(context).unwrap()
+ fory.deserialize_with_context::<i64>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
f32::default(),
- fory.deserialize_with_context::<f32>(context).unwrap()
+ fory.deserialize_with_context::<f32>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
f64::default(),
- fory.deserialize_with_context::<f64>(context).unwrap()
+ fory.deserialize_with_context::<f64>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
String::default(),
- fory.deserialize_with_context::<String>(context).unwrap()
+ fory.deserialize_with_context::<String>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
NaiveDate::default(),
- fory.deserialize_with_context::<NaiveDate>(context).unwrap()
+ fory.deserialize_with_context::<NaiveDate>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
NaiveDateTime::default(),
- fory.deserialize_with_context::<NaiveDateTime>(context)
- .unwrap()
+ fory.deserialize_with_context::<NaiveDateTime>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<bool>::default(),
- fory.deserialize_with_context::<Vec<bool>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<bool>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<i8>::default(),
- fory.deserialize_with_context::<Vec<i8>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i8>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<i16>::default(),
- fory.deserialize_with_context::<Vec<i16>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i16>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<i32>::default(),
- fory.deserialize_with_context::<Vec<i32>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<i64>::default(),
- fory.deserialize_with_context::<Vec<i64>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<i64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<f32>::default(),
- fory.deserialize_with_context::<Vec<f32>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<f32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<f64>::default(),
- fory.deserialize_with_context::<Vec<f64>>(context).unwrap()
+ fory.deserialize_with_context::<Vec<f64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
}
- if to_end {
- assert_eq!(context.reader.slice_after_cursor().len(), 0);
- }
}
-fn deserialize_nullable(fory: &Fory, context: &mut ReadContext, auto_conv:
bool, to_end: bool) {
+fn deserialize_nullable(fory: &Fory, mut bins: Vec<Vec<u8>>, auto_conv: bool) {
+ bins.reverse();
assert_eq!(
Some(BOOL_VAL),
- fory.deserialize_with_context::<Option<bool>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<bool>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(I8_VAL),
- fory.deserialize_with_context::<Option<i8>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i8>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(I16_VAL),
- fory.deserialize_with_context::<Option<i16>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i16>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(I32_VAL),
- fory.deserialize_with_context::<Option<i32>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(I64_VAL),
- fory.deserialize_with_context::<Option<i64>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(F32_VAL),
- fory.deserialize_with_context::<Option<f32>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<f32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(F64_VAL),
- fory.deserialize_with_context::<Option<f64>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<f64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(STR_LATIN1_VAL.to_string()),
- fory.deserialize_with_context::<Option<String>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(LOCAL_DATE_VAL),
- fory.deserialize_with_context::<Option<NaiveDate>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<NaiveDate>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(TIMESTAMP_VAL),
- fory.deserialize_with_context::<Option<NaiveDateTime>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<NaiveDateTime>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(BOOL_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<bool>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<bool>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(INT8_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<i8>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i8>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(INT16_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<i16>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i16>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(INT32_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<i32>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i32>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(INT64_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<i64>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i64>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(FLOAT32_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<f32>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<f32>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
Some(FLOAT64_ARRAY.to_vec()),
- fory.deserialize_with_context::<Option<Vec<f64>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<f64>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
if !auto_conv {
assert_eq!(
None,
- fory.deserialize_with_context::<Option<bool>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<bool>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<i8>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i8>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<i16>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i16>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<i32>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<i64>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<i64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<f32>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<f32>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<f64>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<f64>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<String>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<NaiveDate>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<NaiveDate>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<NaiveDateTime>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<NaiveDateTime>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), fory)
+ )
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<bool>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<bool>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<i8>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i8>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<i16>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i16>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<i32>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i32>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<i64>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<i64>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<f32>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<f32>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
assert_eq!(
None,
- fory.deserialize_with_context::<Option<Vec<f64>>>(context)
- .unwrap()
+ fory.deserialize_with_context::<Option<Vec<f64>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ fory
+ ))
+ .unwrap()
);
}
- if to_end {
- assert_eq!(context.reader.slice_after_cursor().len(), 0);
- }
}
// non-null <-> non-null
@@ -483,12 +714,9 @@ fn basic() {
// serialize
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- serialize_non_null(&fory, &mut write_context);
+ let bins = serialize_non_null(&fory, &mut write_context);
// deserialize
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
- deserialize_non_null(&fory, &mut read_context, false, true);
+ deserialize_non_null(&fory, bins, false);
}
// nullable <-> nullable
@@ -498,12 +726,9 @@ fn basic_nullable() {
// serialize
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- serialize_nullable(&fory, &mut write_context);
+ let bins = serialize_nullable(&fory, &mut write_context);
// deserialize
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
- deserialize_nullable(&fory, &mut read_context, false, true);
+ deserialize_nullable(&fory, bins, false);
}
// non-null -> nullable -> non-null
@@ -513,19 +738,12 @@ fn auto_conv() {
// serialize_non-null
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- serialize_non_null(&fory, &mut write_context);
- // deserialize_nullable
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context: ReadContext = ReadContext::new_from_fory(reader,
&fory);
- deserialize_nullable(&fory, &mut read_context, true, true);
+ let bins = serialize_non_null(&fory, &mut write_context);
+ deserialize_nullable(&fory, bins, true);
// serialize_nullable
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- serialize_nullable(&fory, &mut write_context);
+ let bins = serialize_nullable(&fory, &mut write_context);
// deserialize_non-null
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
- deserialize_non_null(&fory, &mut read_context, true, true);
+ deserialize_non_null(&fory, bins, true);
}
diff --git a/rust/tests/tests/compatible/test_container.rs
b/rust/tests/tests/compatible/test_container.rs
index 0fc43c5ca..3c9f6854f 100644
--- a/rust/tests/tests/compatible/test_container.rs
+++ b/rust/tests/tests/compatible/test_container.rs
@@ -225,14 +225,9 @@ fn container_outer_auto_conv() {
// serialize_outer_non-null
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&basic_list(), &mut write_context)
+ let bytes = fory
+ .serialize_with_context(&basic_list(), &mut write_context)
.unwrap();
- fory.serialize_with_context(&basic_set(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&basic_map(), &mut write_context)
- .unwrap();
- // deserialize_outer_nullable
- let bytes = write_context.writer.dump();
let reader = Reader::new(bytes.as_slice());
let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
@@ -240,11 +235,22 @@ fn container_outer_auto_conv() {
fory.deserialize_with_context::<Option<Vec<String>>>(&mut read_context)
.unwrap()
);
+ let bytes = fory
+ .serialize_with_context(&basic_set(), &mut write_context)
+ .unwrap();
+ let reader = Reader::new(bytes.as_slice());
+ let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
Some(basic_set()),
fory.deserialize_with_context::<Option<HashSet<String>>>(&mut
read_context)
.unwrap()
);
+ let bytes = fory
+ .serialize_with_context(&basic_map(), &mut write_context)
+ .unwrap();
+ // deserialize_outer_nullable
+ let reader = Reader::new(bytes.as_slice());
+ let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
Some(basic_map()),
fory.deserialize_with_context::<Option<HashMap<String, String>>>(&mut
read_context)
@@ -254,51 +260,68 @@ fn container_outer_auto_conv() {
// serialize_outer_nullable
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&Some(basic_list()), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&Some(basic_set()), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&Some(basic_map()), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&Option::<Vec<String>>::None, &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&Option::<HashSet<String>>::None, &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&Option::<HashMap<String, String>>::None, &mut
write_context)
- .unwrap();
- // deserialize_outer_non-null
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
+ let mut bins = vec![
+ fory.serialize_with_context(&Some(basic_list()), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(basic_set()), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&Some(basic_map()), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<Vec<String>>::None, &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<HashSet<String>>::None, &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&Option::<HashMap<String, String>>::None,
&mut write_context)
+ .unwrap(),
+ ];
+ bins.reverse();
assert_eq!(
basic_list(),
- fory.deserialize_with_context::<Vec<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
basic_set(),
- fory.deserialize_with_context::<HashSet<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
basic_map(),
- fory.deserialize_with_context::<HashMap<String, String>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashMap<String, String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
Vec::<String>::default(),
- fory.deserialize_with_context::<Vec<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
HashSet::default(),
- fory.deserialize_with_context::<HashSet<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
HashMap::default(),
- fory.deserialize_with_context::<HashMap<String, String>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashMap<String, String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
}
@@ -313,67 +336,88 @@ fn collection_inner() {
// serialize
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&basic_list(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&item_list(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&basic_set(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&item_set(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_basic_list(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_item_list(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_basic_set(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_item_set(false), &mut
write_context)
- .unwrap();
+ let mut bins = vec![
+ fory.serialize_with_context(&basic_list(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&item_list(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&basic_set(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&item_set(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_basic_list(false), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_item_list(false), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_basic_set(false), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_item_set(false), &mut
write_context)
+ .unwrap(),
+ ];
+ bins.reverse();
// deserialize
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
basic_list(),
- fory.deserialize_with_context::<Vec<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
item_list(),
- fory.deserialize_with_context::<Vec<Item>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<Item>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
basic_set(),
- fory.deserialize_with_context::<HashSet<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
item_set(),
- fory.deserialize_with_context::<HashSet<Item>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<Item>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
nullable_basic_list(false),
- fory.deserialize_with_context::<Vec<Option<String>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<Option<String>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
nullable_item_list(false),
- fory.deserialize_with_context::<Vec<Option<Item>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<Option<Item>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
nullable_basic_set(false),
- fory.deserialize_with_context::<HashSet<Option<String>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<Option<String>>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), &fory)
+ )
+ .unwrap()
);
assert_eq!(
nullable_item_set(false),
- fory.deserialize_with_context::<HashSet<Option<Item>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<Option<Item>>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), &fory)
+ )
+ .unwrap()
);
- assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
}
}
@@ -387,75 +431,95 @@ fn collection_inner_auto_conv() {
// serialize_non_null
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&basic_list(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&item_list(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&basic_set(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&item_set(), &mut write_context)
- .unwrap();
+ let mut bins = vec![
+ fory.serialize_with_context(&basic_list(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&item_list(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&basic_set(), &mut write_context)
+ .unwrap(),
+ fory.serialize_with_context(&item_set(), &mut write_context)
+ .unwrap(),
+ ];
+ bins.reverse();
// deserialize_nullable
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
nullable_basic_list(true),
- fory.deserialize_with_context::<Vec<Option<String>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<Option<String>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
nullable_item_list(true),
- fory.deserialize_with_context::<Vec<Option<Item>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<Option<Item>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
nullable_basic_set(true),
- fory.deserialize_with_context::<HashSet<Option<String>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<Option<String>>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), &fory)
+ )
+ .unwrap()
);
assert_eq!(
nullable_item_set(true),
- fory.deserialize_with_context::<HashSet<Option<Item>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<Option<Item>>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), &fory)
+ )
+ .unwrap()
);
- assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
// serialize_nullable
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&nullable_basic_list(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_item_list(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_basic_set(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_item_set(false), &mut
write_context)
- .unwrap();
+ let mut bins = vec![
+ fory.serialize_with_context(&nullable_basic_list(false), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_item_list(false), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_basic_set(false), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&nullable_item_set(false), &mut
write_context)
+ .unwrap(),
+ ];
+ bins.reverse();
// deserialize_non-null
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
basic_list(),
- fory.deserialize_with_context::<Vec<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
item_list(),
- fory.deserialize_with_context::<Vec<Item>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<Item>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
basic_set(),
- fory.deserialize_with_context::<HashSet<String>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<String>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
item_set(),
- fory.deserialize_with_context::<HashSet<Item>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashSet<Item>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
- assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
}
}
@@ -469,41 +533,49 @@ fn map_inner() {
// serialize
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&basic_map(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&item_map(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_basic_map(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_item_map(false), &mut
write_context)
+ let bytes = fory
+ .serialize_with_context(&basic_map(), &mut write_context)
.unwrap();
- // deserialize
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
basic_map(),
- fory.deserialize_with_context::<HashMap<String, String>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashMap<String, String>>(
+ &mut ReadContext::new_from_fory(Reader::new(bytes.as_slice()),
&fory)
+ )
+ .unwrap()
);
+
+ let bytes = fory
+ .serialize_with_context(&item_map(), &mut write_context)
+ .unwrap();
assert_eq!(
item_map(),
- fory.deserialize_with_context::<HashMap<Item, Item>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashMap<Item, Item>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bytes.as_slice()),
+ &fory
+ ))
+ .unwrap()
);
+
+ let bytes = fory
+ .serialize_with_context(&nullable_basic_map(false), &mut
write_context)
+ .unwrap();
assert_eq!(
nullable_basic_map(false),
fory.deserialize_with_context::<HashMap<Option<String>,
Option<String>>>(
- &mut read_context
+ &mut ReadContext::new_from_fory(Reader::new(bytes.as_slice()),
&fory)
)
.unwrap()
);
+ let bytes = fory
+ .serialize_with_context(&nullable_item_map(false), &mut
write_context)
+ .unwrap();
assert_eq!(
nullable_item_map(false),
- fory.deserialize_with_context::<HashMap<Option<Item>,
Option<Item>>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<HashMap<Option<Item>,
Option<Item>>>(
+ &mut ReadContext::new_from_fory(Reader::new(bytes.as_slice()),
&fory)
+ )
+ .unwrap()
);
- assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
}
}
@@ -517,12 +589,10 @@ fn map_inner_auto_conv() {
// serialize_non_null
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&basic_map(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&item_map(), &mut write_context)
+ let bytes = fory
+ .serialize_with_context(&basic_map(), &mut write_context)
.unwrap();
// deserialize_nullable
- let bytes = write_context.writer.dump();
let reader = Reader::new(bytes.as_slice());
let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
@@ -532,6 +602,12 @@ fn map_inner_auto_conv() {
)
.unwrap()
);
+ let bytes = fory
+ .serialize_with_context(&item_map(), &mut write_context)
+ .unwrap();
+ // deserialize_nullable
+ let reader = Reader::new(bytes.as_slice());
+ let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
nullable_item_map(true),
fory.deserialize_with_context::<HashMap<Option<Item>,
Option<Item>>>(&mut read_context)
@@ -541,12 +617,10 @@ fn map_inner_auto_conv() {
// serialize_nullable
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&nullable_basic_map(false), &mut
write_context)
- .unwrap();
- fory.serialize_with_context(&nullable_item_map(false), &mut
write_context)
+ let bytes = fory
+ .serialize_with_context(&nullable_basic_map(false), &mut
write_context)
.unwrap();
// deserialize_non-null
- let bytes = write_context.writer.dump();
let reader = Reader::new(bytes.as_slice());
let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
@@ -554,6 +628,12 @@ fn map_inner_auto_conv() {
fory.deserialize_with_context::<HashMap<String, String>>(&mut
read_context)
.unwrap()
);
+ let bytes = fory
+ .serialize_with_context(&nullable_item_map(false), &mut
write_context)
+ .unwrap();
+ // deserialize_non-null
+ let reader = Reader::new(bytes.as_slice());
+ let mut read_context = ReadContext::new_from_fory(reader, &fory);
assert_eq!(
item_map(),
fory.deserialize_with_context::<HashMap<Item, Item>>(&mut
read_context)
@@ -572,30 +652,36 @@ fn complex() {
for fory in [fory1, fory2] {
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
- fory.serialize_with_context(&nested_collection(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&complex_container1(), &mut write_context)
- .unwrap();
- fory.serialize_with_context(&complex_container2(), &mut write_context)
- .unwrap();
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
+ let mut bins = vec![
+ fory.serialize_with_context(&nested_collection(), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&complex_container1(), &mut
write_context)
+ .unwrap(),
+ fory.serialize_with_context(&complex_container2(), &mut
write_context)
+ .unwrap(),
+ ];
+ bins.reverse();
assert_eq!(
nested_collection(),
- fory.deserialize_with_context::<Vec<HashSet<Item>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<HashSet<Item>>>(&mut
ReadContext::new_from_fory(
+ Reader::new(bins.pop().unwrap().as_slice()),
+ &fory
+ ))
+ .unwrap()
);
assert_eq!(
complex_container1(),
- fory.deserialize_with_context::<Vec<HashMap<Item, Item>>>(&mut
read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<HashMap<Item, Item>>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), &fory)
+ )
+ .unwrap()
);
assert_eq!(
complex_container2(),
- fory.deserialize_with_context::<Vec<HashMap<Vec<Item>,
Vec<Item>>>>(&mut read_context)
- .unwrap()
+ fory.deserialize_with_context::<Vec<HashMap<Vec<Item>,
Vec<Item>>>>(
+ &mut
ReadContext::new_from_fory(Reader::new(bins.pop().unwrap().as_slice()), &fory)
+ )
+ .unwrap()
);
- assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
}
}
diff --git a/rust/tests/tests/compatible/test_struct_enum.rs
b/rust/tests/tests/compatible/test_struct_enum.rs
index 0d5d92ba6..9919ecc32 100644
--- a/rust/tests/tests/compatible/test_struct_enum.rs
+++ b/rust/tests/tests/compatible/test_struct_enum.rs
@@ -97,24 +97,26 @@ fn basic() {
let writer = Writer::default();
let mut write_context = WriteContext::new_from_fory(writer, &fory);
let person = Person::default();
- fory.serialize_with_context(&person, &mut write_context)
+ let bytes1 = fory
+ .serialize_with_context(&person, &mut write_context)
.unwrap();
- fory.serialize_with_context(&person, &mut write_context)
+ let bytes2 = fory
+ .serialize_with_context(&person, &mut write_context)
.unwrap();
- let bytes = write_context.writer.dump();
- let reader = Reader::new(bytes.as_slice());
- let mut read_context = ReadContext::new_from_fory(reader, &fory);
+ let reader1 = Reader::new(bytes1.as_slice());
+ let mut read_context1 = ReadContext::new_from_fory(reader1, &fory);
assert_eq!(
person,
- fory.deserialize_with_context::<Person>(&mut read_context)
+ fory.deserialize_with_context::<Person>(&mut read_context1)
.unwrap()
);
+ let reader2 = Reader::new(bytes2.as_slice());
+ let mut read_context2 = ReadContext::new_from_fory(reader2, &fory);
assert_eq!(
person,
- fory.deserialize_with_context::<Person>(&mut read_context)
+ fory.deserialize_with_context::<Person>(&mut read_context2)
.unwrap()
);
- assert_eq!(read_context.reader.slice_after_cursor().len(), 0);
}
}
diff --git a/rust/tests/tests/test_debug.rs b/rust/tests/tests/test_debug.rs
index 3aa50da91..96b59ed7d 100644
--- a/rust/tests/tests/test_debug.rs
+++ b/rust/tests/tests/test_debug.rs
@@ -155,10 +155,9 @@ fn debug_hooks_trigger_for_struct() {
fory_compat.register::<DebugSample>(4001).unwrap();
let writer = Writer::default();
let mut write_ctx = WriteContext::new_from_fory(writer, &fory_compat);
- fory_compat
+ let compat_bytes = fory_compat
.serialize_with_context(&sample, &mut write_ctx)
.unwrap();
- let compat_bytes = write_ctx.writer.dump();
let reader = Reader::new(compat_bytes.as_slice());
let mut read_ctx = ReadContext::new_from_fory(reader, &fory_compat);
let _: DebugSample = fory_compat.deserialize_with_context(&mut
read_ctx).unwrap();
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]