From 75343ef5842a51e6e5e3b9eeb9ed31422d2f1f48 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Tue, 10 Sep 2024 16:39:05 -0700 Subject: [PATCH] bevy_reflect: Mention `FunctionRegistry` in `bevy_reflect::func` docs (#15147) # Objective The module docs for `bevy_reflect::func` don't mention the `FunctionRegistry`. ## Solution Add a section about the `FunctionRegistry` to the module-level documentation. ## Testing You can test locally by running: ``` cargo test --doc --package bevy_reflect --all-features ``` --- crates/bevy_reflect/src/func/mod.rs | 31 ++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/bevy_reflect/src/func/mod.rs b/crates/bevy_reflect/src/func/mod.rs index a924231ff4..770f0fa0c2 100644 --- a/crates/bevy_reflect/src/func/mod.rs +++ b/crates/bevy_reflect/src/func/mod.rs @@ -57,7 +57,7 @@ //! Closures, on the other hand, are special functions that do capture their environment. //! These are always defined with anonymous function syntax. //! -//! ```rust +//! ``` //! // A closure that captures an immutable reference to a variable //! let c = 123; //! let add = |a: i32, b: i32| a + b + c; @@ -94,6 +94,35 @@ //! For other functions that don't conform to one of the above signatures, //! [`DynamicFunction`] and [`DynamicFunctionMut`] can instead be created manually. //! +//! # Function Registration +//! +//! This module also provides a [`FunctionRegistry`] that can be used to register functions and closures +//! by name so that they may be retrieved and called dynamically. +//! +//! ``` +//! # use bevy_reflect::func::{ArgList, FunctionRegistry}; +//! fn add(a: i32, b: i32) -> i32 { +//! a + b +//! } +//! +//! let mut registry = FunctionRegistry::default(); +//! +//! // You can register functions and methods by their `std::any::type_name`: +//! registry.register(add).unwrap(); +//! +//! // Or you can register them by a custom name: +//! registry.register_with_name("mul", |a: i32, b: i32| a * b).unwrap(); +//! +//! // You can then retrieve and call these functions by name: +//! let reflect_add = registry.get(std::any::type_name_of_val(&add)).unwrap(); +//! let value = reflect_add.call(ArgList::default().push_owned(10_i32).push_owned(5_i32)).unwrap(); +//! assert_eq!(value.unwrap_owned().try_downcast_ref::(), Some(&15)); +//! +//! let reflect_mul = registry.get("mul").unwrap(); +//! let value = reflect_mul.call(ArgList::default().push_owned(10_i32).push_owned(5_i32)).unwrap(); +//! assert_eq!(value.unwrap_owned().try_downcast_ref::(), Some(&50)); +//! ``` +//! //! [`PartialReflect`]: crate::PartialReflect //! [`Reflect`]: crate::Reflect //! [lack of variadic generics]: https://poignardazur.github.io/2024/05/25/report-on-rustnl-variadics/