Skip to content

Commit 66c7eff

Browse files
committed
Add doc examples
1 parent 9b7d13a commit 66c7eff

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

crates/bevy_app/src/app.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,59 @@ impl App {
605605

606606
/// Registers the given function into the [`AppFunctionRegistry`] resource using the given name.
607607
///
608-
/// See [`bevy_reflect::func::FunctionRegistry::register`] for more information.
608+
/// To avoid conflicts, it's recommended to use a unique name for the function.
609+
/// This can be achieved by either using the function's [type name] or
610+
/// by "namespacing" the function with a unique identifier,
611+
/// such as the name of your crate.
612+
///
613+
/// For example, to register a function, `add`, from a crate, `my_crate`,
614+
/// you could use the name, `"my_crate::add"`.
615+
///
616+
/// Only functions that implement [`IntoFunction`] may be registered via this method.
617+
///
618+
/// See [`FunctionRegistry::register`] for more information.
619+
///
620+
/// # Panics
621+
///
622+
/// Panics if a function has already been registered with the given name.
623+
///
624+
/// # Examples
625+
///
626+
/// ```
627+
/// use bevy_app::App;
628+
///
629+
/// fn yell(text: String) {
630+
/// println!("{}!", text);
631+
/// }
632+
///
633+
/// App::new()
634+
/// // Registering an anonymous function with a unique name
635+
/// .register_function("my_crate::yell_louder", |text: String| {
636+
/// println!("{}!!!", text.to_uppercase());
637+
/// })
638+
/// // Registering an existing function with its type name
639+
/// .register_function(std::any::type_name_of_val(&yell), yell)
640+
/// // Registering an existing function with a custom name
641+
/// .register_function("my_crate::yell", yell);
642+
/// ```
643+
///
644+
/// Names must be unique.
645+
///
646+
/// ```should_panic
647+
/// use bevy_app::App;
648+
///
649+
/// fn one() {}
650+
/// fn two() {}
651+
///
652+
/// App::new()
653+
/// .register_function("my_function", one)
654+
/// // Panic! A function has already been registered with the name "my_function"
655+
/// .register_function("my_function", two);
656+
/// ```
657+
///
658+
/// [type name]: std::any::type_name
659+
/// [`IntoFunction`]: bevy_reflect::func::IntoFunction
660+
/// [`FunctionRegistry`]: bevy_reflect::func::FunctionRegistry::register
609661
#[cfg(feature = "reflect_functions")]
610662
pub fn register_function<F, Marker>(
611663
&mut self,

crates/bevy_reflect/src/func/registry.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,43 @@ impl FunctionRegistry {
4545
/// This method is a convenience around calling [`IntoFunction::into_function`] and [`DynamicFunction::with_name`]
4646
/// on the function and inserting it into the registry using the [`register_dynamic`] method.
4747
///
48+
/// # Examples
49+
///
50+
/// ```
51+
/// # use bevy_reflect::func::{FunctionRegistrationError, FunctionRegistry};
52+
/// fn mul(a: i32, b: i32) -> i32 {
53+
/// a * b
54+
/// }
55+
///
56+
/// # fn main() -> Result<(), FunctionRegistrationError> {
57+
/// let mut registry = FunctionRegistry::default();
58+
/// registry
59+
/// // Registering an anonymous function with a unique name
60+
/// .register("my_crate::add", |a: i32, b: i32| {
61+
/// a + b
62+
/// })?
63+
/// // Registering an existing function with its type name
64+
/// .register(std::any::type_name_of_val(&mul), mul)?
65+
/// // Registering an existing function with a custom name
66+
/// .register("my_crate::mul", mul)?;
67+
/// # Ok(())
68+
/// # }
69+
/// ```
70+
///
71+
/// Names must be unique.
72+
///
73+
/// ```should_panic
74+
/// # use bevy_reflect::func::FunctionRegistry;
75+
/// fn one() {}
76+
/// fn two() {}
77+
///
78+
/// let mut registry = FunctionRegistry::default();
79+
/// registry.register("my_function", one).unwrap();
80+
///
81+
/// // Panic! A function has already been registered with the name "my_function"
82+
/// registry.register("my_function", two).unwrap();
83+
/// ```
84+
///
4885
/// [name]: DynamicFunction::name
4986
/// [`overwrite_registration`]: Self::overwrite_registration
5087
/// [type name]: std::any::type_name
@@ -70,6 +107,42 @@ impl FunctionRegistry {
70107
///
71108
/// You can change the name of the function using [`DynamicFunction::with_name`].
72109
///
110+
/// # Examples
111+
///
112+
/// ```
113+
/// # use bevy_reflect::func::{DynamicFunction, FunctionRegistrationError, FunctionRegistry, IntoFunction};
114+
/// fn add(a: i32, b: i32) -> i32 {
115+
/// a + b
116+
/// }
117+
///
118+
/// # fn main() -> Result<(), FunctionRegistrationError> {
119+
/// let mut registry = FunctionRegistry::default();
120+
///
121+
/// // Register a `DynamicFunction` directly
122+
/// let function: DynamicFunction = add.into_function();
123+
/// registry.register_dynamic(function)?;
124+
///
125+
/// // Register a `DynamicFunction` with a custom name
126+
/// let function: DynamicFunction = add.into_function().with_name("my_crate::add");
127+
/// registry.register_dynamic(function)?;
128+
/// # Ok(())
129+
/// # }
130+
/// ```
131+
///
132+
/// Names must be unique.
133+
///
134+
/// ```should_panic
135+
/// # use bevy_reflect::func::{DynamicFunction, FunctionRegistry, IntoFunction};
136+
/// fn one() {}
137+
/// fn two() {}
138+
///
139+
/// let mut registry = FunctionRegistry::default();
140+
/// registry.register_dynamic(one.into_function().with_name("my_function")).unwrap();
141+
///
142+
/// // Panic! A function has already been registered with the name "my_function"
143+
/// registry.register_dynamic(two.into_function().with_name("my_function")).unwrap();
144+
/// ```
145+
///
73146
/// [name]: DynamicFunction::name
74147
/// [`overwrite_registration_dynamic`]: Self::overwrite_registration_dynamic
75148
pub fn register_dynamic(

0 commit comments

Comments
 (0)