Skip to content

Commit df3673f

Browse files
james-j-obriencart
andcommitted
Add const to methods and const defaults to bevy_ui (#5542)
# Objective - Fixes #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]>
1 parent 2d727af commit df3673f

File tree

3 files changed

+238
-61
lines changed

3 files changed

+238
-61
lines changed

crates/bevy_ui/src/focus.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,47 @@ use smallvec::SmallVec;
3131
///
3232
/// Note that you can also control the visibility of a node using the [`Display`](crate::ui_node::Display) property,
3333
/// which fully collapses it during layout calculations.
34-
#[derive(
35-
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
36-
)]
34+
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
3735
#[reflect(Component, Serialize, Deserialize, PartialEq)]
3836
pub enum Interaction {
3937
/// The node has been clicked
4038
Clicked,
4139
/// The node has been hovered over
4240
Hovered,
4341
/// Nothing has happened
44-
#[default]
4542
None,
4643
}
4744

45+
impl Interaction {
46+
const DEFAULT: Self = Self::None;
47+
}
48+
49+
impl Default for Interaction {
50+
fn default() -> Self {
51+
Self::DEFAULT
52+
}
53+
}
54+
4855
/// Describes whether the node should block interactions with lower nodes
49-
#[derive(
50-
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
51-
)]
56+
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
5257
#[reflect(Component, Serialize, Deserialize, PartialEq)]
5358
pub enum FocusPolicy {
5459
/// Blocks interaction
55-
#[default]
5660
Block,
5761
/// Lets interaction pass through
5862
Pass,
5963
}
64+
65+
impl FocusPolicy {
66+
const DEFAULT: Self = Self::Block;
67+
}
68+
69+
impl Default for FocusPolicy {
70+
fn default() -> Self {
71+
Self::DEFAULT
72+
}
73+
}
74+
6075
/// Contains entities whose Interaction should be set to None
6176
#[derive(Default)]
6277
pub struct State {

crates/bevy_ui/src/geometry.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign};
119119
/// bottom: Val::Px(40.0),
120120
/// };
121121
/// ```
122-
#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)]
122+
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
123123
#[reflect(PartialEq)]
124124
pub struct UiRect {
125125
/// The value corresponding to the left side of the UI rect.
@@ -133,6 +133,13 @@ pub struct UiRect {
133133
}
134134

135135
impl UiRect {
136+
pub const DEFAULT: Self = Self {
137+
left: Val::DEFAULT,
138+
right: Val::DEFAULT,
139+
top: Val::DEFAULT,
140+
bottom: Val::DEFAULT,
141+
};
142+
136143
/// Creates a new [`UiRect`] from the values specified.
137144
///
138145
/// # Example
@@ -152,7 +159,7 @@ impl UiRect {
152159
/// assert_eq!(ui_rect.top, Val::Px(30.0));
153160
/// assert_eq!(ui_rect.bottom, Val::Px(40.0));
154161
/// ```
155-
pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self {
162+
pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self {
156163
UiRect {
157164
left,
158165
right,
@@ -175,7 +182,7 @@ impl UiRect {
175182
/// assert_eq!(ui_rect.top, Val::Px(10.0));
176183
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
177184
/// ```
178-
pub fn all(value: Val) -> Self {
185+
pub const fn all(value: Val) -> Self {
179186
UiRect {
180187
left: value,
181188
right: value,
@@ -313,10 +320,16 @@ impl UiRect {
313320
}
314321
}
315322

323+
impl Default for UiRect {
324+
fn default() -> Self {
325+
Self::DEFAULT
326+
}
327+
}
328+
316329
/// A 2-dimensional area defined by a width and height.
317330
///
318331
/// It is commonly used to define the size of a text or UI element.
319-
#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)]
332+
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
320333
#[reflect(PartialEq)]
321334
pub struct Size {
322335
/// The width of the 2-dimensional area.
@@ -326,6 +339,11 @@ pub struct Size {
326339
}
327340

328341
impl Size {
342+
pub const DEFAULT: Self = Self {
343+
width: Val::DEFAULT,
344+
height: Val::DEFAULT,
345+
};
346+
329347
/// Creates a new [`Size`] from a width and a height.
330348
///
331349
/// # Example
@@ -355,6 +373,12 @@ impl Size {
355373
};
356374
}
357375

376+
impl Default for Size {
377+
fn default() -> Self {
378+
Self::DEFAULT
379+
}
380+
}
381+
358382
impl From<(Val, Val)> for Size {
359383
fn from(vals: (Val, Val)) -> Self {
360384
Self {

0 commit comments

Comments
 (0)