Path: blob/main/crates/bevy_ecs/src/reflect/from_world.rs
6600 views
//! Definitions for [`FromWorld`] reflection.1//! This allows creating instances of types that are known only at runtime and2//! require an `&mut World` to be initialized.3//!4//! This module exports two types: [`ReflectFromWorldFns`] and [`ReflectFromWorld`].5//!6//! Same as [`component`](`super::component`), but for [`FromWorld`].78use alloc::boxed::Box;9use bevy_reflect::{FromType, Reflect};1011use crate::world::{FromWorld, World};1213/// A struct used to operate on the reflected [`FromWorld`] trait of a type.14///15/// A [`ReflectFromWorld`] for type `T` can be obtained via16/// [`bevy_reflect::TypeRegistration::data`].17#[derive(Clone)]18pub struct ReflectFromWorld(ReflectFromWorldFns);1920/// The raw function pointers needed to make up a [`ReflectFromWorld`].21#[derive(Clone)]22pub struct ReflectFromWorldFns {23/// Function pointer implementing [`ReflectFromWorld::from_world()`].24pub from_world: fn(&mut World) -> Box<dyn Reflect>,25}2627impl ReflectFromWorldFns {28/// Get the default set of [`ReflectFromWorldFns`] for a specific type using its29/// [`FromType`] implementation.30///31/// This is useful if you want to start with the default implementation before overriding some32/// of the functions to create a custom implementation.33pub fn new<T: Reflect + FromWorld>() -> Self {34<ReflectFromWorld as FromType<T>>::from_type().035}36}3738impl ReflectFromWorld {39/// Constructs default reflected [`FromWorld`] from world using [`from_world()`](FromWorld::from_world).40pub fn from_world(&self, world: &mut World) -> Box<dyn Reflect> {41(self.0.from_world)(world)42}4344/// Create a custom implementation of [`ReflectFromWorld`].45///46/// This is an advanced feature,47/// useful for scripting implementations,48/// that should not be used by most users49/// unless you know what you are doing.50///51/// Usually you should derive [`Reflect`] and add the `#[reflect(FromWorld)]` bundle52/// to generate a [`ReflectFromWorld`] implementation automatically.53///54/// See [`ReflectFromWorldFns`] for more information.55pub fn new(fns: ReflectFromWorldFns) -> Self {56Self(fns)57}5859/// The underlying function pointers implementing methods on `ReflectFromWorld`.60///61/// This is useful when you want to keep track locally of an individual62/// function pointer.63///64/// Calling [`TypeRegistry::get`] followed by65/// [`TypeRegistration::data::<ReflectFromWorld>`] can be costly if done several66/// times per frame. Consider cloning [`ReflectFromWorld`] and keeping it67/// between frames, cloning a `ReflectFromWorld` is very cheap.68///69/// If you only need a subset of the methods on `ReflectFromWorld`,70/// use `fn_pointers` to get the underlying [`ReflectFromWorldFns`]71/// and copy the subset of function pointers you care about.72///73/// [`TypeRegistration::data::<ReflectFromWorld>`]: bevy_reflect::TypeRegistration::data74/// [`TypeRegistry::get`]: bevy_reflect::TypeRegistry::get75pub fn fn_pointers(&self) -> &ReflectFromWorldFns {76&self.077}78}7980impl<B: Reflect + FromWorld> FromType<B> for ReflectFromWorld {81fn from_type() -> Self {82ReflectFromWorld(ReflectFromWorldFns {83from_world: |world| Box::new(B::from_world(world)),84})85}86}878889