Skip to content

Commit eb2292b

Browse files
authored
docs(rust): update rust serializer trait doc (#2795)
## Why? <!-- Describe the purpose of this PR. --> ## What does this PR do? update rust serializer trait doc ## 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. -->
1 parent cedba30 commit eb2292b

File tree

9 files changed

+1243
-54
lines changed

9 files changed

+1243
-54
lines changed

rust/fory-core/src/resolver/type_resolver.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ impl TypeResolver {
484484
T2::fory_write(v, context, write_ref_info, write_type_info, has_generics)
485485
}
486486
None => Err(Error::type_error(format!(
487-
"Cast type error when writing: {:?}",
487+
"Cast type to {:?} error when writing: {:?}",
488+
std::any::type_name::<T2>(),
488489
T2::fory_static_type_id()
489490
))),
490491
}
@@ -510,7 +511,11 @@ impl TypeResolver {
510511
let this = this.downcast_ref::<T2>();
511512
match this {
512513
Some(v) => T2::fory_write_data_generic(v, context, has_generics),
513-
None => todo!(),
514+
None => Err(Error::type_error(format!(
515+
"Cast type to {:?} error when writing data: {:?}",
516+
std::any::type_name::<T2>(),
517+
T2::fory_static_type_id()
518+
))),
514519
}
515520
}
516521

@@ -660,7 +665,8 @@ impl TypeResolver {
660665
Ok(v.fory_write(context, write_ref_info, write_type_info, has_generics)?)
661666
}
662667
None => Err(Error::type_error(format!(
663-
"Cast type error when writing: {:?}",
668+
"Cast type to {:?} error when writing: {:?}",
669+
std::any::type_name::<T2>(),
664670
T2::fory_static_type_id()
665671
))),
666672
}
@@ -686,7 +692,11 @@ impl TypeResolver {
686692
let this = this.downcast_ref::<T2>();
687693
match this {
688694
Some(v) => T2::fory_write_data_generic(v, context, has_generics),
689-
None => todo!(),
695+
None => Err(Error::type_error(format!(
696+
"Cast type to {:?} error when writing data: {:?}",
697+
std::any::type_name::<T2>(),
698+
T2::fory_static_type_id()
699+
))),
690700
}
691701
}
692702

rust/fory-core/src/serializer/any.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ impl Serializer for Box<dyn Any> {
9999
}
100100

101101
fn fory_read_data(_: &mut ReadContext) -> Result<Self, Error> {
102-
panic!(
103-
"fory_read_data should not be called directly on polymorphic Rc<dyn Any> trait object"
104-
);
102+
Err(Error::not_allowed(
103+
"fory_read_data should not be called directly on polymorphic Rc<dyn Any> trait object",
104+
))
105105
}
106106

107107
fn fory_get_type_id(_: &TypeResolver) -> Result<u32, Error> {
@@ -267,10 +267,10 @@ impl Serializer for Rc<dyn Any> {
267267
}
268268

269269
fn fory_read_data(_: &mut ReadContext) -> Result<Self, Error> {
270-
panic!(
270+
Err(Error::not_allowed(format!(
271271
"fory_read_data should not be called directly on polymorphic Rc<dyn {}> trait object",
272272
stringify!($trait_name)
273-
);
273+
)))
274274
}
275275

276276
fn fory_get_type_id(_: &TypeResolver) -> Result<u32, Error> {
@@ -436,10 +436,10 @@ impl Serializer for Arc<dyn Any> {
436436
}
437437

438438
fn fory_read_data(_: &mut ReadContext) -> Result<Self, Error> {
439-
panic!(
439+
Err(Error::not_allowed(format!(
440440
"fory_read_data should not be called directly on polymorphic Rc<dyn {}> trait object",
441441
stringify!($trait_name)
442-
);
442+
)))
443443
}
444444

445445
fn fory_get_type_id(_type_resolver: &TypeResolver) -> Result<u32, Error> {

rust/fory-core/src/serializer/arc.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ impl<T: Serializer + ForyDefault + Send + Sync + 'static> Serializer for Arc<T>
6161
}
6262

6363
fn fory_write_data_generic(&self, _: &mut WriteContext, _: bool) -> Result<(), Error> {
64-
panic!("Arc<T> should be written using `fory_write` to handle reference tracking properly");
64+
Err(Error::not_allowed(
65+
"Arc<T> should be written using `fory_write` to handle reference tracking properly",
66+
))
6567
}
6668

6769
fn fory_write_data(&self, _: &mut WriteContext) -> Result<(), Error> {
68-
panic!("Arc<T> should be written using `fory_write` to handle reference tracking properly");
70+
Err(Error::not_allowed(
71+
"Arc<T> should be written using `fory_write` to handle reference tracking properly",
72+
))
6973
}
7074

7175
fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
@@ -93,7 +97,7 @@ impl<T: Serializer + ForyDefault + Send + Sync + 'static> Serializer for Arc<T>
9397
}
9498

9599
fn fory_read_data(_: &mut ReadContext) -> Result<Self, Error> {
96-
panic!("Arc<T> should be read using `fory_read/fory_read_with_type_info` to handle reference tracking properly");
100+
Err(Error::not_allowed("Arc<T> should be read using `fory_read/fory_read_with_type_info` to handle reference tracking properly"))
97101
}
98102

99103
fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {

0 commit comments

Comments
 (0)