Skip to content
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

Add Array typehints #639

Merged
merged 1 commit into from Feb 3, 2021
Merged

Add Array typehints #639

merged 1 commit into from Feb 3, 2021

Conversation

Taywee
Copy link
Contributor

@Taywee Taywee commented Dec 2, 2020

This is still completely untested yet.

This draft PR is for code change review and discussion, potentially addressing (or partially addressing at least) #638

Some of it may be able to be trimmed down, made clearer, or better expressed. For instance, I'm not completely thrilled with ArrayHint, and I think there might be a better way around that with generics, but it would possibly involve some API change, as then VariantArray couldn't use the generic version to export due to needing to be able to implement Export with multiple Hint types.

Comment on lines 12 to 11
/// Need the Hint trait so that nested Array hints work.
pub trait Hint: fmt::Debug {
fn export_info(&self) -> ExportInfo;
}

impl Hint for () {
fn export_info(&self) -> ExportInfo {
panic!("Unit hint should never be called, only used as a hintless placeholder.");
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be avoided by using Export as the trait bound in ArrayHint::with_subhint instead.

/// Hints for Array, allowing nesting
#[derive(Debug)]
pub struct ArrayHint {
element_type: Option<Box<dyn Hint>>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boxing is unnecessary because you can just store an ExportInfo here.

ArrayHint { element_type: None }
}

pub fn with_subhint<T>(hint: T) -> Self
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps with_element_hint would be a better name for this?

(
VariantType::VariantArray,
sys::godot_property_hint_GODOT_PROPERTY_HINT_TYPE_STRING,
) => format!("19:{}", subinfo.hint_string),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this 19 value exported as a constant somewhere in the C API? If so, would it be better to use that constant instead of a hardcoded magic number?

@Taywee
Copy link
Contributor Author

Taywee commented Dec 2, 2020

All good points. I've tried to address them here. Still untested. I'll set aside some time to test them tonight, in 10 hours or so.

@Taywee
Copy link
Contributor Author

Taywee commented Dec 3, 2020

I've set up a simple example that looks to be working as expected for me. So far, it just sets some simple properties of single and double arrays with and without hints.

@Taywee Taywee marked this pull request as ready for review December 3, 2020 05:27
Comment on lines 12 to 11
/// Need the Hint trait so that nested Array hints work.
pub trait Hint: fmt::Debug {
fn export_info(self) -> ExportInfo;
}

impl Hint for () {
fn export_info(self) -> ExportInfo {
panic!("Unit hint should never be called, only used as a hintless placeholder.");
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If hint types remain as associated types, is this trait still necessary?

Comment on lines 427 to 430
pub fn with_element_hint<T>(hint: T) -> Self
where
T: Hint,
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can be used to export arrays of resources, because Ref<T> has the hint type of (). The constructor would just panic with the current implementation, and would not be able to know which type it's supposed to export even if that is changed.

@ghost ghost added the feature Adds functionality to the library label Dec 4, 2020
@Taywee Taywee changed the title Initial testing push for Array typehints Add Array typehints Dec 4, 2020
@ghost ghost added this to the 0.9.2 milestone Jan 26, 2021
@ghost ghost added the breaking-change Issues and PRs that are breaking to fix/merge. label Feb 1, 2021
@ghost ghost modified the milestones: 0.9.2, 0.10 Feb 1, 2021
@ghost
Copy link

ghost commented Feb 1, 2021

Since the PR is being inactive, I have taken the liberty to make some changes and squash the commits. Since this is technically a breaking change (changing the Export::Hint type of VariantArray from () to ArrayHint), it is postponed to 0.10.

In the future, we might want to make the "no hint available" hint type a "never" type, so that users will have to use None for those properties. This will make it no longer a breaking change to add hints for those types.

@Taywee
Copy link
Contributor Author

Taywee commented Feb 2, 2021

Ah, thanks. Sorry this one slipped through the cracks. I meant to clean it up but it fell entirely off my radar.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.9.3 is released, so merging this now. Thanks for the PR!

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 3, 2021

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Issues and PRs that are breaking to fix/merge. feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant