mirror of
https://github.com/bevyengine/bevy
synced 2024-12-23 03:23:20 +00:00
df61117850
# Objective #13152 added support for reflecting functions. Now, we need a way to register those functions such that they may be accessed anywhere within the ECS. ## Solution Added a `FunctionRegistry` type similar to `TypeRegistry`. This allows a function to be registered and retrieved by name. ```rust fn foo() -> i32 { 123 } let mut registry = FunctionRegistry::default(); registry.register("my_function", foo); let function = registry.get_mut("my_function").unwrap(); let value = function.call(ArgList::new()).unwrap().unwrap_owned(); assert_eq!(value.downcast_ref::<i32>(), Some(&123)); ``` Additionally, I added an `AppFunctionRegistry` resource which wraps a `FunctionRegistryArc`. Functions can be registered into this resource using `App::register_function` or by getting a mutable reference to the resource itself. ### Limitations #### `Send + Sync` In order to get this registry to work across threads, it needs to be `Send + Sync`. This means that `DynamicFunction` needs to be `Send + Sync`, which means that its internal function also needs to be `Send + Sync`. In most cases, this won't be an issue because standard Rust functions (the type most likely to be registered) are always `Send + Sync`. Additionally, closures tend to be `Send + Sync` as well, granted they don't capture any `!Send` or `!Sync` variables. This PR adds this `Send + Sync` requirement, but as mentioned above, it hopefully shouldn't be too big of an issue. #### Closures Unfortunately, closures can't be registered yet. This will likely be explored and added in a followup PR. ### Future Work Besides addressing the limitations listed above, another thing we could look into is improving the lookup of registered functions. One aspect is in the performance of hashing strings. The other is in the developer experience of having to call `std::any::type_name_of_val` to get the name of their function (assuming they didn't give it a custom name). ## Testing You can run the tests locally with: ``` cargo test --package bevy_reflect ``` --- ## Changelog - Added `FunctionRegistry` - Added `AppFunctionRegistry` (a `Resource` available from `bevy_ecs`) - Added `FunctionRegistryArc` - Added `FunctionRegistrationError` - Added `reflect_functions` feature to `bevy_ecs` and `bevy_app` - `FunctionInfo` is no longer `Default` - `DynamicFunction` now requires its wrapped function be `Send + Sync` ## Internal Migration Guide > [!important] > Function reflection was introduced as part of the 0.15 dev cycle. This migration guide was written for developers relying on `main` during this cycle, and is not a breaking change coming from 0.14. `DynamicFunction` (both those created manually and those created with `IntoFunction`), now require `Send + Sync`. All standard Rust functions should meet that requirement. Closures, on the other hand, may not if they capture any `!Send` or `!Sync` variables from its environment.
59 lines
1.5 KiB
TOML
59 lines
1.5 KiB
TOML
[package]
|
|
name = "bevy_ecs"
|
|
version = "0.15.0-dev"
|
|
edition = "2021"
|
|
description = "Bevy Engine's entity component system"
|
|
homepage = "https://bevyengine.org"
|
|
repository = "https://github.com/bevyengine/bevy"
|
|
license = "MIT OR Apache-2.0"
|
|
keywords = ["ecs", "game", "bevy"]
|
|
categories = ["game-engines", "data-structures"]
|
|
rust-version = "1.77.0"
|
|
|
|
[features]
|
|
default = ["bevy_reflect"]
|
|
trace = []
|
|
multi_threaded = ["bevy_tasks/multi_threaded", "arrayvec"]
|
|
bevy_debug_stepping = []
|
|
serialize = ["dep:serde"]
|
|
track_change_detection = []
|
|
reflect_functions = ["bevy_reflect", "bevy_reflect/functions"]
|
|
|
|
[dependencies]
|
|
bevy_ptr = { path = "../bevy_ptr", version = "0.15.0-dev" }
|
|
bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", optional = true }
|
|
bevy_tasks = { path = "../bevy_tasks", version = "0.15.0-dev" }
|
|
bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev" }
|
|
bevy_ecs_macros = { path = "macros", version = "0.15.0-dev" }
|
|
|
|
petgraph = "0.6"
|
|
bitflags = "2.3"
|
|
concurrent-queue = "2.4.0"
|
|
fixedbitset = "0.5"
|
|
serde = { version = "1", optional = true, default-features = false }
|
|
thiserror = "1.0"
|
|
nonmax = "0.5"
|
|
arrayvec = { version = "0.7.4", optional = true }
|
|
|
|
[dev-dependencies]
|
|
rand = "0.8"
|
|
static_assertions = "1.1.0"
|
|
|
|
[[example]]
|
|
name = "events"
|
|
path = "examples/events.rs"
|
|
|
|
[[example]]
|
|
name = "resources"
|
|
path = "examples/resources.rs"
|
|
|
|
[[example]]
|
|
name = "change_detection"
|
|
path = "examples/change_detection.rs"
|
|
|
|
[lints]
|
|
workspace = true
|
|
|
|
[package.metadata.docs.rs]
|
|
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
|
|
all-features = true
|