-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
base: main
Are you sure you want to change the base?
Conversation
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.
Looks good, all the fields match up correctly and the examples still work.
Strongly prefer struct update syntax for the constructor functions though.
crates/bevy_ui/src/geometry.rs
Outdated
pub const fn left(value: Val) -> Self { | ||
let mut rect = UiRect::DEFAULT; | ||
rect.left = value; | ||
rect | ||
} | ||
|
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.
I'd prefer that we used struct update syntax for the constructor functions, like:
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.
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.
That's the entire reason I've made this PR, 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..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
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.
I thought you can, can't you? Self::DEFAULT
is a const value, it's not Self::default()
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.
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.
This reverts commit 1e41f52.
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.
Everything looks good now.
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