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

Made UIRect initialisation functions const #16823

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Kees-van-Beilen
Copy link
Contributor

Objective

Destructuring in const code blocks isn't allowed, thus using UIRect in const code can be a hassle as it initialisation function aren't const. This Pr makes them const.

Solution

Removed all destructuring in the UIRect implementation

Testing

  • I've ran a few ui examples to check if i didn't make a mistake,

@chompaa chompaa added D-Trivial Nice and easy! A great choice to get started with Bevy A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 14, 2024
Copy link
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

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

Looks good, all the fields match up correctly and the examples still work.
Strongly prefer struct update syntax for the constructor functions though.

Comment on lines 460 to 465
pub const fn left(value: Val) -> Self {
let mut rect = UiRect::DEFAULT;
rect.left = value;
rect
}

Copy link
Contributor

@ickshonpe ickshonpe Dec 14, 2024

Choose a reason for hiding this comment

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

I'd prefer that we used struct update syntax for the constructor functions, like:

Suggested change
pub const fn left(value: Val) -> Self {
let mut rect = UiRect::DEFAULT;
rect.left = value;
rect
}
pub const fn left(left: Val) -> Self {
Self {
left,
..Self::DEFAULT
}
}

is simpler and clearer.

Copy link
Contributor Author

@Kees-van-Beilen Kees-van-Beilen Dec 14, 2024

Choose a reason for hiding this comment

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

That's the entire reason I've made this PR, ..Self::DEFAULT cannot be used in const functions. I also would love to use that notation, but it is going to be a while before rust will support this Sorry I assumed that that wasn't possible as the exact same scenario didn't work on my computer, but i just checked your code change suggestion and that does work. Will refactor and update 👍 thank you

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought you can, can't you? Self::DEFAULT is a const value, it's not Self::default()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently when a struct implements Drop or one of its field implements Drop then you cannot use the struct updater syntax. So for instance you can't write:

const MY_NODE:Node = Node {..Node::DEFAULT};

because Node has a field of type Vec which implements Drop. However this shouldn't be a problem in UIRect's case.

Copy link
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

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

Everything looks good now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants