-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
Use the class_name for the field when building PropertyInfo #441
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-441 |
Getting some new errors in unchanged files with the update to rust 1.73.0. Added fixes in a new commit, let me know if you want me to squash them or move it to a separate PR. |
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.
Thanks! 🙂
The logic around the actual bugfix is solely in property.rs
, if I see correctly?
Could you elaborate what precisely was broken and how it's fixed?
Clippy fixes are fine as-is in a separate commit 👍
@@ -68,10 +68,10 @@ macro_rules! impl_builtin_traits_inner { | |||
impl Eq for $Type {} | |||
}; | |||
|
|||
( PartialOrd for $Type:ty => $gd_method:ident ) => { |
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.
Nice improvement!
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.
Thanks! Clippy actually flagged the whole PartialOrd
implementation as "incorrect" because there was an implementation for Ord
which was interesting.
#[godot_api] | ||
impl ResourceVirtual for CustomResource { | ||
fn init(_base: godot::obj::Base<Self::Base>) -> Self { | ||
Self {} | ||
} | ||
} |
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.
Such blocks can be replaced with #[class(..., init)]
(also below).
// `class_name` should be empty for non-Object variants. | ||
assert_eq!( | ||
property.get_or_nil("class_name"), | ||
"CustomResource".to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("type"), | ||
(VariantType::Object as i32).to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("hint"), | ||
(PropertyHint::PROPERTY_HINT_RESOURCE_TYPE.ord()).to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("hint_string"), | ||
"CustomResource".to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("usage"), | ||
(PropertyUsageFlags::PROPERTY_USAGE_DEFAULT.ord() | ||
| PropertyUsageFlags::PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT.ord()) | ||
.to_variant() | ||
); | ||
|
||
let property = class | ||
.get_property_list() | ||
.iter_shared() | ||
.find(|c| c.get_or_nil("name") == "bar".to_variant()) | ||
.unwrap(); | ||
// `class_name` should be empty for non-Object variants. | ||
assert_eq!( | ||
property.get_or_nil("class_name"), | ||
"NewNameCustomResource".to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("type"), | ||
(VariantType::Object as i32).to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("hint"), | ||
(PropertyHint::PROPERTY_HINT_RESOURCE_TYPE.ord()).to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("hint_string"), | ||
"NewNameCustomResource".to_variant() | ||
); | ||
assert_eq!( | ||
property.get_or_nil("usage"), | ||
PropertyUsageFlags::PROPERTY_USAGE_DEFAULT | ||
.ord() | ||
.to_variant() | ||
); |
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.
You have a lot of code following the same pattern, which can be extracted into a function.
fn check_property(property: &Dictionary, key: &str, expected: impl ToVariant) {
assert_eq!(property.get_or_nil(key), expected.to_variant());
}
This would then simply the code to:
// `class_name` should be empty for non-Object variants. | |
assert_eq!( | |
property.get_or_nil("class_name"), | |
"CustomResource".to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("type"), | |
(VariantType::Object as i32).to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("hint"), | |
(PropertyHint::PROPERTY_HINT_RESOURCE_TYPE.ord()).to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("hint_string"), | |
"CustomResource".to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("usage"), | |
(PropertyUsageFlags::PROPERTY_USAGE_DEFAULT.ord() | |
| PropertyUsageFlags::PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT.ord()) | |
.to_variant() | |
); | |
let property = class | |
.get_property_list() | |
.iter_shared() | |
.find(|c| c.get_or_nil("name") == "bar".to_variant()) | |
.unwrap(); | |
// `class_name` should be empty for non-Object variants. | |
assert_eq!( | |
property.get_or_nil("class_name"), | |
"NewNameCustomResource".to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("type"), | |
(VariantType::Object as i32).to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("hint"), | |
(PropertyHint::PROPERTY_HINT_RESOURCE_TYPE.ord()).to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("hint_string"), | |
"NewNameCustomResource".to_variant() | |
); | |
assert_eq!( | |
property.get_or_nil("usage"), | |
PropertyUsageFlags::PROPERTY_USAGE_DEFAULT | |
.ord() | |
.to_variant() | |
); | |
// `class_name` should be empty for non-Object variants. | |
check_property(&property, "class_name", "CustomResource"); | |
check_property(&property, "type", VariantType::Object as i32); | |
check_property(&property, "hint", PropertyHint::PROPERTY_HINT_RESOURCE_TYPE.ord()); | |
check_property(&property, "hint_string", "CustomResource"); | |
check_property(&property, "usage", PropertyUsageFlags::PROPERTY_USAGE_DEFAULT.ord() | |
| PropertyUsageFlags::PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT.ord()); | |
let property = class | |
.get_property_list() | |
.iter_shared() | |
.find(|c| c.get_or_nil("name") == "bar".to_variant()) | |
.unwrap(); | |
// `class_name` should be empty for non-Object variants. | |
check_property(&property, "class_name", "NewNameCustomResource"); | |
check_property(&property, "type", VariantType::Object as i32); | |
check_property(&property, "hint", PropertyHint::PROPERTY_HINT_RESOURCE_TYPE.ord()); | |
check_property(&property, "hint_string", "NewNameCustomResource"); | |
check_property(&property, "usage", PropertyUsageFlags::PROPERTY_USAGE_DEFAULT.ord()); |
(Maybe check for mistakes)
Godot uses the class_name to construct an instance of the right type in a few places, in particular for the PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT flag.
Yeah, the fix is just the changes in There might be other places where this comes up but that's the case I was running into. |
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.
One small comment, but it's not important, we can clean this up once we fix the bug to remove empty impls.
Thanks a lot for this! You said it fixes #440 only partially, so I guess that issue needs to stay open?
#[godot_api] | ||
impl ResourceVirtual for CustomResource {} |
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.
The whole impl
is not needed anymore 😉
(the inherent impl
above is still required due to a bug, though...)
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.
Neat, when did that happen? Thought I was getting errors for not having that pretty recently
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.
The inherent one is still needed, but not the trait one.
Yeah #440 should stay open. There's another issue with exported |
Godot uses the class_name to construct an instance of the right type in a few places, in particular for the PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT flag.
Partial fix for #440