Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_scene/src/reflect_utils.rs
6598 views
1
use bevy_reflect::{PartialReflect, ReflectFromReflect, TypeRegistration};
2
3
/// Attempts to clone a [`PartialReflect`] value using various methods.
4
///
5
/// This first attempts to clone via [`PartialReflect::reflect_clone`].
6
/// then falls back to [`ReflectFromReflect::from_reflect`],
7
/// and finally [`PartialReflect::to_dynamic`] if the first two methods fail.
8
///
9
/// This helps ensure that the original type and type data is retained,
10
/// and only returning a dynamic type if all other methods fail.
11
pub(super) fn clone_reflect_value(
12
value: &dyn PartialReflect,
13
type_registration: &TypeRegistration,
14
) -> Box<dyn PartialReflect> {
15
value
16
.reflect_clone()
17
.map(PartialReflect::into_partial_reflect)
18
.unwrap_or_else(|_| {
19
type_registration
20
.data::<ReflectFromReflect>()
21
.and_then(|fr| fr.from_reflect(value.as_partial_reflect()))
22
.map(PartialReflect::into_partial_reflect)
23
.unwrap_or_else(|| value.to_dynamic())
24
})
25
}
26
27