ForyDefault

Trait ForyDefault 

Source
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§

Source

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", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ForyDefault for bool

Source§

impl ForyDefault for f32

Source§

impl ForyDefault for f64

Source§

impl ForyDefault for i8

Source§

impl ForyDefault for i16

Source§

impl ForyDefault for i32

Source§

impl ForyDefault for i64

Source§

impl ForyDefault for Box<dyn Any>

Source§

impl ForyDefault for Box<dyn Serializer>

Source§

impl ForyDefault for Rc<dyn Any>

Source§

impl ForyDefault for String

Source§

impl ForyDefault for Arc<dyn Any>

Source§

impl ForyDefault for NaiveDate

Source§

impl ForyDefault for NaiveDateTime

Source§

impl<K, V> ForyDefault for BTreeMap<K, V>

Source§

impl<K, V> ForyDefault for HashMap<K, V>

Source§

impl<T> ForyDefault for Option<T>
where T: ForyDefault,

Source§

impl<T> ForyDefault for Box<T>
where T: ForyDefault,

Source§

impl<T> ForyDefault for BinaryHeap<T>
where T: Ord,

Source§

impl<T> ForyDefault for BTreeSet<T>

Source§

impl<T> ForyDefault for LinkedList<T>

Source§

impl<T> ForyDefault for VecDeque<T>

Source§

impl<T> ForyDefault for Rc<T>
where T: ForyDefault,

Source§

impl<T> ForyDefault for Arc<T>
where T: ForyDefault,

Source§

impl<T> ForyDefault for Vec<T>

Source§

impl<T> ForyDefault for RefCell<T>
where T: ForyDefault,

Source§

impl<T> ForyDefault for HashSet<T>

Source§

impl<T> ForyDefault for Mutex<T>
where T: ForyDefault,

Implementors§

Source§

impl<T> ForyDefault for ArcWeak<T>
where T: ForyDefault,

Source§

impl<T> ForyDefault for RcWeak<T>
where T: ForyDefault,