bevy_reflect/
std_traits.rs

1//! Module containing the [`ReflectDefault`] type.
2
3use crate::{FromType, Reflect};
4use alloc::boxed::Box;
5
6/// A struct used to provide the default value of a type.
7///
8/// A [`ReflectDefault`] for type `T` can be obtained via [`FromType::from_type`].
9#[derive(Clone)]
10pub struct ReflectDefault {
11    default: fn() -> Box<dyn Reflect>,
12}
13
14impl ReflectDefault {
15    /// Returns the default value for a type.
16    pub fn default(&self) -> Box<dyn Reflect> {
17        (self.default)()
18    }
19}
20
21impl<T: Reflect + Default> FromType<T> for ReflectDefault {
22    fn from_type() -> Self {
23        ReflectDefault {
24            default: || Box::<T>::default(),
25        }
26    }
27}