pub trait ForyDefault: Sized {
// Required method
fn fory_default() -> Self;
}Expand description
Trait for creating default values during Fory deserialization.
ForyDefault is similar to Rust’s standard Default trait but specifically designed
for Fory’s serialization framework. It provides a way to create “null” or default
values when deserializing optional or nullable types.
§Why not use Default?
We can’t add a blanket implementation impl<T: Default> ForyDefault for T because
it would conflict with potential future implementations in upstream crates. For example,
the standard library might add impl Default for Rc<dyn Any> in the future, which would
cause a trait coherence conflict.
§When to implement
You should implement ForyDefault when:
- Creating custom types that can be serialized with Fory
- Your type needs to support nullable/optional representations
- You want to define what the “null” or “default” value means for deserialization
§Examples
use fory_core::ForyDefault;
#[derive(Debug, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl ForyDefault for Point {
fn fory_default() -> Self {
Point { x: 0, y: 0 }
}
}
let default_point = Point::fory_default();
assert_eq!(default_point, Point { x: 0, y: 0 });For types that already implement Default, you can simply delegate:
use fory_core::ForyDefault;
#[derive(Default)]
struct Config {
timeout: u32,
retries: u32,
}
impl ForyDefault for Config {
fn fory_default() -> Self {
Default::default()
}
}Required Methods§
Sourcefn fory_default() -> Self
fn fory_default() -> Self
Creates a default value for this type.
This is used by Fory when deserializing null values or when a default instance is needed during the deserialization process.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".