-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 const builder methods to bevy_ui #5529
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
Comments
alice-i-cecile
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
labels
Aug 1, 2022
Two quick questions:
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)]
pub enum PositionType {
/// Relative to all other nodes with the [`PositionType::Relative`] value
#[default]
Relative,
/// Independent of all other nodes
///
/// As usual, the `Style.position` field of this node is specified relative to its parent node
Absolute,
} becomes: /// The strategy used to position this node
#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)]
pub enum PositionType {
/// Relative to all other nodes with the [`PositionType::Relative`] value
Relative,
/// Independent of all other nodes
///
/// As usual, the `Style.position` field of this node is specified relative to its parent node
Absolute,
}
impl PositionType {
const DEFAULT: Self = Self::Relative;
}
impl Default for PositionType {
fn default() -> Self {
Self::DEFAULT
}
}
We lose the convenience and visibility of the macro but this is otherwise fine and could then be removed if/when rust gets const defaults. |
CC @Weibye @TimJentzsch; we'll likely want to pursue this upstream in |
alradish
pushed a commit
to alradish/bevy
that referenced
this issue
Jan 22, 2023
# Objective - Fixes bevyengine#5529 ## Solution - Add assosciated constants named DEFAULT to as many types as possible - Add const to as many methods in bevy_ui as possible I have not applied the same treatment to the bundles in bevy_ui as it would require going into other bevy crates to implement const defaults for structs in bevy_text or relies on UiImage which calls HandleUntyped.typed() which isn't const safe. Alternatively the defaults could relatively easily be turned into a macro to regain some of the readability and conciseness at the cost of explicitness. Such a macro that partially implements this exists as a crate here: [const-default](https://docs.rs/const-default/latest/const_default/derive.ConstDefault.html) but does not support enums. Let me know if there's anything I've missed or if I should push further into other crates. Co-authored-by: Carter Anderson <[email protected]>
ItsDoot
pushed a commit
to ItsDoot/bevy
that referenced
this issue
Feb 1, 2023
# Objective - Fixes bevyengine#5529 ## Solution - Add assosciated constants named DEFAULT to as many types as possible - Add const to as many methods in bevy_ui as possible I have not applied the same treatment to the bundles in bevy_ui as it would require going into other bevy crates to implement const defaults for structs in bevy_text or relies on UiImage which calls HandleUntyped.typed() which isn't const safe. Alternatively the defaults could relatively easily be turned into a macro to regain some of the readability and conciseness at the cost of explicitness. Such a macro that partially implements this exists as a crate here: [const-default](https://docs.rs/const-default/latest/const_default/derive.ConstDefault.html) but does not support enums. Let me know if there's anything I've missed or if I should push further into other crates. Co-authored-by: Carter Anderson <[email protected]>
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
What problem does this solve or what need does it fill?
Constants are useful when manually defining UIs to get compile-time guarantees that your style / layout won't unexpectedly change at runtime.
However, many of the existing builder methods are not const, when they could be.
What solution would you like?
bevy_ui
, and make any methods that you canconst
. This is particularly important for methods that return a new struct of the type that the impl block is for (a "builder method").Default::default()
. This should be aDEFAULT
associated constant. Use these new constants in thedefault
trait implementations where feasible.What alternative(s) have you considered?
Use a
const fn new()
. This is less clear, doesn't display the actual value in docs, and may have indirection overhead (IIRC this varies based on the optimization level). These are simple values, they should be represented as such.The text was updated successfully, but these errors were encountered: