mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 11:03:06 +00:00
24 lines
549 B
Rust
24 lines
549 B
Rust
|
use crate::{FromType, Reflect};
|
||
|
|
||
|
/// A struct used to provide the default value of a type.
|
||
|
///
|
||
|
/// A [`ReflectDefault`] for type `T` can be obtained via [`FromType::from_type`].
|
||
|
#[derive(Clone)]
|
||
|
pub struct ReflectDefault {
|
||
|
default: fn() -> Box<dyn Reflect>,
|
||
|
}
|
||
|
|
||
|
impl ReflectDefault {
|
||
|
pub fn default(&self) -> Box<dyn Reflect> {
|
||
|
(self.default)()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T: Reflect + Default> FromType<T> for ReflectDefault {
|
||
|
fn from_type() -> Self {
|
||
|
ReflectDefault {
|
||
|
default: || Box::new(T::default()),
|
||
|
}
|
||
|
}
|
||
|
}
|