Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/std_traits.rs
6598 views
1
//! Module containing the [`ReflectDefault`] type.
2
3
use crate::{FromType, Reflect};
4
use 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)]
10
pub struct ReflectDefault {
11
default: fn() -> Box<dyn Reflect>,
12
}
13
14
impl ReflectDefault {
15
/// Returns the default value for a type.
16
pub fn default(&self) -> Box<dyn Reflect> {
17
(self.default)()
18
}
19
}
20
21
impl<T: Reflect + Default> FromType<T> for ReflectDefault {
22
fn from_type() -> Self {
23
ReflectDefault {
24
default: || Box::<T>::default(),
25
}
26
}
27
}
28
29