-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
bevy_reflect: Function registry #14098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alice-i-cecile
merged 11 commits into
bevyengine:main
from
MrGVSV:mrgvsv/reflect/function-registry
Aug 6, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
73baaad
Make FunctionInfo::name required
MrGVSV b55faef
Add FunctionRegistry
MrGVSV e4f7c7e
Add AppFunctionRegistry
MrGVSV 231d817
Fix docs on App
MrGVSV 03dc89a
Require functions to be Send + Sync
MrGVSV 6d61b57
Add FunctionRegistrationError
MrGVSV 61c55cf
Fix doc
MrGVSV 83f6169
Update docs to include valid inputs for registration
MrGVSV 8ecd6d6
Remove FunctionRegistry::get_mut
MrGVSV 9b7d13a
Require a name for FunctionRegistry::register
MrGVSV 6ce018a
Add doc examples
MrGVSV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,8 +60,7 @@ use crate::func::{FunctionResult, IntoFunction, ReturnInfo}; | |
| /// | ||
| /// // Instead, we need to define the function manually. | ||
| /// // We start by defining the shape of the function: | ||
| /// let info = FunctionInfo::new() | ||
| /// .with_name("append") | ||
| /// let info = FunctionInfo::new("append") | ||
| /// .with_arg::<String>("value") | ||
| /// .with_arg::<&mut Vec<String>>("list") | ||
| /// .with_return::<&mut String>(); | ||
|
|
@@ -93,7 +92,7 @@ use crate::func::{FunctionResult, IntoFunction, ReturnInfo}; | |
| /// [module-level documentation]: crate::func | ||
| pub struct DynamicFunction { | ||
| info: FunctionInfo, | ||
| func: Arc<dyn for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + 'static>, | ||
| func: Arc<dyn for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + Send + Sync + 'static>, | ||
| } | ||
|
|
||
| impl DynamicFunction { | ||
|
|
@@ -103,7 +102,7 @@ impl DynamicFunction { | |
| /// | ||
| /// It's important that the function signature matches the provided [`FunctionInfo`]. | ||
| /// This info may be used by consumers of the function for validation and debugging. | ||
| pub fn new<F: for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + 'static>( | ||
| pub fn new<F: for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + Send + Sync + 'static>( | ||
| func: F, | ||
| info: FunctionInfo, | ||
| ) -> Self { | ||
|
|
@@ -162,16 +161,29 @@ impl DynamicFunction { | |
| pub fn info(&self) -> &FunctionInfo { | ||
| &self.info | ||
| } | ||
|
|
||
| /// The [name] of the function. | ||
| /// | ||
| /// For [`DynamicFunctions`] created using [`IntoFunction`], | ||
| /// the name will always be the full path to the function as returned by [`std::any::type_name`]. | ||
| /// This can be overridden using [`with_name`]. | ||
| /// | ||
| /// [name]: FunctionInfo::name | ||
| /// [`DynamicFunctions`]: DynamicFunction | ||
| /// [`with_name`]: Self::with_name | ||
| pub fn name(&self) -> &Cow<'static, str> { | ||
| self.info.name() | ||
| } | ||
| } | ||
|
|
||
| /// Outputs the function signature. | ||
| /// | ||
| /// This takes the format: `DynamicFunction(fn {name}({arg1}: {type1}, {arg2}: {type2}, ...) -> {return_type})`. | ||
| /// | ||
| /// Names for arguments and the function itself are optional and will default to `_` if not provided. | ||
| /// Names for arguments are optional and will default to `_` if not provided. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doc line seems out of date.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Argument names are still optional since we can't infer these through the type system unfortunately :( |
||
| impl Debug for DynamicFunction { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
| let name = self.info.name().unwrap_or("_"); | ||
| let name = self.name(); | ||
| write!(f, "DynamicFunction(fn {name}(")?; | ||
|
|
||
| for (index, arg) in self.info.args().iter().enumerate() { | ||
|
|
@@ -215,7 +227,7 @@ mod tests { | |
| fn foo() {} | ||
|
|
||
| let func = foo.into_function().with_name("my_function"); | ||
| assert_eq!(func.info().name(), Some("my_function")); | ||
| assert_eq!(func.info().name(), "my_function"); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -241,8 +253,7 @@ mod tests { | |
| let index = args.pop::<usize>()?; | ||
| Ok(Return::Ref(get(index, list))) | ||
| }, | ||
| FunctionInfo::new() | ||
| .with_name("get") | ||
| FunctionInfo::new("get") | ||
| .with_arg::<usize>("index") | ||
| .with_arg::<&Vec<String>>("list") | ||
| .with_return::<&String>(), | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we panic here or just return the
Result?If we return the
Result, then users will be forced tounwrapit themselves, but it gives them more options on handling the error (not that they can't just get theAppFunctionRegistrydirectly).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long term, I would prefer is this returned a result. But since none of the other methods on
Appdo currently, I vote we panic for consistency. We can take a more general look at error handling during app setup later.